pythonpython-3.xpygametilepytmx

Pygame Load Tilemap With PyTMX


I am trying to understand the tilemaps in pygame, and to learn how to use Tiled Map Editor, but I can't succeed to load the map in the pygame. Here is the code:

import pygame
import pytmx

pygame.init()

display = pygame.display.set_mode((600,400))
clock = pygame.time.Clock()

gameMap = pytmx.TiledMap("map.tmx")

while(True):

    clock.tick(60)
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            quit()

    if(keys[pygame.K_ESCAPE]):
        quit()

    for layer in gameMap.visible_layers:
            for x, y, gid, in layer:
                tile = gameMap.get_tile_image_by_gid(gid)
                if(tile != None):
                    display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))

    pygame.display.update()

It keep giving me this error:

Traceback (most recent call last): File "main.py", line 27, in display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight)) TypeError: argument 1 must be pygame.Surface, not tuple

I know that when I print the tile in console this is what I get

(a tuple): ('img/NES - Super Mario Bros - Tileset.png', (0, 144, 16, 16), TileFlags(flipped_horizontally=False, flipped_vertically=False, flipped_diagonally=False)).

What is the simplest method to successfully load a tilemap in pygame and what am I doing wrong?


Solution

  • use:

    gameMap = pytmx.load_pygame("map.tmx")
    

    instead of:

    gameMap = pytmx.TiledMap("map.tmx")