0
我正在通过libtcod python教程开展工作,我决定在今天使一些代码更加独特,并决定开始使用一个功能允许玩家将鼠标悬停在某个物体上,然后按'd'查看该物体的描述。Python libtcod:对象描述功能错误
我目前遇到属性错误:'str'对象没有属性'describe'line 657.我尝试了很多不同的东西,但notihng似乎工作,不幸的是我的理解水平现在相当有限所以我无法弄清楚发生了什么问题。
下面是相关类和函数:
class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it's always represented by a character on screen.
def __init__(self, x, y, char, name, color, blocks=False, fighter=None, ai=None, item=None, description=None):
self.x = x
self.y = y
self.char = char
self.name = name
self.color = color
self.blocks = blocks
self.fighter = fighter
if self.fighter: #let the fighter component know who owns it
self.fighter.owner = self
self.ai = ai
if self.ai: #let the ai component know who owns it
self.ai.owner = self
self.item = item
if self.item: #let the item component know who owns it, like a bitch
self.item.owner = self
self.description = self
if self.description: #let the description component know who owns it
self.description.owner = self
def describe(self):
#describe this object
message(self.description, libtcod.white)
def handle_keys():
global keys;
if key_char == 'd':
#show the description menu, if an item is selected, describe it.
chosen_object = description_menu('Press the key next to an object to see its description.\n')
if chosen_object is not None:
chosen_object.describe()
return 'didnt-take-turn'
def description_menu(header):
global mouse
#return a string with the names of all objects under the mouse
(x, y) = (mouse.cx, mouse.cy)
#create a list with the names of all objects at the mouse's coordinates and in FOV
names = [obj.name for obj in objects if obj.x == x and obj.y == y and libtcod.map_is_in_fov(fov_map, obj.x, obj.y)]
names = ', '.join(names) #join the names, seperated by commas
return names.capitalize()
#show a menu with each object under the mouse as an option
if len(names) == 0:
options = ['There is nothing here.']
else:
options = [item.name for item in names]
index = menu(header, options, INVENTORY_WIDTH)
#if an item was chosen, return it
if index is None or len(names) == 0: return None
return names[index].description
任何帮助,将不胜感激!
你的意思是这样的: “#如果选择一个项目,返回它 \t如果指数是无或LEN(名称)== 0:返回无 \t返回对象” 很抱歉,如果我很难理解,并非常感谢你的回应。 –
好吧我尽我所能地改变了代码,并且我进一步了一点,但现在返回'list'对象没有'describe'属性。你有什么想法吗? –
'DEF description_menu(报头): \t全局鼠标 \t \t (X,Y)=(mouse.cx,mouse.cy) \t \t \t名称= [OBJ用于如果对象OBJ obj.x == x和obj.y == y和libtcod.map_is_in_fov(fov_map,obj.x,obj.y)] \t \t 返回名称 \t如果len(地名)== 0: \t个\t选项= [ '这里没有什么。'] \t其他: \t \t选项= [object.name在名称对象] \t \t \t \t指数=菜单(头,期权,INVENTORY_WIDTH) \t \t#如果一个项目被选中,返回 \t如果索引是None或len(names)== 0:return None \t返回名称' –