3、创建一个新版本,里边使用两个 class,其中一个是 Map ,另一个是 Engine 。提示: 把 play 放到 Engine 里面。、
#coding=utf-8
from sys import exit
from random import randint
class Map(object):
def __init__(self):
self.quips = ["You died. You kinda suck at this.","Your mom would be proud. If she were smarter.","Such a luser.","I hava a small puppy that‘s better at this."]
def death(self):
print self.quips[randint(0,len(self.quips)-1)]
exit(1)
def central_corridor(self):
action = raw_input(">>> ")
if action == "shoot!":
return ‘death‘
elif action == "dodge!":
return ‘death‘
elif action == "tell a joke":
return ‘laser_weapon_armory‘
else:
return ‘central_corridor‘
def laser_weapon_armory(self):
code = "%d%d%d"% (randint(1,9)),(randint(1,9)),(randint(1,9))
guess = raw_input("[keypad]>>> ")
guesses = 0
while guess !=code and guesses < 10:
print "BZZZZEDDD!"
guesses += 1
guesses = raw_input("[keypad]>>> ")
if guess == code:
return ‘the_bridge‘
else:
return ‘death‘
def the_bridge(self):
action = raw_input(">>> ")
if action == "throw the bomb":
return ‘death‘
elif action == "slowly place the bomb":
return ‘escape_pod‘
else:
return ‘the_bridge‘
def escape_pod(self):
good_pod = randint(1,5)
guess = raw_input("[pod #]>>> ")
if int(guess) != good_pod:
return ‘death‘
else:
exit(0)
class Engine(Map):
def __init__(self,start):
self.start = start
def play(self):
next = self.start
while True:
print "\n-----------"
room = getattr(self,next)
next = room()
a_game = Engine("central_corridor")
a_game.play()
备注:初学小菜鸟,欢迎各位大神指教
原文地址:https://www.cnblogs.com/jion/p/8117027.html