首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python libtcod:对象描述特性错误

Python libtcod:对象描述特性错误
EN

Stack Overflow用户
提问于 2014-07-16 12:57:55
回答 1查看 101关注 0票数 0

我正在学习libtcod python教程,我决定处理一些代码,使其在今天变得更加独特,并决定从一个功能开始,允许玩家将鼠标悬停在一个对象上,然后按'd‘键来描述这个对象。

我目前遇到了一个属性错误:'str‘对象没有属性'describe’第657行。我尝试过许多不同的事情,但通知似乎是有效的,不幸的是,我的理解水平现在是相当有限的,所以我不知道哪里出了问题。

以下是相关的类和功能:

代码语言:javascript
复制
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

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-16 13:04:54

函数description_menu()具有以下return

代码语言:javascript
复制
names[index].description

这是一个属于Object的字符串成员。

当你说

代码语言:javascript
复制
chosen_object.describe()

您正在调用describe()方法,但它属于Object类,而不是字符串(因此是attribute error: 'str' object has no attribute 'describe')。您必须让description_menu()返回Object,而不是只返回它的名称。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24781420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档