所以我正在使用python3中的文本冒险游戏,而我不明白的是def available_actions(self):假设是如何工作的。
下面的文件是我的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: ")
当我运行文件冒险时,我收到错误消息
for 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 :(得分:2)
问题应该在
行location = WORLD.get_location(PLAYER.x, PLAYER.y)
因为在这一行中分配了变量“location”,错误告诉你它是一个'NoneType'对象。在班级get_location
的{{1}}中搜索问题,并确保返回正确的内容。
答案 1 :(得分:0)
看起来你的World类应该创建接收可用选项菜单的Location对象。您的位置构造函数应该指定" available_actions"不是空的,因为这会在代码中导致错误。而且我想在这种游戏中,如果你曾经在一个没有可用行动的地方,游戏就会停止。
答案 2 :(得分:0)
您的available_action方法定义与您在adventure.py中使用它的方式不同
你不会将self传递给每个方法,它是通过实例变量隐含的。传递self会在方法中添加一个变量,如果代码中没有解决,则会导致上述错误,因为签名不匹配。只需从Location.py
中的每个方法中删除self