运行此代码时,我从Python解释器中收到一个奇怪的错误:
def make_map():
map = [[Tile(0, 0) for col in range(MAP_WIDTH)] for row in range(MAP_HEIGHT)]
for x in range(MAP_WIDTH):
for y in range(MAP_HEIGHT):
map[x][y].tileType = round((libtcod.noise_perlin(noise2d,[y/MAP_WIDTH,x/MAP_HEIGHT])*100), 0)
Run Code Online (Sandbox Code Playgroud)
它在终端返回:
TypeError: 'builtin_function_or_method' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)
回溯也指向此功能:
def render_all():
global color_light_wall
global color_light_ground
#go through all tiles, and set their background color
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
tileType = map[x][y].tileType
if tileType>30:
libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET )
else:
libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET )
#draw all objects in the list
for object in objects:
object.draw()
#blit the contents of "con" to the root console
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
我认为他们都与这条线有关: tileType = map[x][y].tileType
但是如果有人能够对此有所了解,我会很感激.
谢谢,Elliot Bonneville
编辑:我忘了包含我的Tile类代码和完整的回溯:
class Tile:
#a tile of the map and its properties
def __init__(self, tileType, blocked):
self.tileType = tileType
self.blocked = blocked
Run Code Online (Sandbox Code Playgroud)
追溯:
File "kindred.py", line 123, in <module>
render_all()
File "kindred.py", line 64, in render_all
tileType = map[x][y].tileType
TypeError: 'builtin_function_or_method' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)
这个错误意味着python尝试获取一个变量名称"map",tileType = map[x][y].tileType
但是他没有找到任何地方所以它在功能映射中获取构建,这是不可取消的,因为它是一个解释错误消息的内置函数:
TypeError: 'builtin_function_or_method' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)
我建议你首先将你的变量名称从"map"更改为不影响任何内置函数的内容,然后当你更改变量名时你应该有一个NameError
错误,因为你的变量没有定义所以你应该解决这个问题
希望我做对了,希望这有帮助:)