-
Notifications
You must be signed in to change notification settings - Fork 2
/
adventure-6.2.py
333 lines (257 loc) · 7.77 KB
/
adventure-6.2.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
Text-based adventure game
https://alissa-huskey.github.io/python-class/exercises/adventure.html
"""
import re
import textwrap
from console import fg, bg, fx
WIDTH = 45
MARGIN = 2
DEBUG = True
PLAYER = {
"place": "home",
}
PLACES = {
"home": {
"key": "home",
"name": "Your Cottage",
"east": "town-square",
"description": "A cozy stone cottage with a desk and a neatly made bed.",
"items": ["desk", "book"],
},
"town-square": {
"key": "town-square",
"name": "The Town Square",
"west": "home",
"description": (
"A large open space surrounded by buildings with a burbling "
"fountain in the center."
),
},
}
ITEMS = {
"elixir": {
"key": "elixir",
"name": "healing elixir",
"description": "a magical elixir that will heal what ails ya",
"price": -10,
},
"dagger": {
"key": "dagger",
"name": "a dagger",
"description": "a 14 inch dagger with a double-edged blade",
"price": -25,
},
"desk": {
"key": "desk",
"name": "a desk",
"description": (
"A wooden desk with a large leather-bound book open on "
"its surface."
),
},
"book": {
"key": "book",
"can_take": True,
"name": "a book",
"description": (
"A hefty leather-bound tome open to an interesting passage."
),
},
}
def header(title):
"""Print a header"""
print()
write(fx.bold(title))
print()
def wrap(text):
"""Print wrapped and indented text."""
margin = MARGIN * " "
# wrap the text
paragraph = textwrap.fill(
text,
WIDTH,
initial_indent=margin,
subsequent_indent=margin,
)
# print the wrapped text
print(paragraph)
def write(text):
"""Print an indented line of game text."""
print(MARGIN * " ", text, sep="")
def error(message):
"""Print an error message."""
print(f"{fg.red('! Error')} {message}\n")
def debug(message):
"""Print a debug message if in debug mode."""
if not DEBUG:
return
print(fg.lightblack(f"# {message}"))
def do_shop():
"""List the items for sale."""
header("Items for sale.")
for item in ITEMS.values():
if "price" not in item:
continue
write(f'{item["name"]:<13} {item["description"]}')
print()
def do_quit():
"""Exit the game."""
write("Ok, goodbye.\n")
quit()
def do_look():
"""Look at the current place"""
debug("Trying to look around.")
# look up where the player is now
place_name = PLAYER["place"]
place = PLACES[place_name]
# print information about the current place
header(f"{place['name']}")
wrap(place["description"])
# get the items in the room
items = place.get("items", [])
if items:
# for each of the place items
# get the info from the ITEMS dictionary
# and make a list of item names
names = []
for key in items:
item = ITEMS.get(key)
names.append(item["name"])
# remove the last name from the list
last = names.pop()
# construct a sentence that looks like one of:
# x, x and y
# x and y
# y
text = ", ".join(names)
if text:
text += " and "
text += last
# print the list of items.
print()
wrap(f"You see {text}.\n")
# add a blank line
print()
# print what is in each direction from here
for direction in ("north", "east", "south", "west"):
name = place.get(direction)
if not name:
continue
destination = PLACES.get(name)
write(f"To the {direction} is {destination['name']}.")
def do_examine(args):
"""Look at an item in the current place."""
debug(f"Trying to examine: {args}")
# make sure the player said what they want to examine
if not args:
error("What do you want to examine?")
return
# look up where the player is now
place_name = PLAYER["place"]
place = PLACES[place_name]
# get the item entered by the user and make it lowercase
name = args[0].lower()
# make sure the item is in this place
if name not in place.get("items", []):
error(f"Sorry, I don't know what this is: {name!r}.")
return
# make sure the item is in the ITEMS dictionary
if name not in ITEMS:
error(f"Woops! The information about {name} seems to be missing.")
return
# get the item dictionary
item = ITEMS[name]
# print the item information
header(item["name"])
wrap(item["description"])
def do_go(args):
"""Move to a different place"""
debug(f"Trying to go: {args}")
# make sure the player included a direction
if not args:
error("Which way do you want to go?")
return
# get the direction from arguments
# and make it lowercase
direction = args[0].lower()
# make sure it's a valid direction
if direction not in ('north', 'south', 'east', 'west'):
error(f"Sorry, I don't know how to go: {direction}.")
return
# look up where the player is now
old_name = PLAYER["place"]
old_place = PLACES[old_name]
# look up what is in that direction from here
new_name = old_place.get(direction)
# print an error if there is nothing in that direction
if not new_name:
error(f"Sorry, you can't go {direction} from here.")
return
# look up the place information
new_place = PLACES.get(new_name)
# this should never happen if we write the code correctly
# but just in case there is no key in PLACES matching
# the new name, print an error
if not new_place:
error(f"Woops! The information about {new_name} seems to be missing.")
return
# move the player to the new place
PLAYER["place"] = new_name
# print information about the new place
header(f"{new_place['name']}")
wrap(new_place["description"])
def do_take(args):
"""Pick up an item and add it to inventory."""
debug(f"Trying to take: {args}")
# make sure the player typed an item
if not args:
error("What do you want to take?")
return
# get the item name from arguments
# and make it lowercase
name = args[0].lower()
# look up where the player is now
place_name = PLAYER["place"]
place = PLACES[place_name]
# make sure the item is in this place
if name not in place.get("items", []):
error(f"Sorry, I don't see a {name!r} here.")
return
# get the item information
item = ITEMS.get(name)
# make sure the item is in the ITEMS dictionary
if not item:
error(f"Woops! The information about {name!r} seems to be missing.")
return
if not item.get("can_take"):
wrap(f"You try to pick up {item['name']}, but you find you aren't able to lift it.")
return
def main():
header("Welcome!")
while True:
debug(f"You are at: {PLAYER['place']}")
reply = input(fg.cyan("> ")).strip()
args = reply.split()
if not args:
continue
command = args.pop(0)
debug(f"Command: {command!r}, args: {args!r}")
if command in ["q", "quit", "exit"]:
do_quit()
elif command in ("shop"):
do_shop()
elif command in ("g", "go"):
do_go(args)
elif command in ("x", "exam", "examine"):
do_examine(args)
elif command in ("l", "look"):
do_look()
elif command in ("t", "take", "grab"):
do_take(args)
else:
error("No such command.")
continue
print()
if __name__ == "__main__":
main()