I can't get pytmx to render transparent tiles from Tiled correctly in pygame. On this example you can see that the tile rendered from the tmx file is showing a black background, i would like to have it like the image rendered directly from the image file.
I have tried messing with .convert()
, .convert_alpha()
or even putting the flag pygame.SCRALPHA
but no luck there.
Here is a link to get the assets for reproducing the example : https://filebin.net/yvmr5jz04j889mlx
Here is the code of the example :
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()