我试图让这个pytmx工作几天,但我似乎无法得到如何继续关于它,我想要的只是加载所有的层,并使几个层是矩形这里,我只是想让它在屏幕上闪烁,但这也不起作用
import pygame
import pytmx
pygame.init()
W, H = 800, 400
FPS = 60
win = pygame.display.set_mode((W, H), 0, 32)
pygame.display.set_caption('trying to make this work somewhow')
game_map = pytmx.load_pygame('map/map.tmx')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill((146, 244, 200))
for layer in game_map.visible_layers:
for x, y, gid, in layer:
tile = game_map.get_tile_image_by_gid(gid)
win.blit(tile, (x * 16, y * 16))
pygame.display.update()
pygame.quit()
不是所有的tile都有图像(pygame.Surface
)。仅计算tile
是否不是None
:
while run:
# [...]
for layer in game_map.visible_layers:
for x, y, gid, in layer:
tile = game_map.get_tile_image_by_gid(gid)
if tile:
win.blit(tile, (x * 16, y * 16))
或者,您可以在tile的位置绘制一个彩色矩形:
while run:
# [...]
for layer in game_map.visible_layers:
for x, y, gid, in layer:
tile = game_map.get_tile_image_by_gid(gid)
if tile:
win.blit(tile, (x * 16, y * 16))
else:
pygame.draw.rect(win, "pink", (x * 16, y * 16, 16, 16))