-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescape_room.py
311 lines (261 loc) · 11.6 KB
/
escape_room.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
import random
import time
import sys
class Choice():
def __init__(self, answer, start, end):
self.answer = answer
self.start = start
self.end = end
# Create all the objects in the game
def create_items(choice):
choice.rooms_list = ["Living Room", "Bedroom", "Bathroom", "Kitchen", "Garage"]
choice.room_objects = [ ["Couch", "Rug", "Plant", "TV", "Shelf"],
["Bed", "Desk", "Drawer", "Lamp", "Wardrobe"],
["Cabinet", "Toilet", "Basin", "Bathtub", "Towel"],
["Cabinet", "Sink", "Microwave", "Oven", "Fridge"],
["Tools Rack", "Car Trunk", "Spare Tire", "Box", "Door"], ]
choice.inventory = set()
# Select which rooms are connected to each other. Check "floor_plan.png" for more info
choice.connected_index = [[1, 3], [0, 2], [1], [0, 4], [3]]
# Set the start and exit locations
choice.current_room = choice.rooms_list[0]
choice.exit_room = choice.rooms_list[4]
choice.exit_object = choice.room_objects[4][4]
# Link the objects to their corresponding rooms
choice.objects = {choice.rooms_list[i]:j for i, j in enumerate(choice.room_objects)}
choice.connected = {choice.rooms_list[i]:j for i, j in enumerate(choice.connected_index)}
# Randomize the colours of the special keys used to unlock the exit
special_room_objects = []
special_room = ""
special_object = ""
colours = "Red, Green, Blue, Orange, Yellow, Black, White, Pink, Purple"
colours = colours.split(", ")
check = True
for index, colour in enumerate(colours):
colours[index] = colour + " Key"
random.shuffle(colours)
# Randomize the location of the special keys
while len(special_room_objects) != choice.number_of_keys:
special_room = random.choice(list(choice.objects.keys()))
special_object = random.choice(choice.objects[special_room])
# Special keys should not be located at the exit door. Also remove duplicates
if (special_room != choice.exit_room and special_object != choice.exit_object and
[special_room , special_object] not in special_room_objects):
special_room_objects.append([special_room, special_object])
choice.special_keys = dict(zip(colours, special_room_objects))
# Show locations of the keys. Leave as a comment
# print(choice.special_keys)
def load_file(file_name):
try:
with open(file_name, "r") as file:
lines = file.readlines()
except:
missing_file(file_name)
sys.exit()
return lines
def missing_file(file_name):
print("> Missing file required to use this feature")
print(f"> Please import \"{file_name}\" into the same directory")
# Display layout for the floor plan
def print_layout(choice):
# time.sleep(choice.delay_msg)
for line in choice.layout:
line = line.rstrip("\n")
print(line)
check_continue = input("Enter any key to continue: ")
if check_continue.lower().strip() == "quit":
sys.exit()
# Start screen
def start_game(choice, game_started, floor_plan):
if game_started == False and floor_plan == False:
if choice.answer.lower().strip() == "yes":
print("---")
print("You have been locked in an unknown house. Find the exit!")
print("Wait, what's this...? Looks like a floor plan of the building")
print("Now we know where the exit is! Hope it's not locked though...")
print("---")
time.sleep(choice.delay_msg)
check_continue = input("Enter any key to continue: ")
if check_continue.lower().strip() == "quit":
sys.exit()
game_started = True
elif choice.answer.lower().strip() == "no":
print("Maybe next time. Bye")
time.sleep(choice.delay_msg)
input("Enter any key to quit: ")
sys.exit()
else:
print("Invalid response. Please try again!")
choice.answer = input("Do you want to play (yes/no)? ")
# Show the option to view the floor plan
elif game_started == True and floor_plan == False:
choice.answer = input("Would you like to view floor plan (yes/no)? ")
if choice.answer.lower().strip() == "yes":
print_layout(choice)
floor_plan = True
elif choice.answer.lower().strip() == "no":
floor_plan = True
else:
print("Invalid response. Please try again!")
elif game_started == True and floor_plan == True:
main_menu(choice)
return game_started, floor_plan
# Main menu inputs
def main_menu(choice):
select_action(choice)
if choice.answer.lower().strip() == "1":
display_objects(choice)
elif choice.answer.lower().strip() == "2":
location(choice)
elif choice.answer.lower().strip() == "3":
print_layout(choice)
elif choice.answer.lower().strip() == "4":
show_inventory(choice)
else:
if choice.answer.lower().strip() != "quit":
print("Invalid response. Please try again!")
# Track user's input
def selection(choice):
if choice.end > 1:
choice.answer = input(f"Make your selection ({choice.start}-{choice.end}): ")
else:
choice.answer = input(f"Make your selection ({choice.start}): ")
# Main menu screen
def select_action(choice):
menu_screen = []
menu_screen.append("Explore The Room")
menu_screen.append("Change Locations")
menu_screen.append("View Floor Plan")
menu_screen.append("Open Inventory")
count = 1
print(f"\nYou are in the {choice.current_room}:")
for option in menu_screen:
print(f"{count}) {option}")
count += 1
choice.end = choice.menu_options
selection(choice)
# Display list of objects
def display_objects(choice):
choice.start = 1
choice.end = len(choice.objects.get(choice.current_room)) + 1
flag = False
while flag != True:
count = 1
print(f"\nYou find the following objects in the {choice.current_room}: ")
for r in choice.objects.get(choice.current_room):
print(f"{count}) {r}")
count += 1
print(f"{choice.end}) Go back")
selection(choice)
flag = check_objects(choice, flag)
# Change locations
def location(choice):
count = 1
print("\nWhere would you like to go?")
for r in choice.connected.get(choice.current_room):
print(f"{count}) {choice.rooms_list[r]}")
count += 1
print(f"{count}) Go back")
index = {i:j for i, j in enumerate(choice.connected.get(choice.current_room))}
choice.end = count
selection(choice)
try:
int(choice.answer.lower().strip())
except ValueError:
print("Invalid response. Please try again!")
location(choice)
else:
if int(choice.answer.lower().strip()) in list(range(choice.start, choice.end)):
choice.current_room = choice.rooms_list[index[int(choice.answer.lower().strip()) - 1]]
elif int(choice.answer.lower().strip()) == choice.end:
main_menu(choice)
else:
print("Invalid response. Please try again!")
location(choice)
# Check objects in each room
def check_objects(choice, flag):
try:
int(choice.answer.lower().strip())
except ValueError:
print("Invalid response. Please try again!")
else:
# Go back to the previous menu
if choice.answer.lower().strip() == str(choice.end):
flag = True
elif int(choice.answer.lower().strip()) in list(range(1, choice.end)):
current_object = choice.objects[choice.current_room][int(choice.answer.lower().strip()) - 1]
# Display special messages when interacting with the exit door
if choice.current_room == choice.exit_room and current_object == choice.exit_object:
if len(choice.inventory) != choice.number_of_keys:
print("---")
print("This must be the exit that was marked on the floor plan")
print("But it seems to be locked...")
print(f"There's {choice.number_of_keys} coloured padlocks on the door")
print("Looks like you'll need to search around the house for these special keys")
print("Let's go back...")
print("---")
time.sleep(choice.delay_msg)
check_continue = input("Enter any key to continue: ")
if check_continue.lower().strip() == "quit":
sys.exit()
else:
print("---")
print("Looks like you have all the keys needed to open this door")
print("You carefully insert all the keys to their corresponding padlocks")
print("You hear a click sound and the door slams wide open")
print("Finally... Now it's time to go back home...")
print("---")
time.sleep(choice.delay_msg)
check_continue = input("Enter any key to continue: ")
print("Congratulations! You have successfully escaped. Thank you for playing!\n")
time.sleep(choice.delay_msg)
input("Enter any key to quit: ")
sys.exit()
# Check for special keys
elif [choice.current_room, current_object] in choice.special_keys.values():
for key_colour, room_object in choice.special_keys.items():
if [room_object[0], room_object[1]] == [choice.current_room, current_object]:
if key_colour not in choice.inventory:
print(f"Found special item [{key_colour}]")
choice.inventory.add(key_colour)
time.sleep(choice.delay)
else:
response = random.choice(choice.responses)
print(response.rstrip("\n"))
time.sleep(choice.delay_msg)
# Dislay a random message for other interactions
else:
response = random.choice(choice.responses)
print(response.rstrip("\n"))
time.sleep(choice.delay_msg)
else:
print("Invalid response. Please try again!")
return flag
def show_inventory(choice):
print("Current items in your inventory:")
if len(choice.inventory) > 0:
for item in choice.inventory:
print(item)
else:
print("None")
time.sleep(choice.delay_msg)
def main():
menu_options = 4
game_started = False
floor_plan = False
file_escape_layout = "escape_layout.txt"
file_responses = "responses.txt"
choice = input("Do you want to play (yes/no)? ")
choice = Choice(choice, 1, menu_options)
choice.layout = load_file(file_escape_layout)
choice.responses = load_file(file_responses)
random.shuffle(choice.responses)
choice.delay_msg = 1
choice.delay = 3
choice.number_of_keys = 3
choice.menu_options = menu_options
create_items(choice)
while(choice.answer.lower().strip() != "quit"):
game_started, floor_plan = start_game(choice, game_started, floor_plan)
if __name__ == "__main__":
main()