2014-07-16 1 views
0

libtcod 파이썬 튜토리얼을 진행하면서 일부 코드를 사용하여 더 독특한 방식으로 코드를 작성하고 기능을 시작하기로 결정했습니다. 플레이어가 개체 위에 마우스를 올려 놓고 해당 개체에 대한 설명을 보려면 'd'를 누릅니다.파이썬 libtcod : 객체 설명 기능 오류

'str'개체의 'describe'속성에 'describe'행이 없습니다. 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 

:

여기에 관련 클래스와 기능은 다음과 같습니다!

답변

1

함수 description_menu() 이것은 Object에 속하는 문자열 부재 인 다음 return

names[index].description 

있다.
당신은

chosen_object.describe() 

당신은 describe() 메소드를 호출하는

을 말할 때, 그러나 그것은 Object 클래스가 아닌 문자열 (따라서 attribute error: 'str' object has no attribute 'describe')에 속한다. description_menu()은 그 이름 대신 Object을 반환해야합니다.

+0

당신은 다음과 같은 의미합니까 : 가 '항목이 선택되었다 # 만약, 그것을 \t를 돌려 지수가 없음 또는 LEN (이름) == 0 인 경우 : 없음에게 \t 반환 객체 반환하지'난 경우 미안 느리게 이해하고, 응답에 대해 대단히 감사합니다. –

+0

좋아, 내가 할 수있는 한 최선을 다해 코드를 바꿨다. 조금 더 나아졌지만 지금은 'list'객체를 반환하면 'describe'속성이 없다. 아이디어 있니? –

+0

'DEF description_menu (헤더) \t 글로벌 마우스 \t \t (X, Y) = (mouse.cx, mouse.cy) \t \t \t 이름 = 경우 개체에 대한 OBJ OBJ obj.x == X 및 Y와 obj.y == libtcod.map_is_in_fov (fov_map, obj.x, obj.y)] \t \t 복귀 이름 \t 경우 렌 (이름) == 0 \t 개 \t 옵션 = [ '아무것도 여기에 없다.'] 다른 \t : \t \t \t \t 지수 = 메뉴 \t \t 옵션 = [이름에 개체에 대한 object.name (헤더, 옵션, INVENTORY_WIDTH) \t \t # 항목이 선택된 경우 \t 색인이 없음 또는 len (이름) == 0 인 경우 반환 없음 \t 반환 이름' –