how to use pytmx and make Rects

154 views Asked by At

i'm trying to get this pytmx work for a few days, but i just can't seem to get how to go on about it, all i want is to load all the layers and make a few layers to be Rects here, i'm just trying to make it blit on the screen, but that isn't working either

import pygame
import pytmx

pygame.init()

W, H = 800, 400
FPS = 60

win = pygame.display.set_mode((W, H), 0, 32)
pygame.display.set_caption('trying to make this work somewhow')

game_map = pytmx.load_pygame('map/map.tmx')

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    win.fill((146, 244, 200))

    for layer in game_map.visible_layers:
        for x, y, gid, in layer:
            tile = game_map.get_tile_image_by_gid(gid)
            win.blit(tile, (x * 16, y * 16))


    pygame.display.update()
pygame.quit()
1

There are 1 answers

0
Rabbid76 On

Not all the tiles have an image (pygame.Surface). Just evaluate if tile is not None:

while run:
    # [...]

    for layer in game_map.visible_layers:
        for x, y, gid, in layer:
            tile = game_map.get_tile_image_by_gid(gid)
            if tile:
                win.blit(tile, (x * 16, y * 16))

Alternatively, you can draw a colored rectangle at the position of the tile:

while run:
    # [...]

    for layer in game_map.visible_layers:

        for x, y, gid, in layer:
            tile = game_map.get_tile_image_by_gid(gid)
            if tile:
                win.blit(tile, (x * 16, y * 16))
            else:
                pygame.draw.rect(win, "pink", (x * 16, y * 16, 16, 16))