这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!
原文例题
# -- coding: utf-8 --
from sys import exit
from random import randint
class Game(object):
# 类内初始化,其中新建初始值quips和start
def __init__(self, start):
self.quips = [
'You died.',
'Your mom would...',
'Such a luser',
'I have ...'
]
self.start = start
def play(self):
next = self.start
while True:
print "\n---------------------"
# 获取对象object的属性或者方法,如果存在打印出来(打印出来的是地址),如果不存在,打印出默认值,默认值可选。
room = getattr(self, next)
#指向这个地址的程序
next = room()
def death(self):
# 随机quips列表中的元素
print self.quips[randint(0, len(self.quips)-1)]
# 退出并关闭这段内存
exit(1)
def central_corridor(self):
print "The Gothons of..."
action = raw_input("> ")
if action == "shoot!":
print "Quick on the ..."
return 'death'
elif action == "dodge!":
print "Like a world ..."
return 'death'
elif action == "te;; a joke":
print "Lucky for you..."
return 'laser_weapon_armory'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
def laser_weapon_armory(self):
print "You do a dive..."
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 "BZZZZEDDDD!"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "The container..."
return 'the_bridge'
else:
print "The lock..."
return 'death'
def the_bridge(self):
print "You burst onto..."
action = raw_input("> ")
if action == "throw the bomb":
print "In a panic..."
return 'death'
elif action == "slowly place the bomb":
print "You point your..."
return 'escape_pod'
else:
print "DOES NOT COMPUTE!"
return "the_bridge"
def escape_pod(self):
print "You rush through..."
good_pod = randint(1,5)
guess = raw_input("[pod #]> ")
if int(guess) != good_pod:
print "You jump into..."
return 'death'
else:
print "You jump into pod %s and hit the eject button." % guess
print "The pod..."
exit(0)
# 构造函数,调用_init_函数
a_game = Game("central_corridor")
# 通俗的讲,上一句声明了Game a_game("central_corridor")(别纠结语法,)我就图个好了解
# 调用play的类内函数
a_game.play()
以上附上个人注释,如有不当,望指出。
加分习题
2、
# -- coding: utf-8 --
from sys import exit
from random import randint
class Game(object):
# 类内初始化,其中新建初始值quips和start
def __init__(self, start):
self.quips = [
'You died.',
'Your mom would...',
'Such a luser',
'I have ...'
]
self.start = start
def play(self):
next = self.start
while True:
print "\n---------------------"
# 获取对象object的属性或者方法,如果存在打印出来(打印出来的是地址),如果不存在,打印出默认值,默认值可选。
room = getattr(self, next)
#指向这个地址的程序
next = room()
def death(self):
# 随机quips列表中的元素
print self.quips[randint(0, len(self.quips)-1)]
# 退出并关闭这段内存
exit(1)
def central_corridor(self):
print "The Gothons of..."
action = raw_input("> ")
if action == "shoot!":
print "Quick on the ..."
return 'death'
elif action == "dodge!":
print "Like a world ..."
return 'death'
elif action == "take a joke":
print "Lucky for you..."
return 'laser_weapon_armory'
# 添加内容
elif action == "riddle":
print "Let's go!"
return 'riddles'
# *********************
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
def laser_weapon_armory(self):
print "You do a dive..."
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 "BZZZZEDDDD!"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "The container..."
return 'the_bridge'
else:
print "The lock..."
return 'death'
def the_bridge(self):
print "You burst onto..."
action = raw_input("> ")
if action == "throw the bomb":
print "In a panic..."
return 'death'
elif action == "slowly place the bomb":
print "You point your..."
return 'escape_pod'
else:
print "DOES NOT COMPUTE!"
return "the_bridge"
def escape_pod(self):
print "You rush through..."
good_pod = randint(1,5)
guess = raw_input("[pod #]> ")
if int(guess) != good_pod:
print "You jump into..."
return 'death'
else:
print "You jump into pod %s and hit the eject button." % guess
print "The pod..."
exit(0)
# 添加内容
def riddles(self):
print "What will you break once you say it?"
answer = raw_input(" ")
if answer == "Silence":
print "Lucky for you!"
return 'succeed'
else:
print "Gone"
return 'death'
def succeed(self):
print "You are lucky boy."
print '>'*10
print "Succeed"
exit(0)
# 构造函数,调用_init_函数
a_game = Game("central_corridor")
# 通俗的讲,上一句声明了Game a_game("central_corridor")(别纠结语法,我就图个好了解)
# 调用play的类内函数
a_game.play()
添加了两个类内函数succeed(self)和riddles(self)。
3、
# -- coding: utf-8 --
from sys import exit
from random import randint
class Map(object):
def __init__(self):
self.quips = [
"You died. You kiinda suck at this.",
"Nice job, you died ...jackass.",
"Such a luser.",
"I hace 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):
print "The Gothons of..."
action = raw_input("> ")
if action == "shoot!":
print "Quick on the ..."
return 'death'
elif action == "dodge!":
print "Like a world ..."
return 'death'
elif action == "take a joke":
print "Lucky for you..."
return 'laser_weapon_armory'
elif action == "riddle":
print "Let's go!"
return 'riddles'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
def laser_weapon_armory(self):
print "You do a dive..."
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 "BZZZZEDDDD!"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "The container..."
return 'the_bridge'
else:
print "The lock..."
return 'death'
def the_bridge(self):
print "You burst onto..."
action = raw_input("> ")
if action == "throw the bomb":
print "In a panic..."
return 'death'
elif action == "slowly place the bomb":
print "You point your..."
return 'escape_pod'
else:
print "DOES NOT COMPUTE!"
return "the_bridge"
def escape_pod(self):
print "You rush through..."
good_pod = randint(1,5)
guess = raw_input("[pod #]> ")
if int(guess) != good_pod:
print "You jump into..."
return 'death'
else:
print "You jump into pod %s and hit the eject button." % guess
print "The pod..."
exit(0)
def riddles(self):
print "What will you break once you say it?"
answer = raw_input(" ")
if answer == "Silence":
print "Lucky for you!"
return 'succeed'
else:
print "Gone"
return 'death'
def succeed(self):
print "You are lucky boy."
print '>'*10
print "Succeed"
exit(0)
class Engine(object):
def __init__(self, game):
self.game = game
def play(self):
next = self.game
while True:
print "\n---------------------"
room = getattr(Map(), next)
next = room()
a_game = Engine("central_corridor")
a_game.play()
好长啊,不过很多是重复的。