2

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()
Kingsley
  • 14,398
  • 5
  • 31
  • 53
lutre69
  • 139
  • 6
  • Can you add images inline without reference to external websites? You should make sure that your question is self-contained. – Arman Mar 02 '20 at 14:42
  • No I can't, It appears that since i'm a new contributor to stackoverflow I can't add images to my posts yet. I added those files so that anyone can reproduce what I'm talking about. – lutre69 Mar 02 '20 at 15:09

1 Answers1

3

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.

lutre69
  • 139
  • 6
  • Hey @lutre69, I tried to reproduce your issue with the transparency color appearing as not being set, but I couldn't see it in the latest Tiled. If you can reproduce this problem, please open an issue at https://github.com/bjorn/tiled/issues. Thanks! – Thorbjørn Lindeijer Mar 03 '20 at 13:26