2015-12-01 92 views
0

所以我正在python3中的文本冒險遊戲,而我不明白的是如何def available_actions(self):假設工作。文本冒險遊戲收到AttributeError

下面的文件是我GAMEDATA功能在哪裏辦理地點,項目,世界

class Location: 

    def __init__(self, brief_description, full_description, available_actions): 
    '''Creates a new location.   
    ADD NEW ATTRIBUTES TO THIS CLASS HERE TO STORE DATA FOR EACH LOCATION. 

    Data that could be associated with each Location object: 
    a position in the world map, 
    a brief description, 
    a long description, 
    a list of available commands/directions to move, 
    items that are available in the location, 
    and whether or not the location has been visited before. 
    Store these as you see fit. 

    This is just a suggested starter class for Location. 
    You may change/add parameters and the data available for each Location class as you see fit. 

    The only thing you must NOT change is the name of this class: Location. 
    All locations in your game MUST be represented as an instance of this class. 
    ''' 
     self.get_brief_description = brief_description 
     self.get_full_description = full_description 
     self.available_actions = available_actions 

    def get_brief_description (self): 
    '''Return str brief description of location.''' 

     return self.brief_description 

    def get_full_description (self): 
    '''Return str long description of location.''' 

     return self.full_description 

    def available_actions(self): 
    ''' 
    -- Suggested Method (You may remove/modify/rename this as you like) -- 
    Return list of the available actions in this location. 
    The list of actions should depend on the items available in the location 
    and the x,y position of this location on the world map.''' 

     return self.available_actions 

這是下面稱爲adventure.py在那裏我猜想在遊戲程序中工作的第二個文件使其工作。

import gamedata 


if __name__ == "__main__": 
    WORLD = World("map.txt", "locations.txt", "items.txt") 
    PLAYER = Player(0,0) # set starting location of player; you may change the x, y coordinates here as appropriate 

    menu = ["look", "inventory", "score", "quit", "back"] 

    while not PLAYER.victory: 
     location = WORLD.get_location(PLAYER.x, PLAYER.y) 

    # ENTER CODE HERE TO PRINT LOCATION DESCRIPTION 
    # depending on whether or not it's been visited before, 
    # print either full description (first time visit) or brief description (every subsequent visit) 

     print("What to do? \n") 
     print("[menu]") 
     for action in location.available_actions(): 
      print(action) 
     choice = input("\nEnter action: ") 

     if (choice == "[menu]"): 
      print("Menu Options: \n") 
      for option in menu: 
       print(option) 
      choice = input("\nChoose action: ") 

當我運行該文件冒險,我得到在location.available_actions()說

採取行動的錯誤:

AttributeError的: 'NoneType' 對象有沒有屬性 'available_actions'

這個錯誤會在此行

for action in location.available_actions(): 

是因爲我得到這個錯誤的原因

def available_actions(self): 
    ''' 
    -- Suggested Method (You may remove/modify/rename this as you like) -- 
    Return list of the available actions in this location. 
    The list of actions should depend on the items available in the location 
    and the x,y position of this location on the world map.''' 

     return self.available_actions 

在gamedata函數中。我不確定如何去做這部分代碼,以及如何解決這個錯誤

+0

錯誤出現在「World」類中。 'WORLD.get_location(PLAYER.x,PLA​​YER.y)'不會(總是)返回一個'Location'。它(有時)返回'None'。 – user38034

+0

@ user38034當我運行它時,我在location.available_actions()中得到了動作: AttributeError:'NoneType'對象沒有屬性'available_actions' – JerryMichaels

回答

2

的問題應該是在該行

location = WORLD.get_location(PLAYER.x, PLAYER.y) 

,因爲該行的變量「位置」被分配和錯誤告訴你,這是一個「NoneType」對象。在WORLD的中搜索問題並確保您返回正確的內容。

0

看起來你的World類應該是創建Location對象,它接收可用選項菜單。您的位置構造函數應該指定「available_actions」不爲空,因爲這會在代碼中稍後導致錯誤。而且我會想象在這種遊戲中,如果你曾經在一個沒有可用動作的地方,那麼遊戲就會停止。

0

你available_action方法定義是你如何在adventure.py

使用不同的你不通過自成每一個方法,它是通過實例變量暗示。傳遞self將爲該方法添加一個變量,如果未在代碼中尋址,則會由於簽名不匹配而導致上述錯誤。只需從Location.py中的每種方法中刪除自己即可。Error

相關問題