-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventure_singleKey.py
176 lines (153 loc) · 4.65 KB
/
adventure_singleKey.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
172
173
174
175
176
import os
import sys
import msvcrt
import re
import cmd
import time
import pickle as pickle
from kid import *
class HandleInput:
def do_save(self):
"""Prompts for a path to save the current game."""
path=input("Enter file name to save to:")
self.kid.saveToFile(path)
def do_quit(self):
"""Quits the game."""
print("Good bye")
sys.exit(0)
def do_stepLeft(self,grab=False):
"""Moves left one place, or turns left if not facing that way."""
if not self.kid.turnLeft():
self.kid.step(grab=grab)
def do_stepLeft_and_grab(self):
return self.do_stepLeft(grab=True)
do_stepLeft_and_grab.__doc__=do_stepLeft.__doc__+" Automatically grabs onto the closest ledge ahead if falling."
def do_stepRight(self,grab=False):
"""Moves right one place, or turns left if not facing that way."""
if not self.kid.turnRight():
self.kid.step(grab=grab)
def do_stepRight_and_grab(self):
return self.do_stepRight(grab=True)
do_stepRight_and_grab.__doc__=do_stepRight.__doc__+" Automatically grabs onto the closest ledge ahead if falling."
def do_walkLeft(self):
"""Continues walking left until reaching a drop or a wall."""
self.kid.turnLeft()
self.kid.walk()
def do_walkRight(self):
"""Continues walking right until reaching a drop or a wall."""
self.kid.turnRight()
self.kid.walk()
def do_take(self):
"""Picks up the item at your position."""
self.kid.pickUpItem()
def do_leapLeft(self,grab=False):
"""Jumps left 2 positions. Useful for crossing empty space."""
self.kid.turnLeft()
self.kid.leap(grab=grab)
def do_leapLeft_and_grab(self):
return self.do_leapLeft(grab=True)
do_leapLeft_and_grab.__doc__=do_leapLeft.__doc__+" Automatically grabs onto the closest ledge ahead if just missing it."
def do_leapRight(self,grab=False):
"""Jumps right 2 positions. Useful for crossing empty space."""
self.kid.turnRight()
self.kid.leap(grab=grab)
def do_leapRight_and_grab(self):
return self.do_leapRight(grab=True)
do_leapRight_and_grab.__doc__=do_leapRight.__doc__+" Automatically grabs onto the closest ledge ahead if just missing it."
def do_kill(self):
"""Kills the guard directly ahead of you."""
self.kid.killGuard()
def do_climbUp(self):
"""Climbs up to the ledge directly ahead of you or directly above when your back is at a drop."""
self.kid.climbUp()
def do_climbDown(self):
"""Climb down behind you."""
self.kid.climbDown()
def do_where(self):
""" Reports level number, room number, floor and place."""
kid.printWhere()
def do_help(self):
"""Prints commands."""
print("Available commands:")
for key,name in self.keyCommands.items():
if key.isupper():
key="Shift + %s"%(key.lower())
func=getattr(self,'do_'+name)
help=func.__doc__
print("%s (%s): %s"%(key,name,help))
def do_sonify(self):
if not sonifier:
print("Sonification not available")
return
image = self.kid.place.room.generateImage(self.kid)
sonifier.sweepImage(image, sweepDuration=5)
keyCommands={
b'q':"quit",
b's':"save",
b'h':"walkLeft",
b'j':"stepLeft",
b'J':"stepLeft_and_grab",
b'l':"stepRight",
b'L':"stepRight_and_grab",
b';':"walkRight",
b'i':"climbUp",
b',':"climbDown",
b'y':"leapLeft",
b'Y':"leapLeft_and_grab",
b'p':"leapRight",
b'P':"leapRight_and_grab",
b'<':"take",
b'k':"kill",
b"w": "where",
b"?":"help",
b"`": "sonify",
}
def __init__(self,kid):
self.kid=kid
while True:
ch=msvcrt.getch()
funcName=self.keyCommands.get(ch)
if funcName:
func=getattr(self,'do_'+funcName)
func()
if __name__=='__main__':
print("Prince of Persia Text Adventure")
levelNumber=1
kid = None
if len(sys.argv)>1:
arg = sys.argv[1]
if os.path.isfile(arg):
kid=Kid.loadFromFile(arg)
print("Restored from %s"%arg)
else:
try:
levelNumber = int(arg)
except ValueError:
print("Bad argument: %r"%arg)
sys.exit(1)
if not kid:
kid=Kid(levelNumber)
try:
from imagePlayer import ImagePlayer
except ModuleNotFoundError:
print("Sonification not available")
sonifier = None
else:
sonifier = ImagePlayer(lowFreq=220, highFreq=7040, width=roomImageWidth, height=roomImageHeight)
while True:
print("On level %d"%kid.place.level.levelNumber)
try:
kid.touchPlace(0,0)
kid.printDirection()
kid.printHealth()
HandleInput(kid)
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.loadNextLevel()