Skip to main content
 首页 » 编程设计

python之如何为我的 tilemap 中的图 block 赋予 sprite

2024年11月01日34Renyi-Fan

我是 Python 的新手,这是一个新手问题,但我如何分配我的 tilemap Sprite 中的图 block ?目前,我只是在 tilemap 中它们应该在的位置绘制矩形,它工作正常。但是,我想添加自己的纹理。

import pygame 
pygame.init() 
 
tilesize = 64 
mapwidth, mapheight = 20, 13 
screen = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize)) 
 
WALLTOP, WALLBOT, GRASS = range(3) 
wallTop = (0, 102, 0) 
wallBot = (51, 51, 0,) 
grass =   (0, 65, 0) 
 
colors = { 
    WALLTOP : wallTop, 
    WALLBOT : wallBot, 
    GRASS   : grass 
} 
 
# Tilemap 
tilemap = [ 
    [WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP], 
    [WALLTOP, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   WALLTOP, WALLTOP, WALLTOP, GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   WALLTOP, WALLTOP, WALLBOT, GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   WALLBOT, WALLBOT, GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS,   GRASS, GRASS,   GRASS,   GRASS,   GRASS,     WALLTOP], 
    [WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP], 
 
# Main loop 
running = True 
while running: 
    # Loop through the rows of the tilemap 
    for row in range(mapheight): 
        # Loop through the columns of the tilemap 
        for column in range(mapwidth): 
            # Draw the picture at the position in the tilemap 
            pygame.draw.rect(screen, colors[tilemap[row][column]], (column*tilesize, row*tilesize, tilesize, tilesize)) 
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running = False 
 
    pygame.display.update() 

请您参考如下方法:

用与 map 的每个元素对应的 block 传输图像替换绘图矩形。

用这样的字典替换 colors 字典和颜色定义:

tileimages = { 
    WALLTOP : pygame.image.load("path/to/image_walltop.png").convert(), 
    WALLBOT : pygame.image.load("path/to/image_wallbot.png").convert(), 
    GRASS   : pygame.image.load("path/to/image_grass.png").convert(), 
} 

pygame.image.load将加载图像,当然需要使用图像编辑器或其他任何工具提前准备好图像并将其存储在系统的某个位置。请务必提供正确的路径。
这里我使用了 png,但也可以是其他格式,有关支持格式的更多信息,请阅读我链接的文档。

要 blit 图像,在您的主循环中将 draw.rect 行替换为:

screen.blit(tileimages[tilemap[row][column]], (column*tilesize, row*tilesize)) 

注意:这里假设图像具有正确的大小,因此它是一个tilesize x tilesize图像(它变成了一个tilesize x tilesize Surface in pygame)。如果不是这样,结果会很糟糕。您需要使用 pygame.transform.scale将其重新调整为 tilesize x tilesize 表面。

因此您的 tileimages 字典应该如下所示:

tileimages = { 
    WALLTOP : pygame.transform.scale(pygame.image.load("path/to/image_walltop.png").convert(), (tilesize, tilesize)), 
    WALLBOT : pygame.transform.scale(pygame.image.load("path/to/image_wallbot.png").convert(), (tilesize, tilesize)), 
    GRASS   : pygame.transform.scale(pygame.image.load("path/to/image_grass.png").convert(), (tilesize, tilesize)), 
}