How to use pytmx in pygame?
<p>There was a question similar to this but the persons question wasn't regarding the same way I'm trying to implement <code>pytmx</code>, so this is the first time I've worked with <code>tiled</code>, or <code>pytmx</code> and I'm having trouble with the docs on <code>pytmx</code>.</p>

<p>I just cannot get the code to execute right. What is the position parameter in the <code>gameScreen.blit(image, position)</code>? and what is the layer parameter in reference to in this module?</p>

<pre><code>import pygame
import time
import random
import pyganim
import sys
import os
import pytmx
from pytmx import load_pygame

pygame.init()


display_width = 800
display_height = 800

white = (255, 255, 255)

gameScreen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('2d Game')
clock = pygame.time.Clock()
def game_loop():
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
        gameMap = pytmx.TiledMap('gamemap.tmx')
        image = gameMap.get_tile_image(0, 0, 0)
        gameScreen.blit(image, position)
        pygame.display.update()
        clock.tick(30)
game_loop()
pygame.quit()
quit()
</code></pre>

<p>There are a lot of things wrong with this code. - did you write it or get it from somewhere else?  It may be that you need to make sure you understand Pygame better before you try to do something as advanced as using a Tiled map. Also, is this the whole code, or did you leave some out?</p>

<p>To address the biggest issues:</p>

<p>1) You are loading your Tiled map every frame, which is a huge problem:</p>

<pre><code>gameMap = pytmx.TiledMap('gamemap.tmx')
</code></pre>

<p>You only need to load the map data once.  Do this before the game loop.</p>

<p>2) The only drawing happening in your code is this:</p>

<pre><code>image = gameMap.get_tile_image(0, 0, 0)
gameScreen.blit(image, position)
</code></pre>

<p>This gets the tile at position (0, 0) on layer 0 of your map and blits it at the coordinates specified by a variable called "position" that doesn't seem to exist.  So even if it worked it would only draw one tile on the screen.</p>

<p>3) "what is the layer parameter in reference to"</p>

<p>Have you used Tiled before?  Tiled is a map editor that lets you draw in multiple layers, so that you can have tiles on top of other tiles.  Layer 0 would be the first (bottom) layer of your map.</p>

<p>4) If you're using PyTMX with Pygame, you need to use the load_pygame() function, which it looks like you imported, but didn't use.  In fact, there are a ton of imports - what are they all there for?</p>

<p>A simple working version of this code would load the map, and then blit all the tiles on the screen, like so:</p>

<pre><code>import pygame
import pytmx

pygame.init()


display_width = 800
display_height = 800

white = (255, 255, 255)

gameScreen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('2d Game')
clock = pygame.time.Clock()

# load map data
gameMap = pytmx.load_pygame('gamemap.tmx')

def game_loop():
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               gameExit = True

    # draw map data on screen
    for layer in gameMap.visible_layers:
        for x, y, gid, in layer:
            tile = gameMap.get_tile_image_by_gid(gid)
            gameScreen.blit(tile, (x * gameMap.tilewidth,
                                   y * gameMap.tileheight))
    pygame.display.update()
    clock.tick(30)

game_loop()
pygame.quit()
</code></pre>

<p>Note, this is not the best way of doing it, but it is the simplest.  I encourage you to look for some Pygame and PyTMX tutorials and work your way up.  It can be very frustrating to try and learn too many new things at the same time.  As you can tell from the PyTMX docs, it's not really targeted at the beginner level.</p>

python python-3.x pygame