在pycharm(pygame)中运行pyTMX时发生TypeError

时间:2018-08-01 13:02:29

标签: python pygame typeerror pytmx

我试图在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

0 个答案:

没有答案