我错过了什么。我已经组装了几个模块,我尝试调试它,但我仍然无法找到它无效的原因。 有一个模块 start.py ,可以快速启动整个游戏 - 没有任何错误,在你创建角色之后它会加载一个模块 game.py 来控制玩家的位置和还列出了游戏中可用的所有位置。位置在各个模块中分开,在这种情况下,到目前为止我只有两个: apartment.py 和 curling_street.py 。
我的模块 apartment.py 工作得很好但是当它返回字符串&#39; curling street&#39; 时应加载 curling_street.py < / strong>它写出了这个错误:
Traceback (most recent call last):
File "start.py", line 56, in <module>
splash_screen()
File "start.py", line 39, in splash_screen
a_game.move()
File "/Volumes/DATA HD/Dropbox/Python/ex45/game.py", line 18, in move
get_next_map = current_map.enter()
AttributeError: 'NoneType' object has no attribute 'enter'
我认为我的引擎类编写不正确。这是模块。
import sys
import custom_error
import player
import handler
import prompt
import game
def splash_screen():
print chr(27) + "[2J"
print "*" * 80
print "***** Welcome to ZOMBIE ADVENTURE *****"
print "*" * 80
print "\nSelect option:"
print "1. Start a new game"
print "2. Load existing game"
print "3. Quit"
while True:
action = prompt.menu()
if action == 1:
create_player = player.CreateNewPlayer()
new_player = player.Player(create_player.name, create_player.age, create_player.male, create_player.inventory)
print "\nYour name is %s and you're %d old." % (new_player.name, new_player.age)
print "It is %s that you're a man." % new_player.male
print "\n1. Continue to game"
print "2. Back to main menu"
action = prompt.menu()
while True:
if action == 1:
game.Engine.launchgame()
elif action == 2:
exit(1)
else:
custom_error.error(1)
# a_game = game.Engine('Room1')
# a_game.LaunchGame(new_player)
elif action == 2:
handler.load()
elif action == 3:
exit(1)
else:
custom_error.errortype(0)
splash_screen()
import apartment
import curling_street
class Engine(object):
def __init__(self, the_player, start_map):
self.the_player = the_player
self.start_map = start_map
def move(self):
current_map = Maps.map_dict.get(self.start_map)
print "Now entering %s." % current_map
raw_input(">")
while True:
get_next_map = current_map.enter()
current_map = Maps.map_dict.get(get_next_map)
class Maps(object):
map_dict = {
'apartment': apartment,
'curling street': curling_street
}
def visited(self):
visited_maps = map_dict.keys()
def enter():
print "Apartment location"
raw_input("now going to curling street")
return 'curling street'
def enter():
print "Curling Street location"
答案 0 :(得分:2)
您使用
current_map = Maps.map_dict.get(get_next_map)
从字典中访问地图。但是,如果字典中没有键get_next_map
,dict.get()
将会return None
。目前,curling_street.enter
将return None
current_map is None
,这不是字典中的关键字,因此while True:
get_next_map = current_map.enter()
current_map = Maps.map_dict.get(get_next_map)
if current_map is None:
break
会出现错误。您应该检查这个最终游戏状态:
{{1}}
答案 1 :(得分:1)
你确定它是在进入“冰壶街”时而不是在那之后吗?你的curling_street enter
函数没有返回任何内容,所以在循环的下一次迭代中,它将使用None
键访问dict,这可能会返回一个None
对象,没有enter
成员。看起来你需要:
curling_street.enter