-
Notifications
You must be signed in to change notification settings - Fork 1
/
listing6-2.py
724 lines (679 loc) · 20.5 KB
/
listing6-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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/usr/bin/env python
# coding: utf-8
# Escape - A Python Adventure
# Personal work following the book “Mission Python” (Sean McManus)
# pgzrun import (+ pgzrun.go() command) makes the code running
# when calling it using the python/python3 command.
import pgzrun
import time, random, math
# Window Size
WIDTH = 800
HEIGHT = 800
PLAYER_NAME = "Steph"
FRIEND1_NAME = "Friend 1"
FRIEND2_NAME = "Friend 2"
DEMO_OBJECTS = [images.floor, images.pillar, images.soil]
MAP_WIDTH = 5
MAP_HEIGHT = 10
MAP_SIZE = MAP_WIDTH * MAP_HEIGHT
GAME_MAP = [["Room 0 - Where unused objects are kept", 0, 0, False, False]]
LANDER_SECTOR = random.randint(1, 24)
LANDER_X = random.randint(2, 11)
LANDER_Y = random.randint(2, 11)
current_room = 31
outdoor_rooms = range(1, 26)
top_left_x = 100
top_left_y = 150
for planetsectors in range(1, 26):
GAME_MAP.append(["The dusty planet surface", 13, 13, True, True])
GAME_MAP += [["The airlock", 13, 5, True, False], # room 26
["The engineering lab", 13, 13, False, False],
["Poodle Mission Control", 9, 13, False, True], # room 28
["The viewing gallery", 9, 15, False, False],
["The crew’s bathroom", 5, 5, False, False], # room 30
["The airlock entry bay", 7, 11, True, True],
["Left elbow room", 9, 7, True, False], # room 32
["Right elbow room", 7, 13, True, True],
["The science lab", 13, 13, False, True], # room 34
["The greenhouse", 13, 13, True, False],
[PLAYER_NAME + "’s sleeping quarters", 9, 11, False, False],
["West corridor", 15, 5, True, True],
["The briefing room", 7, 13, False, True], # room 38
["The crew’s community room", 11, 13, True, False],
["Main Mission Control", 14, 14, False, False], # room 40
["The sick bay", 12, 7, True, False],
["West corridor", 9, 7, True, False], # room 42
["Utilities control room", 9, 9, False, True],
["Systems engineering bay", 9, 11, False, False], # room 44
["Security portal to Mission Control", 7, 7, True, False],
[FRIEND1_NAME + "’s sleeping quarters", 9, 11, True, True],
[FRIEND2_NAME + "’s sleeping quarters", 9, 11, True, True],
["The pipeworks", 13, 11, True, False], # room 48
["The chief scientist’s office", 9, 7, True, True],
["The robot workshop", 9, 11, True, False]
]
assert len(GAME_MAP) - 1 == MAP_SIZE, "Map size and GAME_MAP don’t match."
objects = {
0: [images.floor,
None,
"The floor is shiny and clean."],
1: [images.pillar,
images.full_shadow,
"The wall is smooth and cold."],
2: [images.soil,
None,
"It’s like a desert. Or should that be dessert?"],
3: [images.pillar_low,
images.half_shadow,
"The wall is smooth and cold."],
4: [images.bed,
images.half_shadow,
"A tidy and comfortable bed."],
5: [images.table,
images.half_shadow,
"It’s made from strong plastic."],
6: [images.chair_left,
None,
"A chair with a soft cushion."],
7: [images.chair_right,
None,
"A chair with a soft cushion."],
8: [images.bookcase_tall,
images.full_shadow,
"Bookshelves, stacked with reference books."],
9: [images.bookcase_small,
images.half_shadow,
"Bookshelves, stacked with reference books."],
10: [images.cabinet,
images.half_shadow,
"A small locker, for storing personal items."],
11: [images.desk_computer,
images.half_shadow,
"A computer. Use it to run life support diagnostics."],
12: [images.plant,
images.plant_shadow,
"A spaceberry plant, grown here."],
13: [images.electrical1,
images.half_shadow,
"Electrical systems used for powering the space station."],
14: [images.electrical2,
images.half_shadow,
"Electrical systems used for powering the space station."],
15: [images.cactus,
images.cactus_shadow,
"Ouch! Careful on the cactus!"],
16: [images.shrub,
images.shrub_shadow,
"A space lettuce. A bit limp, but amazing it’s growing here!"],
17: [images.pipes1,
images.pipes1_shadow,
"Water purification pipes."],
18: [images.pipes2,
images.pipes2_shadow,
"Pipes for the life support systems."],
19: [images.pipes3,
images.pipes3_shadow,
"Pipes for the life support systems."],
20: [images.door,
images.door_shadow,
"Safety door. Opens automatically \
for astronauts in functioning spacesuits."],
21: [images.door,
images.door_shadow,
"The airlock door. \
For safety reasons, it requires two person operation."],
22: [images.door,
images.door_shadow,
"A locked door. It needs " + PLAYER_NAME + "’s access card."],
23: [images.door,
images.door_shadow,
"A locked door. It needs " + FRIEND1_NAME + "’s access card."],
24: [images.door,
images.door_shadow,
"A locked door. It needs " + FRIEND2_NAME + "’s access card."],
25: [images.door,
images.door_shadow,
"A locked door. It is opened from Main Mission Control."],
26: [images.door,
images.door_shadow,
"A locked door in the engineering bay."],
27: [images.map,
images.full_shadow,
"The screen says the crash site was Sector: " \
+ str(LANDER_SECTOR) + " // X: " + str(LANDER_X) \
+ " // Y " + str(LANDER_Y)],
28: [images.rock_large,
images.rock_large_shadow,
"A rock. Its coarse surface feels like a whetstone",
"the rock"],
29: [images.rock_small,
images.rock_small_shadow,
"A small but heavy piece of Martian rock."],
30: [images.crater,
None,
"A crater in the planet surface."],
31: [images.fence,
None,
"A fine gauze fence. \
It helps protect the station from dust storms."],
32: [images.contraption,
images.contraption_shadow,
"One of the scientific experiments. It gently vibrates."],
33: [images.robot_arm,
images.robot_arm_shadow,
"A robot arm, used for heavy lifting."],
34: [images.toilet,
images.half_shadow,
"A sparkling clean toilet."],
35: [images.sink,
None,
"A sink with running water.",
"the taps"],
36: [images.globe,
images.globe_shadow,
"A giant globe of the planet. \
It gently glows from inside."],
37: [images.science_lab_table,
None,
"A table of experiments, analyzing the planet soil and dust."],
38: [images.vending_machine,
images.full_shadow,
"A vending machine. It requires a credit.",
"the vending machine"],
39: [images.floor_pad,
None,
"A pressure sensor to make sure nobody goes out alone."],
40: [images.rescue_ship,
images.rescue_ship_shadow,
"A rescue ship!"],
41: [images.mission_control_desk,
images.mission_control_desk_shadow,
"Mission Control stations."],
42: [images.button,
images.button_shadow,
"The button for opening the time-locked door in engineering."],
43: [images.whiteboard,
images.full_shadow,
"The whiteboard is used in brainstorms and planning meetings."],
44: [images.window,
images.full_shadow,
"The window provides a view out onto the planet surface."],
45: [images.robot,
images.robot_shadow,
"A cleaning robot, turned off."],
46: [images.robot2,
images.robot2_shadow,
"A planet surface exploration robot, awaiting set-up."],
47: [images.rocket,
images.rocket_shadow,
"A 1-person craft in repair."],
48: [images.toxic_floor,
None,
"Toxic floor - do not walk on!"],
49: [images.drone,
None,
"A delivery drone."],
50: [images.energy_ball,
None,
"An energy ball - dangerous!"],
51: [images.energy_ball2,
None,
"An energy ball - dangerous!"],
52: [images.computer,
images.computer_shadow,
"A computer workstation, for managing space station systems."],
53: [images.clipboard,
None,
"A clipboard. Someone has doodled on it.",
"the clipboard"],
54: [images.bubble_gum,
None,
"A piece of sticky bubble gum. Spaceberry flavour.",
"bubble gum"],
55: [images.yoyo,
None,
"A toy made of fine, strong string and plastic. \
Used for antigrav experiments.",
PLAYER_NAME + "’s yoyo"],
56: [images.thread,
None,
"A piece of fine, strong string.",
"a piece of string"],
57: [images.needle,
None,
"A sharp needle from a cactus plant.",
"a cactus needle"],
58: [images.threaded_needle,
None,
"A cactus needle, spearing a length of string",
"needle and string"],
59: [images.canister,
None,
"The air canister has a leak.",
"a leaky air canister"],
60: [images.canister,
None,
"It looks like the seal will hold!",
"a sealed air canister"],
61: [images.mirror,
None,
"The mirror throws a circle of light on the walls.",
"a mirror"],
62: [images.bin_empty,
None,
"A rarely bin, made of light plastic",
"a bin"],
63: [images.bin_full,
None,
"A heavy bin full of water.",
"a bin full of water"],
64: [images.rags,
None,
"An oily rag. Pick it up by a corner if you must!",
"an oily rag"],
65: [images.hammer,
None,
"A hammer. Maybe good for cracking things open…",
"a hammer"],
66: [images.spoon,
None,
"A large serving spoon.",
"a spoon"],
67: [images.food_pouch,
None,
"A dehydrated food pouch. It needs water.",
"a dry food pack"],
68: [images.food,
None,
"A food pouch. Use it to get 100% energy.",
"ready-to-eat food"],
69: [images.book,
None,
"The book has the words 'Don’t Panic' \
on the cover in large, friendly letters.",
"a book"],
70: [images.mp3_player,
None,
"An MP3 player, with all the latest tunes.",
"an MP3 player"],
71: [images.lander,
None,
"The Poodle, a small space exploration craft. \
Its black box has a radio sealed inside.",
"the Poodle lander"],
72: [images.radio,
None,
"A radio communications system, from the Poodle.",
"a communications radio"],
73: [images.gps_module,
None,
"A GPS Module",
"a GPS module"],
74: [images.positioning_system,
None,
"Part of a positioning system. \
Needs a GPS module.",
"a positioning interface"],
75: [images.positioning_system,
None,
"A working positioning system.",
"a positioning computer"],
76: [images.scissors,
None,
"Scissors. They’re too blunt to cut anything. \
Can you sharpen them?",
"blunt scissors"],
77: [images.scissors,
None,
"Razor-sharp scissors. Careful!",
"sharpened scissors"],
78: [images.credit,
None,
"A small coin for the station’s vending systems.",
"a station credit"],
79: [images.access_card,
None,
"This access card belongs to " + PLAYER_NAME,
"an access card"],
80: [images.access_card,
None,
"This access card belongs to " + FRIEND1_NAME,
"an access card"],
81: [images.access_card,
None,
"This access card belongs to " + FRIEND2_NAME,
"an access card"]
}
items_player_may_carry = list(range(53, 82))
# Numbers for floor, pressure pad, soil and toxic floor.
items_player_may_stand_on = items_player_may_carry + [0, 39, 2, 48]
# Objects that cannot move between rooms.
scenery = {
26: [
[39, 8, 2]
],
27: [
[33, 5, 5],
[33, 1, 1],
[33, 1, 8],
[47, 5, 2],
[47, 3, 10],
[47, 9, 8],
[42, 1, 6]
],
28: [
[27, 0, 3],
[41, 4, 3],
[41, 4, 7]
],
29: [
[7, 2, 6],
[6, 2, 8],
[12, 1, 13],
[44, 0, 1],
[36, 4, 10],
[10, 1, 1],
[19, 4, 2],
[17, 4, 4]
],
30: [
[34, 1, 1],
[35, 1, 3]
],
31: [
[11, 1, 1],
[19, 1, 8],
[46, 1, 3]
],
32: [
[48, 2, 2],
[48, 2, 3],
[48, 2, 4],
[48, 3, 2],
[48, 3, 3],
[48, 3, 4],
[48, 4, 2],
[48, 4, 3],
[48, 4, 4]
],
33: [
[13, 1, 1],
[13, 1, 3],
[13, 1, 8],
[13, 1, 10],
[48, 2, 1],
[48, 2, 7],
[48, 3, 6],
[48, 3, 3]
],
34: [
[37, 2, 2],
[32, 6, 7],
[37, 10, 4],
[28, 5, 3]
],
35: [
[16, 2, 9],
[16, 2, 2],
[16, 3, 3],
[16, 3, 8],
[16, 8, 9],
[16, 8, 2],
[16, 1, 8],
[16, 1, 3],
[12, 8, 6],
[12, 9, 4],
[12, 9, 8],
[15, 4, 6],
[12, 7, 1],
[12, 7, 11]
],
36: [
[4, 3, 1],
[9, 1, 7],
[8, 1, 8],
[8, 1, 9],
[5, 5, 4],
[6, 5, 7],
[10, 1, 1],
[12, 1, 2]
],
37: [
[48, 3, 1],
[48, 3, 2],
[48, 7, 1],
[48, 5, 2],
[48, 5, 3],
[48, 7, 2],
[48, 9, 2],
[48, 9, 3],
[48, 11, 1],
[48, 11, 2]
],
38: [
[43, 0, 2],
[6, 2, 2],
[6, 3, 5],
[6, 4, 7],
[6, 2, 9],
[45, 1, 10]
],
39: [
[38, 1, 1],
[7, 3, 4],
[7, 6, 4],
[5, 3, 6],
[5, 6, 6],
[6, 3, 9],
[6, 6, 9],
[45, 1, 11],
[12, 1, 8],
[12, 1, 4]
],
40: [
[41, 5, 3],
[41, 5, 7],
[41, 9, 3],
[41, 9, 7],
[13, 1, 1],
[13, 1, 3],
[42, 1, 12]
],
41: [
[4, 3, 1],
[10, 3, 5],
[4, 5, 1],
[10, 5, 5],
[4, 7, 1],
[10, 7, 5],
[12, 1, 1],
[12, 1, 5]
],
44: [
[46, 4, 3],
[46, 4, 5],
[18, 1, 1],
[19, 1, 3],
[19, 1, 5],
[52, 4, 7],
[14, 1, 8]
],
45: [
[48, 2, 1],
[48, 2, 2],
[48, 3, 3],
[48, 3, 4],
[48, 1, 4],
[48, 1, 1]
],
46: [
[10, 1, 1],
[4, 1, 2],
[8, 1, 7],
[9, 1, 8],
[8, 1, 9],
[5, 4, 3],
[7, 3, 2]
],
47: [
[9, 1, 1],
[9, 1, 2],
[10, 1, 3],
[12, 1, 7],
[5, 4, 4],
[6, 4, 7],
[4, 1, 8]
],
48: [
[17, 4, 1],
[17, 4, 2],
[17, 4, 3],
[17, 4, 4],
[17, 4, 5],
[17, 4, 6],
[17, 4, 7],
[17, 8, 1],
[17, 8, 2],
[17, 8, 3],
[17, 8, 4],
[17, 8, 5],
[17, 8, 6],
[17, 8, 7],
[14, 1, 1]
],
49: [
[14, 2, 2],
[14, 2, 4],
[7, 5, 1],
[5, 5, 3],
[48, 3, 3],
[48, 3, 4]
],
50: [
[45, 4, 8],
[11, 1, 1],
[13, 1, 8],
[33, 2, 1],
[46, 4, 6]
]
}
checksum = 0
check_counter = 0
for key, room_scenery_list in scenery.items():
for scenery_item_list in room_scenery_list:
checksum += (scenery_item_list[0] * key
+ scenery_item_list[1] * (key + 1)
+ scenery_item_list[2] * (key + 2)
)
check_counter += 1
print(check_counter, "scenery items")
assert check_counter == 161, "Expected 161 scenery items."
assert checksum == 200095, "Error in scenery data."
print("Scenery checksum: " + str(checksum))
# Add random scenery in planet locations.
for room in range(1, 26):
if room != 13:
scenery_item = random.choice([16, 28, 29, 30])
scenery[room] = [[scenery_item, random.randint(2, 10),
random.randint(2, 10)]
]
# Add fences to the planet surface rooms.
for room_coordinate in range(0, 13):
for room_number in [1, 2, 3, 4, 5]:
scenery[room_number] += [[31, 0, room_coordinate]]
for room_number in [1, 6, 11, 16, 21]:
scenery[room_number] += [[31, room_coordinate, 0]]
for room_number in [5, 10, 15, 20, 25]:
scenery[room_number] += [[31, room_coordinate, 12]]
# Delete last fence panel in Rooms 21 and 25.
del scenery[21][-1]
del scenery[25][-1]
def get_floor_type():
if current_room in outdoor_rooms:
return 2 # soil
else:
return 0 # tiled floor
def generate_map():
global room_map, room_width, room_height, room_name, hazard_map
global top_left_x, top_left_y, wall_transparency_frame
room_data = GAME_MAP[current_room]
room_name = room_data[0]
room_height = room_data[1]
room_width = room_data[2]
floor_type = get_floor_type()
if current_room in range(1, 21):
bottom_edge = 2 # soil
side_edge = 2
if current_room in range(21, 26):
bottom_edge = 1 # wall
side_edge = 2
if current_room > 25:
bottom_edge = 1
side_edge = 1
# Top line of room map
room_map = [[side_edge] * room_width]
# Add middle lines (wall, floor to fill width, wall)
for y in range(room_height - 2):
room_map.append([side_edge]
+ [floor_type] * (room_width - 2) + [side_edge])
# Add bottom line of room map
room_map.append([bottom_edge] * room_width)
# Add doorways
middle_row = int(room_height / 2)
middle_column = int(room_width / 2)
if room_data[4]: # If exit at right of this room
room_map[middle_row][room_width - 1] = floor_type
room_map[middle_row + 1][room_width - 1] = floor_type
room_map[middle_row - 1][room_width - 1] = floor_type
if current_room % MAP_WIDTH != 1: # If not on the left of the map
room_to_left = GAME_MAP[current_room - 1]
if room_to_left[4]:
room_map[middle_row][0] = floor_type
room_map[middle_row + 1][0] = floor_type
room_map[middle_row - 1][0] = floor_type
if room_data[3]: # If exit at top of this room
room_map[0][middle_column] = floor_type
room_map[0][middle_column + 1] = floor_type
room_map[0][middle_column - 1] = floor_type
if current_room <= MAP_SIZE - MAP_WIDTH: # If room not on bottom row
room_below = GAME_MAP[current_room + MAP_WIDTH]
if room_below[3]:
room_map[room_height - 1][middle_column] = floor_type
room_map[room_height - 1][middle_column + 1] = floor_type
room_map[room_height - 1][middle_column - 1] = floor_type
def draw():
global room_height, room_width, room_map
generate_map()
screen.clear()
room_map[2][4] = 7
room_map[2][6] = 6
room_map[1][1] = 8
room_map[1][2] = 9
room_map[1][8] = 12
room_map[1][9] = 9
for y in range(room_height):
for x in range(room_width):
image_to_draw = objects[room_map[y][x]][0]
screen.blit(image_to_draw,
(top_left_x + (x * 30),
top_left_y + (y * 30) - image_to_draw.get_height()))
# Exploring the room
def movement():
global current_room
old_room = current_room
if keyboard.left:
current_room -= 1
if keyboard.right:
current_room += 1
if keyboard.up:
current_room -= MAP_WIDTH
if keyboard.down:
current_room += MAP_WIDTH
if current_room > 50:
current_room = 50
if current_room < 1:
current_room = 1
if current_room != old_room:
print("Entering room: " + str(current_room))
clock.schedule_interval(movement, 0.1)
pgzrun.go()