错误:第29行,在地图'empty_room'中: 我曾尝试重写我的课程和其他小东西,但却无法提出任何解决方案。我的缩进在记事本上是正确的(或似乎是这样),他们只是没有很好地转移到SOF。任何故障排除表示赞赏:)谢谢!
P.S。我正在学习“学习Python艰难之路”这本书,我正在做一个类似于zork的游戏练习。希望这会有所帮助。
from sys import exit
class Scene(object):
def enter(self):
print "This scene is not configured"
exit(1)
class Engine(object):
##calling Engine(x) x is the mapScene
def __init__(self, mapScene):
self.mapScene = mapScene
def play(self):
currentScene = self.mapScene.openingScene()
while True:
print "\n-------------------"
nextSceneName = currentScene.enter()
currentScene = self.mapScene.nextScene(nextSceneName)
class Map(object):
scenes = {
'empty_room': EmptyRoom(),
'living_room': LivingRoom(),
'office': Office(),
'hallway': Hallway(),
'kitchen': Kitchen(),
'master_bedroom': MasterBedroom(),
'kids_bedroom': KidsBedroom(),
'attic': Attic()
}
##when calling Map(x) x is the startscene
def __init__(self, startScene):
self.startScene = startScene
##When calling nextScene(x) x is the sceneName
def nextScene(self, sceneName):
return Map.scenes.get(sceneName)
##????
def openingScene(self):
return self.nextScene(self.startScene)
class EmptyRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "open door":
return 'living_room'
class LivingRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "kitchen":
return 'kitchen'
elif action == "stairs" or "go upstairs":
print "The steps creek as you ascend to the unknown..."
print "Are you sure you want to go up here?"
action = raw_input("> ")
if action == "yes" or "kinda":
return 'hallway'
else:
return 'living_room'
elif action == "empty room":
return 'empty_room'
else:
return 'living_room'
class Kitchen(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "office" or "go right":
return 'office'
elif action == "living room":
return 'living_room'
else:
return 'kitchen'
class Office(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "kitchen":
return 'kitchen'
class MasterBedroom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "hallway":
return 'hallway'
class KidsBedroom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "hallway":
return 'hallway'
class Hallway(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "downstairs" or "stairs" or "living room":
return 'living_room'
elif action == "bedroom" or "master bedroom" or "left":
return 'master_bedroom'
elif action == "kids room" or "kids bedroom" or "right":
return 'kids_bedroom'
elif action == "pull string":
print"You have just opened the attic staircase would you like to go up?"
action = raw_input("> ")
if action == "yes":
return 'attic'
elif action == "no" or "nope":
return 'hallway'
else:
print "I wouldn't have went either\n"
print "SMASH, the attic door springs shut\n"
return 'hallway'
else:
return 'hallway'
class Attic(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "downstairs" or "hallway":
return 'hallway'
aMap = Map('empty_room')
aGame = Engine(aMap)
aGame.play()
答案 0 :(得分:1)
在使用类定义之前,它应该首先出现。
class EmptyRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "open door":
return 'living_room'
class Map(object):
scenes = {
'empty_room': EmptyRoom(),
...
}
LivingRoom
,Office
,Hallway
,Kitchen
,MasterBedroom
,KidsBedroom
,Attic
相同。
答案 1 :(得分:0)
scenes
内Map
的定义是在构建类时运行的,而不是在您稍后调用类来实例化它时。那时,EmptyRoom
还不存在 - Python从代码的顶部开始工作,因此EmptyRoom
仅在到达class EmptyRoom:
下面的缩进块的末尾后才存在。因此,为了实现这一点,Map
需要在所有其他类之后而不是之前。