首页 > 解决方案 > blit:不能显示多个精灵

问题描述

我用 Tiled 创建了一张地图,但现在我在 pygame 上正确显示它时遇到了问题。它只显示一个精灵(用grass.png 填充整个屏幕)。

pygame.init()
tilemap = Tilemap({
    # Load the textures
    0: pygame.image.load("grass.png"),
    1: pygame.image.load("water.png")},
                  
    100, # Tile size
    10, # Width
    10, # Height
    2) # Border size (default is 0)


DISPLAYSURF = pygame.display.set_mode(tilemap.window_size)
tilemap.read_csv('Mappa.csv')
tilemap.draw(DISPLAYSURF) # Draw the map

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

从班级:

class Tilemap:
    tilemap = []

    def __init__(self, textures, tilesize, width, height, borders=0):
        self.textures = textures # List of textures in dictionary format
        self.tilesize = tilesize + borders # The size of a tile
        self.width = width # How many tiles wide the map is
        self.height = height # How many tile high the map is
        self.window_size = [self.tilesize*self.width-borders,
                            self.tilesize*self.height-borders]
        self.mapsize = [self.width, self.height]
        self.borders = borders

    def read_csv(self, filename):
        
        with open(os.path.join(filename)) as data:
            data = csv.reader(data, delimiter=',')
            for row in data:
                self.tilemap.append(list(row))
        return self.tilemap

        

    def draw(self, display):
        for row in range( self.tilesize -self.borders  ):

            for column in range( self.tilesize -self.borders   ):
                Texture = int(self.tilemap[row][column])


                display.blit(self.textures[Texture],
                        (column*self.tilesize, row*self.tilesize))

草纹理

草.png

水纹理

水.png

它应该做这样的事情:

地图

但最终的结果只是草:

最后

标签: pythonpygamesprite

解决方案


推荐阅读