它根据您传递给它的关键字从字典中实例化一个场景对象scenes
,并将其返回给您。
为什么会切换到下一个场景?
它切换到下一个场景的原因是因为在基类中,每个场景都扩展了在完成运行enter()
功能后指定序列中的下一个场景:
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
while True:
print "\n--------"
next_scene_name = current_scene.enter() # returns next scene key
current_scene = self.scene_map.next_scene(next_scene_name) # initiates next scene and sets it as `current_scene`
例如最后的CentralCorridor
场景取决于输入的动作返回下一个场景的键:
def enter(self):
print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
...
print "flowing around his hate filled body. He's blocking the door to the"
print "Armory and about to pull a weapon to blast you."
action = raw_input("> ")
if action == "shoot!":
print "Quick on the draw you yank out your blaster and fire it at the Gothon."
...
print "you are dead. Then he eats you."
return 'death' # next scene `Death`
elif action == "dodge!":
print "Like a world class boxer you dodge, weave, slip and slide right"
...
print "your head and eats you."
return 'death' # next scene `Death`
elif action == "tell a joke":
print "Lucky for you they made you learn Gothon insults in the academy."
...
return 'laser_weapon_armory' # next scene `LaserWeaponArmory`
else:
print "DOES NOT COMPUTE!"
return 'central_corridor' # next scene `CentralCorridor`
整个序列以enter()
退出程序的死亡场景函数结束:
def enter(self):
print Death.quips[randint(0, len(self.quips)-1)]
exit(1)