Int对象没有属性'__getitem__'

时间:2018-12-25 21:38:35

标签: python pygame

我正在使用Pygame制作一款小型游戏,需要提取玩家站立的当前瓷砖并将其保存到库存中。

我在论坛上浏览了一个类似的问题,但这并没有帮助我解决问题。我很难理解为什么它不起作用。

inventory = {
            Elements.DIRT : 0,
            Elements.GRASS : 0,
            Elements.WATER : 0,
            Elements.COAL : 0,
            Elements.ROCK : 0,
            Elements.LAVA : 0
        }

tilemap = [[Elements.DIRT for w in range(MAPWIDTH)] for h in 
range(MAPHEIGHT)]

for rw in range(MAPHEIGHT):
    for cl in range(MAPWIDTH):
        randomNumber = random.randint(0,100)
        if randomNumber == 0:
            tile = Elements.LAVA
        elif randomNumber >= 1 and randomNumber <= 10:
            tile = Elements.COAL
        elif randomNumber >= 11 and randomNumber <= 30:
            tile = Elements.WATER
        elif randomNumber >= 31 and randomNumber <= 75:
            tile = Elements.GRASS
        else:
            tile = Elements.DIRT
        tilemap[rw][cl] = tile


if event.key == K_SPACE:
        #What resource is the player standing on?
        currentTile = tilemap[playerPos[1][playerPos[0]]]
        #Player now has 1 more of this resource
        inventory[currentTile] += 1
        #The player is now standing on dirt
        tilemap[playerPos[1][playerPos[0]]] = Elements.DIRT
        print(inventory)

我可以启动它,但是每当我点击空格时,我都会得到'int'对象没有属性'__getitem__'。我希望打印库存字典。

1 个答案:

答案 0 :(得分:1)

这些行上的括号不匹配

currentTile = tilemap[playerPos[1][playerPos[0]]]

tilemap[playerPos[1][playerPos[0]]] = Elements.DIRT

应该是:

currentTile = tilemap[playerPos[1]][playerPos[0]]  

tilemap[playerPos[1]][playerPos[0]] = Elements.DIRT

以前,您尝试使用索引tilemap访问playerPos[1][playerPos[0]]二维数组。这本身就是试图访问索引为playerPos[1]的{​​{1}},这是错误的出处。