首页 > 解决方案 > 在 pycharm (pygame) 中运行 pyTMX 时出现 TypeError

问题描述

我正在尝试在 pyCharm 中使用 pyGame 和 pyTMX;但是,每当我尝试运行该程序时,都会收到一条恼人的错误消息。以下代码是

def get_tile_properties(self, x, y, layer):
    """ Return the tile image GID for this location
    :param x: x coordinate
    :param y: y coordinate
    :param layer: layer number
    :rtype: python dict if found, otherwise None
    """
    try:
        assert (x >= 0 and y >= 0 and layer >= 0)
    except AssertionError:
        raise ValueError

    try:
        gid = self.layers[int(layer)].data[int(y)][int(x)]
    except (IndexError, ValueError):
        msg = "Coords: ({0},{1}) in layer {2} is invalid."
        logger.debug(msg.format(x, y, layer))
        raise Exception

    else:
        try:
            return self.tile_properties[gid]
        except (IndexError, ValueError):
            msg = "Coords: ({0},{1}) in layer {2} has invalid GID: {3}"
            logger.debug(msg.format(x, y, layer, gid))
            raise Exception
        except KeyError:
            return None

Traceback (most recent call last):
  File "C:/Users/tett/Documents/Vietnam/test.py", line 16, in <module>
    properties = tmxdata.get_tile_properties(x, y, layer)
  File "C:\Users\tett\PycharmProjects\untitled\venv\lib\site- 
packages\pytmx\pytmx.py", line 568, in get_tile_properties
    assert (x >= 0 and y >= 0 and layer >= 0)
TypeError: '>=' not supported between instances of 'TiledTileLayer' and 'int'

我在发布这篇文章前两周开始学习 pyGame,所以我知道平铺地图是如何超出我的技能水平的。因此,我从 pyTMX 网站上复制了这个和其他一些代码。我相信平铺地图是我在继续之前需要为我的游戏学习的下一个最重要的东西。代码似乎不起作用(至少对我来说),这就是为什么我在这里看看是否有人有解决方案。我在 Youtube 上查看,那些平铺地图教程视频不起作用,我解决此问题的解决方案也不起作用。

我复制来调用 pyTMX 文件的代码如下所示。m1w1.tmx 是我要加载的文件。

tmxdata = pytmx.TiledMap("m1w1.tmx")
tiled_map = pytmx.TiledMap('m1w1.tmx')
for layer in tiled_map.layers:
    for x, y, image in layer.tiles():
        for obj in layer:

            # tile ('GID') properties are accessed through the TiledMap:
            properties = tmxdata.get_tile_properties(x, y, layer)
            bbox = obj.x, obj.y, obj.width, obj.height
            points = obj.points
            # if obj.closed == True, then obj is a polygon

标签: pythonpygametypeerrorpytmx

解决方案


推荐阅读