我不能让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()发布于 2020-03-02 23:41:40
找到解决方案了!
原来,当我在Tilesets中创建Tilesets时,我选中了“使用透明颜色”复选框。我花了很长时间才弄明白这一点,因为当我在创建磁贴集后查看磁贴中的磁贴集属性时,它显示没有设置透明度颜色,并且在磁贴中考虑了透明度。
https://stackoverflow.com/questions/60490707
复制相似问题