繁体   English   中英

如何在Pygame中绘制精灵列表

[英]How to draw a list of sprites in Pygame

我问是否有可能在Pygame中绘制精灵列表,如果可以,怎么办?

我正在尝试从二维列表中绘制地图,这将绘制地图

导入pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)


screen_width = 700
screen_height = 400

screen = pygame.display.set_mode([screen_width, screen_height])


pygame.init()

image = pygame.image.load("Textures(Final)\Grass_Tile.bmp").convert()
imagerect = image.get_rect()

tilemap = [
          [image, image, image],
          [image, image, image]
        ]


done = False

clock = pygame.time.Clock()



while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(BLACK)

    screen.blit(tilemap, imagerect)    

    clock.tick(20)

    pygame.display.flip()


pygame.quit()

我知道我可以做得更简单,例如绘制每张图像。 但是,我想知道是否可以执行此操作,因此在将来有更多精灵时,我可以将其添加到列表中并创建地图。

谢谢!

我不认为这在您描述时是完全可能的,但是如果这些图像不会经常更改,我建议将所有地图图像预先绘制到pygame Surface上,然后使用,例如:

map_surface = pygame.Surface(( len(tilemap[0])*TILE_WIDTH, len(tilemap)*TILE_HEIGHT ))
for y,row in enumerate(tilemap):
    for x,tile_surface in enumerate(row):
        map_surface.blit(tile_surface,(x*TILE_WIDTH,y*TILE_HEIGHT))

然后,您可以简单地使用:

screen.blit(map_surface)

需要注意的一件事是,即使地图确实发生了变化,您也只需将更改后的图块map_surfacemap_surface曲面上,而不必重新创建整个图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM