1

I have made a map in the tiled editor and intend to use it in a game using pygame. I imported pytmx to help with this process but when I use load_pygame("directory of file") it give out this error


Traceback (most recent call last):
  File "D:\Coder man\code\GUI\game map\showing map.py", line 7, in <module>
    tmx_data = load_pygame("D:\Tile sheet\Dungeon Hub room.tmx")
  File "C:\Users\Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytmx\util_pygame.py", line 183, in load_pygame
    return pytmx.TiledMap(filename, *args, **kwargs)
  File "C:\Users\Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytmx\pytmx.py", line 501, in __init__
    self.parse_xml(ElementTree.parse(self.filename).getroot())
  File "C:\Users\Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytmx\pytmx.py", line 550, in parse_xml
    self.add_tileset(TiledTileset(self, subnode))
  File "C:\Users\Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytmx\pytmx.py", line 1101, in __init__
    self.parse_xml(node)
  File "C:\Users\Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pytmx\pytmx.py", line 1129, in parse_xml
    source, self.parent.filename, path
Exception: Cannot find tileset file :/automap-tiles.tsx from D:\Tile sheet\Dungeon Hub room.tmx, should be at D:\Tile sheet\:\automap-tiles.tsx

the code I have so far is this:

import pygame, sys
from pytmx.util_pygame import load_pygame


pygame.init()
screen = pygame.display.set_mode((1280, 720))
tmx_data = load_pygame("D:\Tile sheet\Dungeon Hub room.tmx")
print(tmx_data)


# game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill("black")
    pygame.display.update()


i was expecting the pygame to come up with no errors and an output of:

<Tiledmap: "Tile sheet\Dungeon Hub room.tmx">

and a pygame window

I have tried double checking the directory of the file and using pytmx.load_pygame

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

2 Answers2

0

The error message is clearly saying what's wrong.

Cannot find tileset file :/automap-tiles.tsx ... at D:\Tile sheet\:\automap-tiles.tsx

Your tile map is referencing an external tile set file(*.tsx), and its file path is specified by a full path without a drive letter, defined by Microsoft, which I'll call a semi-absolute path, both not an absolute path and not a relative path. That kind of path is not supported by PyTMX.

There are two solutions for this.

  1. Change the tile set path to either an absolute or relative path, correctly pointing the tile set file.

  2. Save the tile map so that it embeds the tile set. Try the 'Embed tilesets' export options in the preferences dialog of TileD.

relent95
  • 3,703
  • 1
  • 14
  • 17
0

Do not use backslashes in a file path, because "\" starts and escape sequence (see String and Bytes literals, e.g. "\n" is new line, "\t" is tab). You have 3 options:

  1. / instead of \:

    tmx_data = load_pygame("D:/Tile sheet/Dungeon Hub room.tmx")
    
  2. \\ instead of \ (the escapes sequence "\\" is a single backslash)

    tmx_data = load_pygame("D:\\Tile sheet\\Dungeon Hub room.tmx")
    
  3. use a raw string literal (r"...")

    tmx_data = load_pygame(r"D:\Tile sheet\Dungeon Hub room.tmx")
    
Rabbid76
  • 202,892
  • 27
  • 131
  • 174