Feline Feline - 2 months ago 28
Python Question

How to use pytmx in pygame?

There was a question similar to this but the persons question wasn't regarding the same way I'm trying to implement

pytmx
, so this is the first time I've worked with
tiled
, or
pytmx
and I'm having trouble with the docs on
pytmx
.

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

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

Answer

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?

To address the biggest issues:

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

gameMap = pytmx.TiledMap('gamemap.tmx')

You only need to load the map data once. Do this before the game loop.

2) The only drawing happening in your code is this:

image = gameMap.get_tile_image(0, 0, 0)
gameScreen.blit(image, position)

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.

3) "what is the layer parameter in reference to"

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.

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?

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

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

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.

Comments