gpt4 book ai didi

python - 如何在 pygame 中导入 tmx map ?

转载 作者:行者123 更新时间:2023-11-28 22:12:37 26 4
gpt4 key购买 nike

我在 Tiled Editor 程序中制作了一个 *tmx map 。然后我尝试将其导入到我的游戏中。当我将变量 layers 更改为 0 时,它可以工作,但屏幕上只有 1 个图 block 。我想在屏幕上打印整个 map 。但我收到以下错误。

Traceback (most recent call last):
File "C:\Users\LL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytmx\pytmx.py", line 512, in get_tile_image
layer = self.layers[layer]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\LL\Desktop\Erik\RPG_project\RPG project\data\main.py", line 143, in <module>
game_initialize()
File "C:\Users\LL\Desktop\Erik\RPG_project\RPG project\data\main.py", line 117, in game_initialize
map_setup()
File "C:\Users\LL\Desktop\Erik\RPG_project\RPG project\data\main.py", line 140, in map_setup
image = tmxdata.get_tile_image(0, 0, 2)
File "C:\Users\LL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytmx\pytmx.py", line 514, in get_tile_image
raise ValueError
ValueError

我认为这与我的图层有关。我的 map 只有 1 层。我的脚本仍然不起作用。我还为我的 map 使用 Base64(压缩)。和 32 像素大图 block 。

from pytmx import load_pygame

def map_setup():
global image

# Getting / Importing the map
tmxdata = load_pygame("Tile_files\\mymap2.tmx")

image = tmxdata.get_tile_image(0, 0, 1) # x, y, layer

最佳答案

因为tile看起来只有(0,0)位置。你必须给层循环。我写的这段代码也许对你有帮助。

from pytmx import load_pygame, TiledTileLayer

def map_setup():
global image

# Getting / Importing the map
tmxdata = load_pygame("Tile_files\\mymap2.tmx")
width = tmxdata.width * tmxdata.tilewidth
height = tmxdata.height * tmxdata.tileheight

ti = tmxdata.get_tile_image_by_gid
for layer in tmxdata.visible_layers:
if isinstance(layer, TiledTileLayer):
for x, y, gid, in layer:
tile = ti(gid)
if tile:
image = tmxdata.get_tile_image(x, y, layer)

顺便说一句,请原谅我的语言。希望有效。

编辑:您可以观看此视频,详细了解在 pygame 中导入 tmx map 。视频链接:https://www.youtube.com/watch?v=QIXyj3WeyZM

关于python - 如何在 pygame 中导入 tmx map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com