pygame中带有平铺和pytmx的透明瓷砖存在问题

2024-04-27 03:25:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法让pytmx在pygame中正确渲染平铺的透明瓷砖。 在本例中,您可以看到从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()

Tags: theinimagealphalayerforifpygame
1条回答
网友
1楼 · 发布于 2024-04-27 03:25:07

找到了解决办法

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

相关问题 更多 >