Problem with transparent tiles with Tiled and pytmx in pygame

941 views Asked by At

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()
1

There are 1 answers

1
lutre69 On

Found the solution !

Turns out that when I created the Tilesets in Tiled I checked the box "use transparency color". I had a hard time figuring this out because when I looked at the tileset properties in Tiled after the tileset was created, it showed that the transparency color was not set, and the transparency was taken into account in Tiled.