IndexError: list index out of range

IndexError: list index out of range

在教程中跌跌撞撞时,我遇到了一个反复出现的问题(我说反复出现,因为它似乎在我放弃了一天之后自发解决,后来又开始出现,我不不知道我做了什么来做到这一点)错误:

Traceback (most recent call last):
  File "roguelike.py", line 215, in <module>
    make_map()
  File "roguelike.py", line 131, in make_map
    create_room(new_room)
  File "roguelike.py", line 84, in create_room
    map[x][y].blocked = False
IndexError: list index out of range

相关代码如下:

def create_room(room):
    global map
    #set all tiles inside the room to passable
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[x][y].blocked = False
            map[x][y].block_sight = False

最后我将我编写的逐个字符的代码与教程中编写的示例代码进行了比较,但没有成功。这是程序中第一次使用 'map',python.

中的列表我很迷茫

最后一点,我本着同样的精神探讨了其他问题,但由于我的知识有限,我无法将它们应用到我的情况中。

room.x1 + 1room.x2room.y1 + 1room.y2 中至少有一个超出了您的地图。请检查您的地图大小或限制访问权限。

选项 1:

class RoomOutsideError(Exception):
    pass

# ...

def create_room(room):
    global map
    #set all tiles inside the room to passable

    # check for limits and raise exception
    #    (this will need handling outside this function)
    max_x, max_y = len(map) - 1, len(max[0]) - 1
    if room.x1+1 > max_x or room.x2 > max_x or \
       room.y1+1 > max_y or room.y2 > max_y:
           raise RoomOutsideError("Room outside map.")

    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[x][y].blocked = False
            map[x][y].block_sight = False

选项 2:

def create_room(room):
    global map
    #set all tiles inside the room to passable

    # limit room coordinates. This is free of additional handlings but
    # might create odd rooms in the corner of the map.
    max_x, max_y = len(map) - 1, len(map[0]) - 1
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[min(max_x, x)][min(max_y, y)].blocked = False
            map[min(max_x, x)][min(max_y, y)].block_sight = False