-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzork.py
636 lines (573 loc) · 19 KB
/
zork.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# TODO:
# *** Save game ***
# important information:
# number of trees
# inventory
#
# chickens in the field by the log cabin hut and in tree house
# eat banana
# eat fish doesn't show an error
# get tired if you chop down a tree
# healthy of "tired"? (energy level?)
# build a log cabin hut
# go to the log cabin hut
# sell stuff and get money
# more stuff to do in treehouse
# more health than 2
# what happens when you run in the clearing?
# chickens
# fight bear with bare hands
# fight with "bear" hands?
# do something in forest (like a maze with a clearing with a house with a guy that sells you things)
# quit/die command?
# money
# fight a tiger in the forest
# dinosaurs in the clearing or town
# a town
# make a bow and arrow
# more efficient way to alter inventory randomly
#
# Bugs:
# - If you climb with an ax on the ground, you get hurt
import sys
import random
import time
class Player:
def have_wood(player):
return inv.wood > 0
class Bear:
pass
class Inventory:
pass
class Forest:
pass
class Supplies:
pass
you = Player()
you.location = "pit"
# you.location = "clearing" # ***
you.health = "healthy"
bear = Bear()
bear.health = "healthy"
forest = Forest()
forest.trees = 10
supplies = Supplies()
supplies.things = 3
inv = Inventory()
inv.ax = 3
inv.rope = 1
# inv.stone = 2 # ***
inv.stone = 0
inv.bear_skin = 0
inv.apple = 2
# inv.bear_meat = 3 # ***
inv.bear_meat = 0
inv.roasted_bear_meat = 0
inv.wood = 0
inv.banana = 2
inv.chicken = 0
inv.crowbar = 0
inv.armor = 0
def print_inventory():
print "You have: "
if inv.ax:
print " " + str(inv.ax) + " axes"
if inv.rope:
print " " + str(inv.rope) + " rope"
if inv.stone:
print " " + str(inv.stone) + " stone"
if inv.bear_skin:
print " " + str(inv.bear_skin) + " bear skin"
if inv.bear_meat:
print " " + str(inv.bear_meat) + " bear meat"
if inv.roasted_bear_meat:
print " " + str(inv.roasted_bear_meat) + " roasted bear meat"
if inv.apple:
print " " + str(inv.apple) + " apple"
if inv.banana:
print " " + str(inv.banana) + " banana"
if inv.wood:
print " " + str(inv.wood) + " wood"
if inv.chicken:
print " " + str(inv.chicken) + " chickens"
if inv.armor:
print " " + str(inv.armor) + " armor"
if inv.crowbar:
print " " + str(inv.crowbar) + " crowbars"
def help():
print "You can: "
print " look (l)"
print " inventory (i)"
print " run (r)"
print " eat (e)"
if inv.ax > 0:
print " weave ax into rope"
print " sleep (s)"
if you.location == "pit":
print " climb with rope (cr)"
if inv.ax > 0:
print " climb with ax (ca)"
print " mine with ax (ma)"
elif you.location == "ground":
if inv.ax > 0:
print " fight with ax (fa)"
print " fight with stone (fs)"
print " jump into pit (j)"
elif you.location == "forest":
if inv.ax > 0:
print " chop (ch)"
print " climb (c)"
elif you.location == "treehouse":
if inv.ax > 0:
print " chop (ch)"
def print_your_location():
if you.location == "pit":
print "You are in a pit."
elif you.location == "ground":
print "You are on the ground. There is a bear."
elif you.location == "forest":
print "You are in the forest."
elif you.location == "clearing":
print "You are in a clearing. There is a log cabin hut in the middle. There are chickens walking around."
elif you.location == "roof":
print "You are on the roof of a log cabin hut in a clearing."
elif you.location == "treehouse":
print "You are in a treehouse. There are some supplies. There are " + str(supplies.things) + " things in the supplies."
else:
print "you.location == '" + you.location + "'"
def print_your_health():
if you.health == "healthy":
print "You are healthy."
elif you.health == "wounded":
print "You are wounded."
else:
print "you.health == '" + you.health + "' state."
def print_bear_health():
if bear.health == "healthy":
print "The bear is healthy."
elif bear.health == "wounded":
print "The bear is wounded."
elif bear.health == "dead":
print "The bear is dead."
def climb():
if you.location == "pit":
print "You tried climbing with your hands, but it fails miserably."
you_got_hurt()
elif you.location == "forest":
print "You find a treehouse laden with supplies."
you.location = "treehouse"
elif you.location == "clearing":
print "You are now on top of the log cabin hut."
you.location = "roof"
elif you.location == "roof":
print "You try to climb an invisible wall, but fall off the roof and plow a tunnel through the ground all the way back to the pit."
you.location = "pit"
you_got_hurt()
else:
print "You can't climb here."
def climb_with_rope():
if you.location == "pit":
you.location = "ground"
print_your_location()
elif you.location == "forest":
outcome = random.choice(["succeed", "fall"])
if outcome == "fall":
print "You tried climbing with the rope, but you fell and got hurt. Try climbing with you bare hands next time."
you_got_hurt()
else:
you.location = "treehouse"
elif you.location == "clearing":
print "You lasso the rope around the chimney and pull it down. The man in the log cabin hut comes out and shoots you."
you.location = "roof"
# TODO: Random outcome of where he shot you.
you_got_hurt()
elif you.location == "roof":
print "You climb down the chimney into a fire and are burned."
# TODO: Random outcome of where he shot you.
you_got_hurt()
else:
print "You can't climb here."
def ax_climbing():
if inv.ax == 0:
print "You don't have an ax."
return
you_got_hurt()
if you.health == "dead":
print "You fell and died. I thought that by now you would learn that climbing with an ax is a bad idea."
else:
print "You fell and got wounded."
def ax_fighting():
if inv.ax == 0:
print "You don't have an ax."
return
if you.location == "ground":
outcome = random.choice(["win", "lose"])
if outcome == "win":
if bear.health == "wounded":
print "You beat the bear!"
bear.health = "dead"
print "You gather up his skin and you chop him up into meat but he somehow flung you into the forest while you were doing so."
you.location = "forest"
inv.bear_skin += 1
inv.bear_meat += 3
else:
print "A blow with the bear connected."
bear.health = "wounded"
elif outcome == "lose":
you_got_hurt()
if you.health == "dead":
print "The bear ate you! At least you were tasty."
else:
print "The bear slashed his claws at you!"
else:
print "You can't fight here."
def ax_mining():
if inv.ax == 0:
print "You don't have an ax."
return
if you.location == "pit":
print "You received stone, but cut your finger."
you_got_hurt()
inv.stone += 1
else:
print "You can't mine here."
def stone_fighting():
if inv.stone == 0:
print "You imagine yourself throwing a stone."
elif you.location == "ground":
outcome = random.choice(["hit", "miss"])
if outcome == "hit":
if bear.health == "wounded":
print "You nailed the bear!"
bear.health = "dead"
print "He dropped a bear skin and flung you into the forest."
you.location = "forest"
inv.bear_skin += 1
inv.bear_meat += 1
else:
print "You hit the bear."
bear.health = "wounded"
elif outcome == "miss":
print "You missed. The bear is angry."
inv.stone -= 1
else:
print "You hit yourself in the head with the stone."
you_got_hurt()
def you_got_hurt():
if you.health == "wounded":
you.health = "dead"
else:
you.health = "wounded"
def you_got_healed():
if you.health == "dead":
pass
else:
you.health = "healthy"
def sleep():
print "You are going into a deep sleep... dream world..."
for _ in range(5):
print "..."
time.sleep(.1)
outcome = random.choice(["stone stolen", "ax stolen", "nothing", "bear attack"])
if outcome == "stone stolen":
if inv.stone > 0:
inv.stone -= 1
print "A thief stole a stone!"
else:
print "You dreamt of a man stealing your stones. But you have none."
elif outcome == "ax stolen":
if inv.ax > 0:
inv.ax = 0
print "A thief stole your ax!"
else:
print "You dreamt of a man stealing your ax. But you have none."
elif outcome == "nothing":
print "Nothing was stolen. Lucky you."
elif outcome == "bear attack":
if you.location == "ground":
print "The bear got you while you're sleeping."
you.health = "dead"
else:
print "You dreamt of a bear attacking you."
you_got_healed()
if you.health != "dead":
print "You're awake! Luckily, you're still alive."
def jump():
if you.location == "pit":
print "You can't jump out of a pit."
elif you.location == "ground":
print "You hit the ground painfully."
you_got_hurt()
you.location = "pit"
elif you.location == "clearing":
print "You jump so far you land in the pit (???). Too bad. So sad. Have fun repeating all that stuff."
you.location = "pit"
elif you.location == "roof":
print "You hop of the roof with no problem."
you.location = "clearing"
elif you.location == "treehouse":
print "You jump so far you land in the pit (???). Too bad. So sad. Have fun repeating all that stuff."
def eat(what):
if what == "apple":
if inv.apple > 0:
you_got_healed()
inv.apple -= 1
print "You munched into the juicy apple and feel better."
elif inv.apple == 0:
print "You imagine you're eating an apple. But you don't really have one."
elif what == "bear meat":
if inv.bear_meat > 0:
outcome = random.choice(["rotten", "delicious"])
if outcome == "rotten":
print "That meat was rotten. Why did you eat it?"
you_got_hurt()
elif outcome == "delicious":
print "You ate the bear and it was delicious. You feel much better."
you_got_healed()
inv.bear_meat -= 1
else:
print "You imagine you're eating bear meat. But you don't really have any."
elif what == "roasted bear meat":
if inv.roasted_bear_meat > 0:
print "That was delicious apple-roasted bear meat! Your health improves"
you_got_healed()
you_got_healed()
inv.roasted_bear_meat -= 1
else:
print "You imagine eating a juicy bear steak with apples. But you don't have any."
elif what == "banana":
if inv.banana > 0:
print "That was a good banana!"
you_got_healed()
inv.banana -= 1
else:
print "You don't have a banana!"
def dance():
print "You did a little jig."
def run():
if you.location == "pit":
print "You bump your head on the wall"
you_got_hurt()
elif you.location == "ground":
if bear.health == "healthy":
print "You bear ate you! At least you were tasty."
you.health = "dead"
print_your_health()
else:
print "You escape!"
you.location = "forest"
print_your_location()
elif you.location == "forest":
print "You run into a clearing with a log cabin hut in the middle."
you.location = "clearing"
elif you.location == "clearing":
print "You run back to the forest."
you.location = "forest"
elif you.location == "roof":
print "You run across the air (you're magic!) but you land in the pit. Don't press your luck."
you.location = "pit"
elif you.location == "treehouse":
print "You run across the air (you're magic!) but you land in the pit. Don't press your luck."
you.location = "pit"
def chop():
if you.location == "forest":
if not forest.trees:
print "You chopped down all the trees! You swung and hit your leg."
you_got_hurt()
else:
outcome = random.choice(["apple", "banana", "wood"])
if outcome == "apple":
print "You chopped down an apple tree and got apples and wood."
inv.apple += 20
inv.wood += 10
elif outcome == "banana":
print "You chopped down a banana tree and got bananas and wood."
inv.banana += 100
inv.wood += 10
elif outcome == "wood":
print "You chopped down a tree and got lots of wood."
inv.wood += 20
else:
print "What kind of tree was that??? " + outcome
forest.trees -= 1
elif you.location == "treehouse":
print "You chopped down the tree you were in! You die."
you.health = "dead"
elif you.location == "clearing":
# TODO: Random outcome
print "You chopped down the wheat of the owner of the log cabin hut. He's not happy with you so he shot at you."
you_got_hurt()
else:
print "There is no tree here. You swung and it your leg."
you_got_hurt()
def cook():
if not you.have_wood():
print "You need wood to cook on a fire."
elif not inv.stone > 1:
print "You need at least two stones to start a fire."
you_got_hurt()
elif inv.apple == 0:
print "You can't roast bear meat without apples!"
elif inv.bear_meat == 0:
print "You wasted your apples roasting nothing."
inv.apple -= 1
else:
print "You start a roasty fire using your wood and stones and cook your bear meat and apples"
inv.bear_meat -= 1
inv.apple -= 1
inv.roasted_bear_meat += 1
def grab():
if you.location == "treehouse":
if supplies.things < 1:
print "There's nothing left. You took it all, you selfish thief."
else:
supplies.things -= 1
outcome = random.choice(["crowbar", "suite of armor", "ax"])
print "You grabbed a " + outcome
if outcome == "crowbar":
inv.crowbar += 1
elif outcome == "ax":
inv.ax += 1
elif outcome == "suite of armor":
inv.armor += 1
else:
print "Nothing!!! Mwahahahahah"
else:
print "There's nothing to grab."
code_from_location_dictionary = {
"ground": "g",
"forest": "f",
"clearing": "c",
"treehouse": "t",
"roof": "r",
"pit": "p"
}
location_from_code_dictionary = {
"g" : "ground",
"f" : "forest",
"c" : "clearing",
"t" : "treehouse",
"r" : "roof",
"p" : "pit"
}
code_from_health_dictionary = {
"healthy": "h",
"wounded": "w",
"dead": "d"
}
health_from_code_dictionary = {
"h": "healthy",
"w": "wounded",
"d": "dead"
}
def save_game():
name = "saved-game"
file = open(name, "w")
location_code = code_from_location_dictionary.get(you.location)
file.write(location_code)
health_code = code_from_health_dictionary.get(you.health)
file.write(health_code)
bear_health_code = code_from_health_dictionary.get(bear.health)
file.write(bear_health_code)
file.close()
print "Saved to '" + name + "'"
def continue_game():
name = "saved-game"
file = open(name, "r")
location_code = file.read(1)
health_code = file.read(1)
bear_health_code = file.read(1)
you.location = location_from_code_dictionary.get(location_code)
print_your_location()
you.health = health_from_code_dictionary.get(health_code)
print_your_health()
bear.health = health_from_code_dictionary.get(bear_health_code)
print_bear_health()
# you.health = "what it was before"
# forest.trees = "what it was before"
# bear.health = "what it was before"
# inv = "what it was before"
# print "Continuing game from '" + name + "'"
# print "Your location is '" + you.location + "'"
shortcuts = {
"l" : "look",
"i" : "inventory",
"d" : "dance",
"cr" : "climb with rope",
"co" : "cook",
"ch" : "chop",
"ca" : "climb with ax",
"ea" : "eat apple",
"eb" : "eat banana",
"ebm" : "eat bear meat",
"fa" : "fight with ax",
"fs" : "fight with stone",
"s" : "sleep",
"h" : "help",
"c" : "climb",
"r" : "run",
"j" : "jump",
"g" : "grab",
"ma" : "mine with ax",
"z" : "continue",
"s" : "save",
"q" : "quit"
}
commands = {
"dance" : dance,
"inventory" : print_inventory,
"sleep" : sleep,
"climb" : climb,
"chop" : chop,
"climb with ax" : ax_climbing,
"fight with ax" : ax_fighting,
"fight with stone" : stone_fighting,
"jump" : jump,
"mine with ax" : ax_mining,
"cook" : cook,
"grab" : grab,
"run" : run,
"help" : help,
"save" : save_game,
"continue" : continue_game
}
print "Welcome to Zork. You can ask for help (h)."
print_your_location()
while True:
if you.health == "dead":
print "******************************"
print "* You have died *"
print "******************************"
break
input = sys.stdin.readline().strip()
if input in shortcuts:
input = shortcuts.get(input)
# print "You used a shortcut! You changed to " + input
if input in commands:
command = commands.get(input)
command()
elif input.startswith("eat"):
if input.startswith("eat "):
nothing, what = input.split(" ", 1)
eat(what)
else:
print "Eat what?"
elif input == "climb with rope":
climb_with_rope()
elif input == "look":
print_your_location()
print_your_health()
if you.location == "ground":
print_bear_health()
if you.location == "forest":
print "There are " + str(forest.trees) + " trees."
elif input == "weave ax into rope":
print "You cut yourself trying to bend a sharp piece of metal."
you_got_hurt()
elif input == "jump" or input == "j":
jump()
elif input == "quit":
save_game()
break
else:
print "That doesn't make sense."