我无法让 pytmx 在 pygame 中正确渲染 Tiled 中的透明图块。在此示例中,您可以看到从 tmx 文件渲染的图块显示黑色背景,我希望它像直接从图像文件渲染的图像一样。
我试过弄乱.convert()
,.convert_alpha()
甚至放旗帜pygame.SCRALPHA
,但没有运气。
这是获取用于重现示例的资产的链接:https ://filebin.net/yvmr5jz04j889mlx
这是示例的代码:
import pygame
import pytmx
pygame.init()
gameScreen = pygame.display.set_mode((280, 210))
clock = pygame.time.Clock()
# filling in white to see the lack of alpha
gameScreen.fill((255, 255, 255))
# bliting from tmx file, (the alpha is not recognized)
gameMap = pytmx.load_pygame('test_map.tmx')
for layer in gameMap.visible_layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = gameMap.get_tile_image_by_gid(gid)
if tile:
gameScreen.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
# bliting the image directly from pygame (the alpha is correctly recognized)
rock = pygame.image.load('rock.png')
gameScreen.blit(rock, (140, 70))
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()