-
Notifications
You must be signed in to change notification settings - Fork 0
/
textadventure.py
591 lines (480 loc) · 21.2 KB
/
textadventure.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#! python3
import cmd
import textwrap
import time
DESC = 'desc'
NORTH = 'north'
SOUTH = 'south'
EAST = 'east'
WEST = 'west'
UP = 'up'
DOWN = 'down'
GROUND = 'ground'
SHOP = 'shop'
GROUNDDESC = 'grounddesc'
SHORTDESC = 'shortdesc'
LONGDESC = 'longdesc'
TAKEABLE = 'takeable'
TALKABLE = 'talkable'
EDIBLE = 'edible'
DESCWORDS = 'descwords'
SCREEN_WIDTH = 100
# Available Rooms
worldRooms = {
'Homebase': {
DESC: 'Nothing is here. Go out and solve some riddles and mysteries!',
NORTH: 'Backyard',
GROUND: []},
'Backyard': {
DESC: 'It\'s really dirty here!',
SOUTH: 'Homebase',
GROUND: ['Piece of Paper', 'Stone', 'Pelto Pekka']},
'Central Plaza': {
DESC: 'This is the wonderful Central Plaza of Burningham (German: "brennender Schinken")',
EAST: 'Townhall',
GROUND: []},
'Townhall': {
DESC: 'You know what, Burningham got a townhall',
GROUND: ['Greyham']},
}
worldItems = { #and persons
'Piece of Paper': {
GROUNDDESC: 'A piece of paper lays on the ground',
SHORTDESC: 'piece of paper',
LONGDESC: "The piece of paper says: \"Solve the first task to prove you are strong enough to enter Burningham!"
"\"",
TAKEABLE: True,
TALKABLE: False,
DESCWORDS: ['piece', 'paper', 'pieceofpaper']},
'Stone': {
GROUNDDESC: 'There is a small round stone on the floor',
SHORTDESC: 'small stone',
LONGDESC: 'You can read "0x67 0x6F"',
TAKEABLE: True,
TALKABLE: False,
DESCWORDS: ['stone'],
},
'Pelto Pekka': {
GROUNDDESC: 'Pelto Pekka alway hangs around in your backyard, nobody knows why',
SHORTDESC: 'pelto pekka',
LONGDESC: 'Hello friend! If I were you I would ask myself for advice and than I would tell you that if I where '
'you I would always say what I think!',
TAKEABLE: False,
TALKABLE: True,
DESCWORDS: ['pelto', 'pekka', 'pelto pekka'],
},
'Greyham': {
GROUNDDESC: 'Greyham is the mayor of Burningham and he is always in the townhall',
SHORTDESC: 'greyham',
LONGDESC: 'Welcome to Burningham newcomer! I hope you feel comfortable in our city and always remember to take'
'a bowl of tuna salad with you!',
TAKEABLE: False,
TALKABLE: True,
DESCWORDS: ['greyham', 'grey'],
},
}
location = 'Homebase' # start in homebase
inventory = [] # start with blank inventory
showFullExits = True
def displayLocation(loc):
"""A helper function for displaying an area's description and exits."""
# Print the room name.
print(loc)
print('=' * len(loc))
# Print the room's description (using textwrap.wrap())
print('\n'.join(textwrap.wrap(worldRooms[loc][DESC], SCREEN_WIDTH)))
# Print all the items on the ground.
if len(worldRooms[loc][GROUND]) > 0:
for item in worldRooms[loc][GROUND]:
print(worldItems[item][GROUNDDESC])
# Print all the exits.
exits = []
for direction in (NORTH, SOUTH, EAST, WEST, UP, DOWN):
if direction in worldRooms[loc].keys():
exits.append(direction.title())
print()
if showFullExits:
for direction in (NORTH, SOUTH, EAST, WEST, UP, DOWN):
if direction in worldRooms[location]:
print('%s: %s' % (direction.title(), worldRooms[location][direction]))
else:
print('Exits: %s' % ' '.join(exits))
def moveDirection(direction):
"""A helper function that changes the location of the player."""
global location
if direction in worldRooms[location]:
print('You move to the %s.' % direction)
location = worldRooms[location][direction]
displayLocation(location)
else:
print('You cannot move in that direction')
def getAllDescWords(itemList):
"""Returns a list of "description words" for each item named in itemList."""
itemList = list(set(itemList)) # make itemList unique
descWords = []
for item in itemList:
descWords.extend(worldItems[item][DESCWORDS])
return list(set(descWords))
def getAllFirstDescWords(itemList):
"""Returns a list of the first "description word" in the list of
description words for each item named in itemList."""
itemList = list(set(itemList)) # make itemList unique
descWords = []
for item in itemList:
descWords.append(worldItems[item][DESCWORDS][0])
return list(set(descWords))
def getFirstItemMatchingDesc(desc, itemList):
itemList = list(set(itemList)) # make itemList unique
for item in itemList:
if desc in worldItems[item][DESCWORDS]:
return item
return None
def getAllItemsMatchingDesc(desc, itemList):
itemList = list(set(itemList)) # make itemList unique
matchingItems = []
for item in itemList:
if desc in worldItems[item][DESCWORDS]:
matchingItems.append(item)
return matchingItems
def checkForStone():
for item in inventory:
if item == 'Stone':
enterBurningham()
def enterBurningham():
time.sleep(2)
for i in range(0, 20):
print("***\n")
print("white flashlight")
time.sleep(0.1)
print("Wubba lubba dub dub ! \n")
global location
location = 'Central Plaza'
displayLocation(location)
class TextAdventureCmd(cmd.Cmd):
prompt = '\n> '
# The default() method is called when none of the other do_*() command methods match.
def default(self, arg):
print('I do not understand that command. Type "help" for a list of commands.')
# A very simple "quit" command to terminate the program:
def do_quit(self, arg):
"""Quit the game."""
return True # this exits the Cmd application loop in TextAdventureCmd.cmdloop()
def help_combat(self):
print('Combat is not implemented in this program.')
# These direction commands have a long (i.e. north) and show (i.e. n) form.
# Since the code is basically the same, I put it in the moveDirection()
# function.
def do_north(self, arg):
"""Go to the area to the north, if possible."""
moveDirection('north')
def do_south(self, arg):
"""Go to the area to the south, if possible."""
moveDirection('south')
def do_east(self, arg):
"""Go to the area to the east, if possible."""
moveDirection('east')
def do_west(self, arg):
"""Go to the area to the west, if possible."""
moveDirection('west')
def do_up(self, arg):
"""Go to the area upwards, if possible."""
moveDirection('up')
def do_down(self, arg):
"""Go to the area downwards, if possible."""
moveDirection('down')
def do_go(self, arg):
"""You say go"""
print('You said "go" with a mysterious voice')
checkForStone()
def do_help(self, arg):
print("Documented commands:")
print("====================")
print("North: n\nSouth: s\nWest: w\nEast: e")
print("Look around: look")
print("Look at item: look <item>")
print("Inventory: inv")
print("Take item: take <item>\nDrop item: drop <item>")
print("Eat item: eat <item>")
print("Exit game: quit")
def do_ascii(self, arg):
print(" | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |")
print("-------------------------------------")
print(" 0 |NUL|DLE|SPC| 0 | @ | P | ` | p |")
print(" 1 |SOH|DC1| ! | 1 | A | Q | a | q |")
print(" 2 |STX|DC2| \" | 2 | B | R | b | r |")
print(" 3 |ETX|DC3| # | 3 | C | S | c | s |")
print(" 4 |EQT|DC4| $ | 4 | D | T | d | t |")
print(" 5 |ENQ|NAK| % | 5 | E | U | e | u |")
print(" 6 |ACK|SQN| & | 6 | F | V | f | v |")
print(" 7 |BEL|BTB| ' | 7 | G | W | g | w |")
print(" 8 |BS |CAN| ( | 8 | H | X | h | x |")
print(" 9 |HT |EM | ) | 9 | I | Y | i | y |")
print(" A |LF |SUB| * | : | J | Z | j | z |")
print(" B |VT |ESC| + | ; | K | [ | k | { |")
print(" C |FF |FS | , | < | L | \\ | l | | |")
print(" D |CR |GS | - | = | M | ] | m | } |")
print(" E |SO |RS | . | > | N | ^ | n | ~ |")
print(" F |SI |US | / | ? | O | _ | o |del|")
# Since the code is the exact same, we can just copy the
# methods with shortened names:
do_n = do_north
do_s = do_south
do_e = do_east
do_w = do_west
do_u = do_up
do_d = do_down
def do_exits(self, arg):
"""Toggle showing full exit descriptions or brief exit descriptions."""
global showFullExits
showFullExits = not showFullExits
if showFullExits:
print('Showing full exit descriptions.')
else:
print('Showing brief exit descriptions.')
def do_inventory(self, arg):
"""Display a list of the items in your possession."""
if len(inventory) == 0:
print('Inventory:\n (nothing)')
return
# first get a count of each distinct item in the inventory
itemCount = {}
for item in inventory:
if item in itemCount.keys():
itemCount[item] += 1
else:
itemCount[item] = 1
# get a list of inventory items with duplicates removed:
print('Inventory:')
for item in set(inventory):
if itemCount[item] > 1:
print(' %s (%s)' % (item, itemCount[item]))
else:
print(' ' + item)
do_inv = do_inventory
def do_take(self, arg):
""""take <item> - Take an item on the ground."""
# put this value in a more suitably named variable
itemToTake = arg.lower()
if itemToTake == '':
print('Take what? Type "look" to see the items on the ground here.')
return
cantTake = False
# get the item name that the player's command describes
for item in getAllItemsMatchingDesc(itemToTake, worldRooms[location][GROUND]):
if not worldItems[item].get(TAKEABLE):
cantTake = True
continue # there may be other items named this that you can take, so we continue checking
print('You take %s.' % (worldItems[item][SHORTDESC]))
worldRooms[location][GROUND].remove(item) # remove from the ground
inventory.append(item) # add to inventory
return
if cantTake:
print('You cannot take "%s".' % itemToTake)
else:
print('That is not on the ground.')
def do_talk(self, arg):
itemToTalk = arg.lower()
if itemToTalk == '':
print('To whom? Type "look" to see everything around you!')
return
cantTalk = False
for item in getAllItemsMatchingDesc(itemToTalk, worldRooms[location][GROUND]):
if not worldItems[item].get(TALKABLE):
cantTalk = True
continue # there may be other items named this that you can look at, so we continue checking
print('You talk to %s.' % (worldItems[item][SHORTDESC]))
print('%s' % (worldItems[item][LONGDESC]))
return
if cantTalk:
print('You cannot talk to "%s".' % itemToTalk)
else:
print('This person isn\'t near you')
def do_drop(self, arg):
""""drop <item> - Drop an item from your inventory onto the ground."""
# put this value in a more suitably named variable
itemToDrop = arg.lower()
# get a list of all "description words" for each item in the inventory
invDescWords = getAllDescWords(inventory)
# find out if the player doesn't have that item
if itemToDrop not in invDescWords:
print('You do not have "%s" in your inventory.' % (itemToDrop))
return
# get the item name that the player's command describes
item = getFirstItemMatchingDesc(itemToDrop, inventory)
if item != None:
print('You drop %s.' % (worldItems[item][SHORTDESC]))
inventory.remove(item) # remove from inventory
worldRooms[location][GROUND].append(item) # add to the ground
def complete_take(self, text, line, begidx, endidx):
possibleItems = []
text = text.lower()
# if the user has only typed "take" but no item name:
if not text:
return getAllFirstDescWords(worldRooms[location][GROUND])
# otherwise, get a list of all "description words" for ground items matching the command text so far:
for item in list(set(worldRooms[location][GROUND])):
for descWord in worldItems[item][DESCWORDS]:
if descWord.startswith(text) and worldItems[item].get(TAKEABLE, True):
possibleItems.append(descWord)
return list(set(possibleItems)) # make list unique
def complete_talk(self, text, state):
possibleItems = []
text = text.lower()
# if the user has only typed "take" but no item name:
if not text:
return getAllFirstDescWords(worldRooms[location][GROUND])
# otherwise, get a list of all "description words" for ground items matching the command text so far:
for item in list(set(worldRooms[location][GROUND])):
for descWord in worldItems[item][DESCWORDS]:
if descWord.startswith(text) and worldItems[item].get(TALKABLE, True):
possibleItems.append(descWord)
return list(set(possibleItems)) # make list unique
def complete_drop(self, text, line, begidx, endidx):
possibleItems = []
itemToDrop = text.lower()
# get a list of all "description words" for each item in the inventory
invDescWords = getAllDescWords(inventory)
for descWord in invDescWords:
if line.startswith('drop %s' % (descWord)):
return [] # command is complete
# if the user has only typed "drop" but no item name:
if itemToDrop == '':
return getAllFirstDescWords(inventory)
# otherwise, get a list of all "description words" for inventory items matching the command text so far:
for descWord in invDescWords:
if descWord.startswith(text):
possibleItems.append(descWord)
return list(set(possibleItems)) # make list unique
def do_look(self, arg):
"""Look at an item, direction, or the area:
"look" - display the current area's description
"look <direction>" - display the description of the area in that direction
"look exits" - display the description of all adjacent areas
"look <item>" - display the description of an item on the ground or in your inventory"""
lookingAt = arg.lower()
if lookingAt == '':
# "look" will re-print the area description
displayLocation(location)
return
if lookingAt == 'exits':
for direction in (NORTH, SOUTH, EAST, WEST, UP, DOWN):
if direction in worldRooms[location]:
print('%s: %s' % (direction.title(), worldRooms[location][direction]))
return
if lookingAt in ('north', 'west', 'east', 'south', 'up', 'down', 'n', 'w', 'e', 's', 'u', 'd'):
if lookingAt.startswith('n') and NORTH in worldRooms[location]:
print(worldRooms[location][NORTH])
elif lookingAt.startswith('w') and WEST in worldRooms[location]:
print(worldRooms[location][WEST])
elif lookingAt.startswith('e') and EAST in worldRooms[location]:
print(worldRooms[location][EAST])
elif lookingAt.startswith('s') and SOUTH in worldRooms[location]:
print(worldRooms[location][SOUTH])
elif lookingAt.startswith('u') and UP in worldRooms[location]:
print(worldRooms[location][UP])
elif lookingAt.startswith('d') and DOWN in worldRooms[location]:
print(worldRooms[location][DOWN])
else:
print('There is nothing in that direction.')
return
# see if the item being looked at is on the ground at this location
item = getFirstItemMatchingDesc(lookingAt, worldRooms[location][GROUND])
if item != None:
print('\n'.join(textwrap.wrap(worldItems[item][LONGDESC], SCREEN_WIDTH)))
return
# see if the item being looked at is in the inventory
item = getFirstItemMatchingDesc(lookingAt, inventory)
if item != None:
print('\n'.join(textwrap.wrap(worldItems[item][LONGDESC], SCREEN_WIDTH)))
return
print('You do not see that nearby.')
def complete_look(self, text, line, begidx, endidx):
possibleItems = []
lookingAt = text.lower()
# get a list of all "description words" for each item in the inventory
invDescWords = getAllDescWords(inventory)
groundDescWords = getAllDescWords(worldRooms[location][GROUND])
shopDescWords = getAllDescWords(worldRooms[location].get(SHOP, []))
for descWord in invDescWords + groundDescWords + shopDescWords + [NORTH, SOUTH, EAST, WEST, UP, DOWN]:
if line.startswith('look %s' % (descWord)):
return [] # command is complete
# if the user has only typed "look" but no item name, show all items on ground, shop and directions:
if lookingAt == '':
possibleItems.extend(getAllFirstDescWords(worldRooms[location][GROUND]))
possibleItems.extend(getAllFirstDescWords(worldRooms[location].get(SHOP, [])))
for direction in (NORTH, SOUTH, EAST, WEST, UP, DOWN):
if direction in worldRooms[location]:
possibleItems.append(direction)
return list(set(possibleItems)) # make list unique
# otherwise, get a list of all "description words" for ground items matching the command text so far:
for descWord in groundDescWords:
if descWord.startswith(lookingAt):
possibleItems.append(descWord)
# otherwise, get a list of all "description words" for items for sale at the shop (if this is one):
for descWord in shopDescWords:
if descWord.startswith(lookingAt):
possibleItems.append(descWord)
# check for matching directions
for direction in (NORTH, SOUTH, EAST, WEST, UP, DOWN):
if direction.startswith(lookingAt):
possibleItems.append(direction)
# get a list of all "description words" for inventory items matching the command text so far:
for descWord in invDescWords:
if descWord.startswith(lookingAt):
possibleItems.append(descWord)
return list(set(possibleItems)) # make list unique
def do_list(self, arg):
"""List the items for sale at the current location's shop. "list full" will show details of the items."""
if SHOP not in worldRooms[location]:
print('This is not a shop.')
return
arg = arg.lower()
print('For sale:')
for item in worldRooms[location][SHOP]:
print(' - %s' % (item))
if arg == 'full':
print('\n'.join(textwrap.wrap(worldItems[item][LONGDESC], SCREEN_WIDTH)))
def do_eat(self, arg):
""""eat <item>" - eat an item in your inventory."""
itemToEat = arg.lower()
if itemToEat == '':
print('Eat what? Type "inventory" or "inv" to see your inventory.')
return
if itemToEat == 'stone':
print('You eat %s' % (itemToEat))
print('The magical stone exploded in your mouth...')
print("GAME OVER")
time.sleep(2)
return True
cantEat = False
for item in getAllItemsMatchingDesc(itemToEat, inventory):
if worldItems[item].get(EDIBLE, False) == False:
cantEat = True
continue # there may be other items named this that you can eat, so we continue checking
# NOTE - If you wanted to implement hunger levels, here is where
# you would add code that changes the player's hunger level.
print('You eat %s' % (worldItems[item][SHORTDESC]))
inventory.remove(item)
return
if cantEat:
print('You cannot eat that.')
else:
print('You do not have "%s". Type "inventory" or "inv" to see your inventory.' % (itemToEat))
def complete_eat(self, text, line, begidx, endidx):
itemToEat = text.lower()
possibleItems = []
# if the user has only typed "eat" but no item name:
if itemToEat == '':
return getAllFirstDescWords(inventory)
# otherwise, get a list of all "description words" for edible inventory items matching the command text so far:
for item in list(set(inventory)):
for descWord in worldItems[item][DESCWORDS]:
if descWord.startswith(text) and worldItems[item].get(EDIBLE, False):
possibleItems.append(descWord)
return list(set(possibleItems)) # make list unique
if __name__ == '__main__':
print('Burningham booted...')
print('====================\n')
print('(Type "help" for commands.)\n')
displayLocation(location)
TextAdventureCmd().cmdloop()