Skip to content

Commit 704962b

Browse files
committed
add ex42
使用 class把习题 41 的内容重写
1 parent 55a8bf2 commit 704962b

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

lihoufeng/ex42_rewrite_41.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
from sys import exit
2+
from random import randint
3+
4+
class Game(object):
5+
6+
def __init__(self, start):
7+
self.quips = [
8+
"You died. You kinda suck at this.",
9+
"Your mom would be proud. If she were smarter.",
10+
"Such a luser.",
11+
"I have a small puppy that's better at this."
12+
]
13+
self.start = start
14+
15+
def play(self):
16+
next = self.start
17+
18+
while True:
19+
print "\n---------"
20+
room = getattr(self, next)
21+
mext = room()
22+
23+
24+
def death(self):
25+
print self.quips[randint(0, len(self.quips)-1)]
26+
exit(1)
27+
28+
def central_corridor(self):
29+
print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
30+
print "your entire crew. You are the last surviving member and your last"
31+
print "mission is to get the neutron destruct bomb from the Weapons Armory,"
32+
print "put it in the bridge, and blow the ship up after getting into an "
33+
print "escape pod."
34+
print "\n"
35+
print "You're running down the central corridor to the Weapons Armory when"
36+
print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume"
37+
print "flowing around his hate filled body. He's blocking the door to the"
38+
print "Armory and about to pull a weapon to blast you."
39+
40+
action = raw_input("> ")
41+
42+
if action == "shoot!":
43+
print "Quick on the draw you yank out your blaster and fire it at the Gothon."
44+
print "His clown costume is flowing and moving around his body, which throws"
45+
print "off your aim. Your laser hits his costume but misses him entirely. This"
46+
print "completely ruins his brand new costume his mother bought him, which"
47+
print "makes him fly into an insane rage and blast you repeatedly in the face until"
48+
print "you are dead. Then he eats you."
49+
return 'death'
50+
51+
elif action == "dodge!":
52+
print "Like a world class boxer you dodge, weave, slip and slide right"
53+
print "as the Gothon's blaster cranks a laser past your head."
54+
print "In the middie of your artful dodge your foot slips and you"
55+
print "bang your head on the metal wall and pass out."
56+
print "You wake up shortly after only to die as the Gothon stomps on"
57+
print "your head and eats you."
58+
return 'death'
59+
60+
elif action == "tell a joke":
61+
print "Lucky for you they made you learn Gothon insults in the academy."
62+
print "You tell the one Gothon joke you know:"
63+
print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
64+
print "The Gothon stops, tries not to laugh, then busts out laughing and can't move."
65+
print "While he's laughing you run up and shoot him square in the head"
66+
print "putting him down, then jump through the Weapon Armory door."
67+
return 'laser_weapon_armory'
68+
69+
else:
70+
print "DOES NOT COMPUTE!"
71+
return 'central_corridor'
72+
73+
def laser_weapon_armory(self):
74+
print "You do a dive roll into the Weapon Armory, crouch and scan the room"
75+
print "for more Gothons that might be hiding. It's dead quiet, too quiet."
76+
print "You stand up and run to the far side of the room and find the"
77+
print "neutron bomb in its container. There's a keypad lock on the box"
78+
print "and you need the code to get the bomb out. If you get the code"
79+
print "wrong 10 times then the lock closes forever and you can't"
80+
print "get the bomb. The code is 3 digits."
81+
code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
82+
guess = raw_input("[keypad]> ")
83+
guesses = 0
84+
85+
while guess != code and guesses < 10:
86+
print "BZZZZEDDD!"
87+
guesses += 1
88+
guess = raw_input("[keypad]> ")
89+
90+
if guess == code:
91+
print "The container clicks open and the seal breaks, letting gas out."
92+
print "You grab the neutron bomb and run as fast as you can to the"
93+
print "bridge where you must place it in the right spot."
94+
return 'the_bridge'
95+
else:
96+
print "The lock buzzes one last time and then you hear a sickening"
97+
print "melting sound as the mechanism is fused togehter."
98+
print "You decide to sit there, and finally the Gothons blow up the"
99+
print "ship from their ship and you die."
100+
return 'death'
101+
102+
103+
def the_bridge(self):
104+
print "You burst onto the Bridge with the neutron destruct bomb"
105+
print "under your arm and surprise 5 Gothons who are trying to"
106+
print "take control of the ship. Each of them has an even uglier"
107+
print "clown costume than the last. They haven't pulled their"
108+
print "weapons out yet, as they see the active bomb under your"
109+
print "arm and don't want to set it off."
110+
111+
action == raw_inut("> ")
112+
113+
if action == "throw the bomb":
114+
print "In a panic you throw the bomb at the group of Gothons"
115+
print "and make a leap for the door. Right as you drop it a"
116+
print "Gothon shoots you right in the back killing you."
117+
print "As you die you see another Gothon frantically try to disarm"
118+
print "the bomb. You die knowing they will probably blow up when"
119+
print "it goes off."
120+
return 'death'
121+
122+
elif action == "slowly place the bomb":
123+
print "You point your blaster at the bomb under your arm"
124+
print "and the Gothons put their hands up and start to sweat."
125+
print "You inch backward to the door, open it, and then carefully"
126+
print "place the bomb on the floor, pointing your blaster at it."
127+
print "You then jump back through the door, punch the close button"
128+
print "and blast the lock so the Gothons can't get out."
129+
print "Now that the bomb is placed you run to the escape pod to"
130+
print "get off this tin can."
131+
return 'escape_pod'
132+
else:
133+
print "DOES NOT COMPUTE!"
134+
return "the_bridge"
135+
136+
def escape_pod(self):
137+
print "You rush through the ship desperately trying to make it to"
138+
print "the escape pod before the whole ship explodes. It seems like"
139+
print "hardly any Gothons are on the ship, so your run is clear of"
140+
print "interference. You get to the chamber with the escape pods, and"
141+
print "now need to pick one to take. Some of them could be damaged"
142+
print "but you don't have time to look. There's 5 pods, which one"
143+
print "do you take?"
144+
145+
good_pod = randint(1, 5)
146+
147+
guess = raw_input("[pod #]> ")
148+
149+
if int(guess) != good_pod:
150+
print "You jump into pod %s and hit the eject button." % guess
151+
print "The pod escapes out into the void of space, then"
152+
print "implodes as the hull ruptures, crushing your body"
153+
print "into jam jelly."
154+
return 'death'
155+
else:
156+
print "You jump into pod %s and hit the eject button." % guess
157+
print "The pod easily slides out into space heading to"
158+
print "the planet below. As it flies to the planet, you look"
159+
print "back and see your ship implode then explode like a"
160+
print "bright star, taking out the Gothon ship at the same"
161+
print "time. You won!"
162+
exit(0)
163+
164+
165+
a_game = Game("central_corridor")
166+
a_game.play()

0 commit comments

Comments
 (0)