-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventure.py
171 lines (152 loc) · 4.02 KB
/
adventure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import sys
import cmd
import re
import time
import pickle as pickle
import msvcrt
from kid import *
class Commander(cmd.Cmd):
prompt=">"
_re_numPlaces=re.compile(r"(\d+) places?")
_re_toPlace=re.compile(r"to (.+)")
_lastBatchCommand=None
def do_save(self,args):
kid.saveToFile(args)
do_save.__doc__="""save <fileName>
Saves the current state of the game to a file for later reloading.
"""
def emptyline(self):
print("What?")
def onecmd(self,line):
sublines=line.split(' then ')
if len(sublines)>1:
for subline in sublines:
self.onecmd(subline)
self._lastBatchCommand=self.parseline(subline)[0]
self._lastBatchCommand=None
else:
cmd.Cmd.onecmd(self,line)
def do_quit(self,args):
print("Good bye")
sys.exit(0)
do_quit.__doc__="Quits the game!"
def do_step(self,args):
kid.step()
do_step.__doc__="Alias for walk 1 place"
def do_take(self,args):
kid.pickUpItem()
do_take.__doc__="""Picks up the item from the floor where you are standing.
Examples:
> take sword
or
> take potion
"""
def do_exit(self,args):
kid.exit()
do_exit.__doc__="Exits the level, if you are at an opened exit."
def _do_walkRun(self,args,run=False):
walkRun=kid.run if run else kid.walk
if not args:
walkRun()
return
m=self._re_numPlaces.match(args)
if m:
walkRun(maxPlaces=int(m.groups()[0]))
return
m=self._re_toPlace.match(args)
if m:
walkRun(stopAtName=m.groups()[0])
return
print("walk where?")
def do_walk(self,args):
self._do_walkRun(args)
do_walk.__doc__="""walk [{how far|to where}]
Walks 1 or more places.
Examples:
> walk
(Walks until there is a danger)
> walk 3 places
(Walks 3 places)
> walk to sword
(Walks until at a sword)
"""
def do_run(self,args):
self._do_walkRun(args,run=True)
do_run.__doc__="""run [{how far|to where}]
Runs 1 or more places.
Similar to walk, but does not stop at dangers.
It also takes less time, and avoids falling when stepping on loose boards.
Examples:
> run
(runs continuously)
> run 3 places
(runs 3 places)
> run to sword
(runs until at a sword)
"""
def _do_leap(self,args,large=False):
large=self._lastBatchCommand=="run"
kid.leap(large=large,grab=True)
def do_leap(self,args):
self._do_leap(args)
do_leap.__doc__="""Leaps forward over the next place (E.g. a drop).
Normally only 1 place is leaped.
However, it is possible to leap 2 places with a run-up by joining it onto the end of a run command:
> run to edge then leap
Which will leap 2 places rather than 1.
If you still won't make the distance, try grabbing for the far edge:
leap and grab
or
run to edge then leap and grab
The full syntax is: [run [{how far|to where}] then ]leap [and grab]
"""
def do_turn(self,args):
kid.turn()
do_turn.__doc__="Turns to the opposite direction."
def do_kill(self,args):
if args=="guard":
kid.killGuard()
else:
print("Kill what?")
do_kill.__doc__="""Kill something.
Example:
> kill guard
An item may be required to kill something.
"""
def do_climb(self,args):
if args=="up":
kid.climbUp()
elif args=="down":
kid.climbDown()
else:
print("climb which way? [up or down]")
do_climb.__doc__="""climb {up|down}
Climb up onto a ledge, or down off a ledge.
"""
if __name__=='__main__':
print("Prince of Persia Level Walker")
levelNo=1
if len(sys.argv)>1:
loadPath=sys.argv[1]
kid=Kid.loadFromFile(loadPath)
print("Restored from %s"%loadPath)
else:
kid=Kid(levelNo)
while True:
print("On level %d"%kid.place.level.levelNumber)
kid.touchPlace(0,0)
kid.printDirection()
kid.printHealth()
commander=Commander()
try:
commander.cmdloop()
except KidDeath:
print("Press enter to restart")
input()
kid=kid.loadFromRestorePoint(kid.lastRestorePoint)
continue
except LevelExit:
print("Level complete")
print("Press enter to proceed to next level")
input()
kid.loadLevel(kid.place.level.nextLevelPath)