2

我无法让 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()
4

1 回答 1

3

找到了解决方案!

事实证明,当我在 Tiled 中创建 Tilesets 时,我选中了“使用透明度颜色”框。我很难弄清楚这一点,因为当我在创建瓦片集后查看 Tiled 中的瓦片集属性时,它显示没有设置透明度颜色,并且在 Tiled 中考虑了透明度。

于 2020-03-02T15:41:39.723 回答