首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python libtcod奇怪描述错误

Python libtcod奇怪描述错误
EN

Stack Overflow用户
提问于 2014-07-17 02:09:59
回答 1查看 93关注 0票数 0

我很惊讶这没有崩溃,但我真的不确定这里发生了什么。在鼠标悬停+键按下'd‘时,应调出鼠标下的对象菜单(确实如此)。当选择一个对象时,它应该从该对象描述中打印一条消息到控制台。但它不会,它会打印像<__main__.Object instance at 0x02AE1800>这样的东西。

下面是相关的代码

代码语言:javascript
复制
def menu(header, options, width):
    if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.'

    #calculate the total height for the header (afer auto wrap) and one line per option
    header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)
    height = len(options) + header_height

    #create an off-screen console that represents the menu's window
    window = libtcod.console_new(width, height)

    #print the header, with auto wrap, baby.
    libtcod.console_set_default_foreground(window, libtcod.white)
    libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)

    #print all the options
    y = header_height
    letter_index = ord('a')
    for option_text in options:
        text = '(' + chr(letter_index) + ') ' + option_text
        libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)
        y += 1
        letter_index += 1

    #blit the contents of "window" to the root console
    x = SCREEN_WIDTH/2 - width/2
    y = SCREEN_HEIGHT   /2 - height/2
    libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)

    #present the root console to the player and wait for keypress
    libtcod.console_flush()
    key = libtcod.console_wait_for_keypress(True)

    #convert the ASCII code to an index; if it corresponds to an option, return it
    index = key.c - ord('a')
    if index >= 0 and index < len(options): return index
    return None

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()
                else:
                    return 'cancelled'

            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 for obj in objects if obj.x == x and obj.y == y and libtcod.map_is_in_fov(fov_map, obj.x, obj.y) and obj.description is not None]

    #show a menu with each object under the mouse as an option
    if len(names) == 0:
        options = ['There is nothing here.']
    else:
        options = [object.name for object 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]

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
        if self.description is None:
            message('The ' + self.owner.name + ' cannot be described.')
        else:
            message(str(self.description), libtcod.white)`
EN

回答 1

Stack Overflow用户

发布于 2014-11-17 08:13:06

我的猜测是,当您确实想要传递一个包含一些易于理解的信息的字符串时,您会将Object实例的字符串表示传递给messagestr方法调用Object.__str__ (python的magic methods之一) Object.__str__没有在您的代码中定义,所以str默认调用Object.__repr__,这是您获取<__main__.Object instance at 0x02AE1800>的地方。

尝试定义Object.__str__,使其返回一个字符串,其中包含您希望在print-ing时显示的信息。

例如:

代码语言:javascript
复制
Class Object:
    def __init__(self, name, x, y):
        self.name = name
        self.x = x
        self.y = y

    def __str__(self):
        return("Object " + self.name + " is at position [" + self.x + "," + self.y + "]")

最后,我不能确定问题出在哪里,因为我们没有message方法的定义。您应该包含该方法的代码,以便我们能够更好地了解发生了什么。祝好运!

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

https://stackoverflow.com/questions/24787789

复制
相关文章

相似问题

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