forked from ta-data-lis/python-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_code_1.py
More file actions
454 lines (350 loc) · 13.4 KB
/
test_code_1.py
File metadata and controls
454 lines (350 loc) · 13.4 KB
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
import random
import time
from multiprocessing import Process
import sys
# Can't reinitialize game after losing all lives
# Can't handle inputs that arent A,B,C,D
# define rooms
game_room = {
"name": "game room",
"type": "room",
}
bedroom_1 = {
"name": "bedroom 1",
"type": "bedroom",
}
bedroom_2 = {
"name": "bedroom 2",
"type": "bedroom",
}
living_room = {
"name": "living room",
"type": "room",
}
outside = {
"name": "outside"
}
#Define Items
#Itmens in game room
couch = {
"name": "couch",
"type": "furniture",
}
door_a = {
"name": "door a",
"type": "door",
}
key_a = {
"name": "key for door a",
"type": "key",
"target": door_a,
}
piano = {
"name": "piano",
"type": "furniture",
}
#Items bedroom 1
queen_bed = {
"name": "queen bed",
"type": "furniture",
}
door_b = {
"name": "door b",
"type": "door",
}
key_b = {
"name": "key for door b",
"type": "key",
"target": door_b,
}
door_c = {
"name": "door c",
"type": "door",
}
# Items in bedroom 2
double_bed = {
"name": "double bed",
"type": "furniture",
}
dresser = {
"name": "dresser",
"type": "furniture",
}
key_c = {
"name": "key for door c",
"type": "key",
"target": door_c,
}
door_d = {
"name": "door d",
"type": "door",
}
key_d = {
"name": "key for door d",
"type": "key",
"target": door_d,
}
#Items in Living Room
dining_table = {
"name": "dining table",
"type": "furniture",
}
from optparse import Values
import PySimpleGUI as sg
def interface_1(a,b):
event, values = sg.Window(a, [[sg.Text('Interface')],[sg.Text(b)], [sg.Input()], [sg.OK(), sg.Cancel()] ], size=(600,110)).read(close=True)
print(values[0])
return values[0]
def interface_2(a):
event, values = sg.Window('Interface', [[sg.Text(a)], [sg.Input()], [sg.OK(), sg.Cancel()] ],size=(600,110) ).read(close=True)
return
all_rooms = [game_room,bedroom_1,bedroom_2,living_room, outside]
all_doors = [door_a,door_b,door_c,door_d]
# define which items/rooms are related
object_relations = {
"game room": [couch, piano, door_a],
"piano": [key_a],
"door a": [game_room, bedroom_1],
"bedroom 1": [door_a,door_b,door_c,queen_bed],
"queen bed":[key_b],
"door b":[bedroom_1,bedroom_2],
"bedroom 2":[door_b,double_bed,dresser],
"dresser":[key_d],
"double bed":[key_c],
"door c":[bedroom_1,living_room],
"living room":[door_c,dining_table,door_d],
"door d":[living_room,outside]
}
# Question for each key
def QA1():
answer1=interface_1("Please enter a or b or c:", "Which country does not have the same name of its capital? \n a) Finland b) Monaco c) Kuwait \n Please enter a or b or c:").lower()
if answer1 == "a":
interface_2("CONGRATULATION, you won the key")
elif answer1=="b":
interface_2("Try Agian")
return QA1()
elif answer1=="C":
interface_2("Try Agian")
return QA1()
else:
interface_2("Your answer should be a or b or c")
return QA1()
def QA2():
answer2=interface_1("Please enter a or b or c:", "What is the capital of Lebanon? \n a) Beirut b) Jbeil c) Tripoly \n Please enter a or b or c:").lower()
if answer2 == "a":
interface_2("CONGRATULATION, you won the key")
elif answer2=="b":
interface_2("Try Agian")
return QA2()
elif answer2=="C":
interface_2("Try Agian")
return QA2()
else:
interface_2("Your answer should be a or b or c")
return QA2()
def QA3():
answer3=interface_1("Please enter a or b or c:", "Luanda is the capita of which country ? \n a) Angola b) Panama c) Singapore \n Please enter a or b or c:").lower()
if answer3 == "a":
interface_2("CONGRATULATION, you won the key")
elif answer3=="b":
interface_2("Try Agian")
return QA3()
elif answer3=="C":
interface_2("Try Agian")
return QA3()
else:
interface_2("Your answer should be a or b or c")
return QA3()
def QA4():
answer4=interface_1("Please enter a or b or c:", "What is the largest city in portugal by population ? \n a) Lisbon b) Braga c) Tripoly \n Please enter a or b or c:").lower()
if answer4 == "a":
interface_2("CONGRATULATION, you won the key")
elif answer4=="b":
interface_2("Try Agian")
return QA4()
elif answer4=="C":
interface_2("Try Agian")
return QA4()
else:
interface_2("Your answer should be a or b or c")
return QA4()
# Questions dict and variables
question = {"Which character was first played by Arnold Schwarzenegger in a 1984 film?":
{"A": "A: The Demonstrator", "B": "B: The Instigator", "C": "C: The Investigator", "D": "D: The Terminator", "correct": "D"},
'what name is given to the person who traditionally attends the groom on his wedding day?': {"A": "A: Best man", "B": "B: Top man", "C": "Old man", "D": "Poor man", "correct": "A"},
"California has alomost the same population as...": {"A": "A: The United Kingdom", "B": "B: Spain", "C": "C: Italy", "D": "D: Poland", "correct": "D"},"Which is a chain of international hotels?": {"A": "A: Four Tops","B":"B: Four Pennies","C": "C: Four Seasons","D":"D:Four Posters","correct": "C"} }
question_presented = 0
options = 0
correct_answer = 0
# define game state. Do not directly change this dict.
# Instead, when a new game starts, make a copy of this
# dict and use the copy to store gameplay state. This
# way you can replay the game multiple times.
INIT_GAME_STATE = {
"current_room": game_room,
"keys_collected": [],
"target_room": outside,
"lives": 5
}
# define variable game_state
game_state={"current_room":game_room}
def linebreak():
"""
Print a line break
"""
print("\n\n")
def start_game():
"""
Start the game
"""
interface_2("You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. \n You don't remember why you are here and what had happened before. You feel some unknown danger is approaching \n and you must get out of the house, NOW! \n Press Ok")
#print("You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. You don't remember why you are here and what had happened before. You feel some unknown danger is approaching and you must get out of the house, NOW!")
play_room(game_state["current_room"])
def play_room(room):
"""
Play a room. First check if the room being played is the target room.
If it is, the game will end with success. Otherwise, let player either
explore (list all items in this room) or examine an item found here.
"""
game_state["current_room"] = room
if(game_state["current_room"] == game_state["target_room"]):
interface_2("Congrats! You escaped the room!")
#print("Congrats! You escaped the room!")
else:
interface_2("You are now in " + room["name"] +"\n Press OK")
#print("You are now in " + room["name"])
intended_action =interface_1("What would you like to do? Type 'explore' or 'examine'?","What would you like to do? Type 'explore' or 'examine'?").strip()
#intended_action = input("What would you like to do? Type 'explore' or 'examine'?").strip()
linebreak()
if intended_action == "explore":
explore_room(room)
play_room(room)
elif intended_action == "examine":
examine_item(interface_1("What would you like to examine?","What would you like to examine?").strip())
#examine_item(input("What would you like to examine?").strip())
linebreak()
else:
interface_2("Not sure what you mean. Type 'explore' or 'examine'. \n Press OK")
# print("Not sure what you mean. Type 'explore' or 'examine'.")
linebreak()
play_room(room)
linebreak()
def explore_room(room):
"""
Explore a room. List all items belonging to this room.
"""
items = [i["name"] for i in object_relations[room["name"]]]
interface_2("You explore the room. This is " + room["name"] + ". You find " + ", ".join(items)+"\n Press OK")
#print("You explore the room. This is " + room["name"] + ". You find " + ", ".join(items))
linebreak()
def get_next_room_of_door(door, current_room):
"""
From object_relations, find the two rooms connected to the given door.
Return the room that is not the current_room.
"""
connected_rooms = object_relations[door["name"]]
for room in connected_rooms:
if(not current_room == room):
return room
def examine_item(item_name):
"""
Examine an item which can be a door or furniture.
First make sure the intended item belongs to the current room.
Then check if the item is a door. Tell player if key hasn't been
collected yet. Otherwise ask player if they want to go to the next
room. If the item is not a door, then check if it contains keys.
Collect the key if found and update the game state. At the end,
play either the current or the next room depending on the game state
to keep playing.
"""
current_room = game_state["current_room"]
next_room = ""
output = None
for item in object_relations[current_room["name"]]:
if(item["name"] == item_name):
interface_2("You examine " + item_name + ". \n Press ok")
output = "You examine " + item_name + ". "
if(item["type"] == "door"):
have_key = False
for key in game_state["keys_collected"]:
if(key["target"] == item):
have_key = True
if(have_key):
give_question = randomize_question()
interface_2("You unlock it with a key you have. \n Press ok")
output += "You unlock it with a key you have."
next_room = get_next_room_of_door(item, current_room)
else:
interface_2("It is locked but you don't have the key. \n Press ok")
output += "It is locked but you don't have the key."
else:
if(item["name"] in object_relations and len(object_relations[item["name"]])>0):
if item["name"]=="piano":
QA1()
elif item["name"]=="queen bed":
QA2()
elif item["name"]=="dresser":
QA3()
else:
QA4()
item_found = object_relations[item["name"]].pop()
game_state["keys_collected"].append(item_found)
interface_2("You find " + item_found["name"] + "."+"Press OK")
output += "You find " + item_found["name"] + "."
else:
interface_2("There isn't anything interesting about it.")
output += "There isn't anything interesting about it."
print(output)
break
if(output is None):
interface_2("The item you requested is not found in the current room. \n Press Ok")
#print("The item you requested is not found in the current room.")
linebreak()
if(next_room and interface_1("Do you want to go to " + next_room['name'] + "? Enter 'yes' or 'no'","Do you want to go to " + next_room['name'] + "? Enter 'yes' or 'no'").strip() == 'yes'):
play_room(next_room)
else:
play_room(current_room)
def answer_question ():
while game_state['lives']> 0:
interface_2("You have the key! But don't get ahead of yourself, you have to answer a little question first!")
a = interface_1(question_presented + "\n" + options[0] + " " + options[1] + " " + options[2] + " " + options[3] + " answer A, B, C or D",question_presented + "\n" + options[0] + " " + options[1] + " " + options[2] + " " + options[3] + " answer A, B, C or D")
if a == correct_answer:
interface_2("That's correct, the door has unlocked!")
break
elif a != correct_answer:
game_state['lives'] -= 1
interface_2("Wrong! Try again!")
interface_2("you have " + str(game_state["lives"]) + " lives left!")
randomize_question()
else:
interface_2("wrong answer, type A, B, C or D")
answer_question()
if game_state['lives'] == 0:
interface_2('You have lost the game!!!! Better luck next time')
sys.exit()
def randomize_question():
global question_presented
global options
global correct_answer
question_presented = random.choice(list(question.keys()))
options = [i for i in question[question_presented].values()][:-1]
correct_answer = question[question_presented]['correct']
input_question = answer_question()
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
time.sleep(1)
t -= 1
print("The bomb goes off!! GAME OVER!!")
game_state = INIT_GAME_STATE.copy()
if __name__ == '__main__':
p1 = Process(target=countdown, args=(300,))
p2 = Process(target=start_game)
p1.start()
p2.start()
p1.join()
if p1.is_alive() == False:
p2.terminate()
# while game_state['lives']>0:
# start_game()