Python' builtin_function_or_method'对象没有属性' __ getitem __'列表清单

时间:2017-06-29 07:51:28

标签: python-2.7

通常这个错误意味着括号出现问题(例如使用括号,缺失,错误的点等),但这似乎不是问题所在。

#iterate through all the tiles on the map and set as a background color
for y in range(MAP_WIDTH):
    for x in range(MAP_HEIGHT):
        #checks if the tile is a wall
        wall = map[x][y].block_sight
        if wall:
            rlib.console_set_char_background(
            con, x, y, color_dark_wall, rlib.BKGND_SET)
        else:
            rlib.console_set_char_background(
            con, x, y, color_dark_ground, rlib.BKGND_SET)

错误发生在wall = map行中。该映射是一个列表,用于模拟python中数组的功能。 block_sight为true或false,并在此处设置:

class Tile:
    #Tiles are components of the map
    def __init__(self, blocked, block_sight = None):
        #takes the information and stores it on the tile
        self.blocked = blocked

        #if not specified, block_sight if the same as blocked
        if block_sight is None: block_sight = blocked
        self.block_sight = block_sight

对此的任何帮助将不胜感激。

编辑:以下是地图的构建方式:

#generates a list of lists with empty tiles as elements
def makemap():
    map = [[Tile(False) #Must call a conctructor, not a variable such as floor
        for y in range(MAP_HEIGHT)] #uses comprehension to generate lists
            for x in range(MAP_WIDTH)]

    #place two pillars to test the map
    map[30][22].blocked = True
    map[30][22].block_sight = True
    map[50][22].blocked = True
    map[50][22].block_sight = True

1 个答案:

答案 0 :(得分:0)

在函数makemap()中,我忘记使用map作为全局变量,因此任何需要使用此函数构建的映射都无法访问它。添加“全局地图”作为函数的第一行解决了错误。