-
Notifications
You must be signed in to change notification settings - Fork 1
/
listing4-3.py
170 lines (135 loc) · 5.54 KB
/
listing4-3.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
#!/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]]
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."
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()
for y in range(room_height):
for x in range(room_width):
image_to_draw = DEMO_OBJECTS[room_map[y][x]]
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()