-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHangman.py
591 lines (479 loc) · 20.7 KB
/
Hangman.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
import pygame
import sys
import random
import time
import re
from circuit import *
from pygame.locals import *
from qiskit.visualization import plot_histogram
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
GREY = (200,200,200)
LEFT_CLICK = (1,0,0)
RIGHT_CLICK = (0,0,1)
pygame.init()
pygame.display.set_caption("PROJECT FOR QCHACK")
bg = 'logo.png'
bg = pygame.image.load(bg)
bg = pygame.transform.scale(bg, (500, 600))
gates = ['X0', 'Y0', 'Z0', 'H0', 'C01', 'X1', 'Y1', 'Z1', 'H1', 'C10']
gateslist=['X0','H1']
#32 bit display
Display = pygame.display.set_mode((500,600),0,32)
Easy = ['State','Shor', 'Pauli','Quantum','Computing']
Medium = ['Qiskit','Simulation','Entanglement','Superposition','Interference']
Hard = ['Statevector','Toffolli','Hadamard','Superconductivity','Microwave']
Color = ["BLACK","WHITE","RED","GREEN","BLUE","GREY"]
Font = pygame.font.Font("freesansbold.ttf",30)
Font2 = pygame.font.Font("freesansbold.ttf",20)
font = pygame.font.Font("freesansbold.ttf", 20)
Display.fill(WHITE)
display_width = 500
display_height = 600
def randomNum(choice):
RandomNum = 0
if choice == 1:
RandomNum = random.randint(0,len(Easy)-1)
elif choice == 2:
RandomNum = random.randint(0,len(Medium)-1)
elif choice == 3:
RandomNum = random.randint(0,len(Hard)-1)
elif choice == 4:
RandomNum = random.randint(0,len(Color)-1)# as elements of color is 0,1,2,3,4
return RandomNum
def List(number,choice):
if (choice == 1):
Word = Easy[number]
elif (choice == 2):
Word = Medium[number]
elif (choice == 3):
Word = Hard[number]
elif (choice == 4):
Word = Color[number]
return Word
def Hangman(condition):
if (condition == 0):
img = pygame.image.load('initial.png')
img = pygame.transform.scale(img, (350, 150))
Display.blit(img, (50,380))
else:
img = pygame.image.load('current.png')
img = pygame.transform.scale(img, (350, 150))
img2 = pygame.image.load('histogram.png')
img2 = pygame.transform.scale(img2, (300, 280))
Display.blit(img2, (100,30))
Display.blit(img, (50,380))#baseline
def PreHangMan():
qc=ansatz(gateslist)
plot_histogram(get_expectation(gateslist)).savefig('histogram.png')
qc.draw('mpl').savefig('initial.png')
qc.draw('mpl').savefig('current.png')
img = pygame.image.load('initial.png')
img2 = pygame.image.load('histogram.png')
img3=pygame.image.load('catstarting.png')
img = pygame.transform.scale(img, (150, 150))
img2 = pygame.transform.scale(img2, (300, 280))
img3 = pygame.transform.scale(img3, (80, 100))
Display.blit(img2, (150,320))
Display.blit(img, (50,180))
Display.blit(img3,(50,400))
def StartScreen():
Display.blit(pygame.font.Font("freesansbold.ttf",30).render("Rescue Schrondinger's cat",True,BLACK), (20,20))
Display.blit(Font2.render("For the QcHACK2021",True,BLACK), (60,60))
Display.blit(Font.render("Level Difficulty",True,BLACK), (200,150))
Display.blit(Font2.render("1- Easy",True,BLACK), (200,200))
Display.blit(Font2.render("2- Medium",True,BLACK), (200,250))
Display.blit(Font2.render("3- Hard",True,BLACK), (200,300))
class Button(object):
def __init__(self, text, color, x=None, y=None, **kwargs):
self.surface = font.render(text, True, color)
self.WIDTH = self.surface.get_width()
self.HEIGHT = self.surface.get_height()
if 'centered_x' in kwargs and kwargs['centered_x']:
self.x = display_width // 2 - self.WIDTH // 2
else:
self.x = x
if 'centered_y' in kwargs and kwargs['cenntered_y']:
self.y = display_height // 2 - self.HEIGHT // 2
else:
self.y = y
def display(self):
Display.blit(self.surface, (self.x, self.y))
def check_click(self, position):
x_match = position[0] > self.x and position[0] < self.x + self.WIDTH
y_match = position[1] > self.y and position[1] < self.y + self.HEIGHT
if x_match and y_match:
return True
else:
return False
def gate_choose():
Display.fill(WHITE)
img = pygame.image.load('current.png')
img = pygame.transform.scale(img, (400, 200))
Display.blit(img, (50, 20))
img2 = pygame.image.load('histogram.png')
img2 = pygame.transform.scale(img2, (500, 200))
Display.blit(img2, (500, 20))
pygame.display.update() # update the display
game_title = font.render('Which gate would you like to choose?', True, RED)
Display.blit(game_title, (display_width // 2 - game_title.get_width() // 2, 250))
left = display_width//3
pos2=left+100
H_button = Button('H', WHITE, left, 50)
X_button = Button('X', WHITE, left, 400)
Y_button = Button('Y', WHITE, left, 450)
if gateslist[-1][0]!='Z':
Z_button = Button('Z', WHITE, pos2, 350)
CNOT1_button = Button('CX10', WHITE, pos2, 400)
CNOT_button = Button('CX01', WHITE, pos2, 450)
DONE_button = Button('DONE', WHITE, pos2, 500)
H_button.display()
X_button.display()
if gateslist[-1][0]!='Z':
Z_button.display()
Y_button.display()
CNOT1_button.display()
CNOT_button.display()
DONE_button.display()
pygame.display.update()
while True:
if H_button.check_click(pygame.mouse.get_pos()):
H_button = Button('H', RED, left, 350)
else:
H_button = Button('H', BLACK, left, 350)
if X_button.check_click(pygame.mouse.get_pos()):
X_button = Button('X', RED, left, 400)
else:
X_button = Button('X', BLACK, left, 400)
if Y_button.check_click(pygame.mouse.get_pos()):
Y_button = Button('Y', RED, left, 450)
else:
Y_button = Button('Y', BLACK, left, 450)
if gateslist[-1][0]!='Z':
if Z_button.check_click(pygame.mouse.get_pos()):
Z_button = Button('Z', RED, pos2, 350)
else:
Z_button = Button('Z', BLACK, pos2, 350)
if CNOT1_button.check_click(pygame.mouse.get_pos()):
CNOT1_button = Button('CX10', RED, pos2, 400)
else:
CNOT1_button = Button('CX10', BLACK, pos2, 400)
if CNOT_button.check_click(pygame.mouse.get_pos()):
CNOT_button = Button('CX01', RED, pos2, 450)
else:
CNOT_button = Button('CX01', BLACK, pos2, 450)
if DONE_button.check_click(pygame.mouse.get_pos()):
DONE_button = Button('DONE', BLACK, pos2, 500)
else:
DONE_button = Button('DONE', BLACK, pos2, 500)
H_button.display()
X_button.display()
Y_button.display()
if gateslist[-1][0]!='Z':
Z_button.display()
CNOT1_button.display()
CNOT_button.display()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
raise SystemExit
if pygame.mouse.get_pressed()[0]:
if H_button.check_click(pygame.mouse.get_pos()):
return 'H'
break
if X_button.check_click(pygame.mouse.get_pos()):
return "X"
break
if Y_button.check_click(pygame.mouse.get_pos()):
return "Y"
break
if gateslist[-1][0]!='Z':
if Z_button.check_click(pygame.mouse.get_pos()):
return "Z"
break
if CNOT1_button.check_click(pygame.mouse.get_pos()):
return "C10"
break
if CNOT_button.check_click(pygame.mouse.get_pos()):
return "C01"
break
def qubit_choose():
Display.fill(WHITE)
game_title = font.render('Which qubit would you like to apply that gate on?', True, RED)
Display.blit(game_title, (display_width // 2 - game_title.get_width() // 2, 250))
zero_button = Button('0', BLACK, None, 350, centered_x=True)
one_button = Button('1', BLACK, None, 400, centered_x=True)
zero_button.display()
one_button.display()
pygame.display.update()
while True:
if zero_button.check_click(pygame.mouse.get_pos()):
zero_button = Button('0', RED, None, 350, centered_x=True)
else:
zero_button = Button('0', BLACK, None, 350, centered_x=True)
if one_button.check_click(pygame.mouse.get_pos()):
one_button = Button('1', RED, None, 400, centered_x=True)
else:
one_button = Button('1', BLACK, None, 400, centered_x=True)
zero_button.display()
one_button.display()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
raise SystemExit
if pygame.mouse.get_pressed()[0]:
if zero_button.check_click(pygame.mouse.get_pos()):
return '0'
break
if one_button.check_click(pygame.mouse.get_pos()):
return '1'
break
def main():
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
TheChoice = 0
StartScreen()
PreHangMan()
FirstCondi = True
while FirstCondi:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
#print(FirstCondi)
if (event.key == K_1) or (event.key == 257):#257 is 1 in numpad
TheChoice = 1
FirstCondi = False
break
elif event.key == K_2 or event.key == 258:#258 is 2 in numpad
TheChoice = 2
FirstCondi = False
break
elif event.key == K_3 or event.key == 259:#259 is 3 in numpad
TheChoice = 3
FirstCondi = False
break
elif event.key == K_4 or event.key == 260:#260 is 4 in numpad
TheChoice = 4
FirstCondi = False
break
elif event.type == MOUSEBUTTONDOWN:
#Easy
if (pygame.mouse.get_pos()[0] > 200 and\
pygame.mouse.get_pos()[1] > 200 and\
pygame.mouse.get_pos()[0] < 265 and\
pygame.mouse.get_pos()[1] < 215):
TheChoice = 1
FirstCondi = False
break
#Medium
elif (pygame.mouse.get_pos()[0] > 200 and\
pygame.mouse.get_pos()[1] > 250 and\
pygame.mouse.get_pos()[0] < 295 and\
pygame.mouse.get_pos()[1] < 265):
TheChoice = 2
FirstCondi = False
break
#Hard
elif (pygame.mouse.get_pos()[0] > 200 and\
pygame.mouse.get_pos()[1] > 300 and\
pygame.mouse.get_pos()[0] < 265 and\
pygame.mouse.get_pos()[1] < 315):
TheChoice = 3
FirstCondi = False
break
#Color
elif (pygame.mouse.get_pos()[0] > 200 and\
pygame.mouse.get_pos()[1] > 350 and\
pygame.mouse.get_pos()[0] < 270 and\
pygame.mouse.get_pos()[1] < 365):
TheChoice = 4
FirstCondi = False
break
if (TheChoice!= 0):
Display.fill(WHITE)
pygame.display.update()
pygame.time.Clock().tick(30) #30fps
#This is to make sure the word and random number is constant
TheNum = randomNum(TheChoice)
TheWord = List(TheNum, TheChoice)
#TheWord = "wew"
#This is to check if it got the word from the list
#print(TheWord)
EmptyList = []
for i in range(len(TheWord)):
EmptyList.append('-')
#print(EmptyList)
#print("".join(EmptyList))
Hidden = Font.render("".join(EmptyList),True,BLACK)
HiddenRect = Hidden.get_rect()
HiddenRect.center = (250,350)
Display.blit(Hidden,HiddenRect)
Condition = 0;#if condition is 10, then end game
Off = 0 #quit game at game over and congrats
TheTime = 0
Start = time.time() #current time
Display.blit(Font2.render("Time(s):",True,BLACK),(300,10))
LastKeyPressed = ""
Display.blit(pygame.font.Font("freesansbold.ttf",15).render("Press 0 to quit game",True,BLACK),(20,10))
while True:
Hangman(Condition)
finishgame=True
End = time.time() #end time
if (int(End) - int(Start) == 1):
pygame.draw.rect(Display,WHITE,(385,0,100,50)) #hide time
TheTime = TheTime + 1
#print(TheTime, 'seconds has passed')
Timer = Font2.render(str(TheTime),True,BLACK)
Display.blit(Timer, (400,10))
Start = time.time()
for event in pygame.event.get():
#mouse
#print(pygame.mouse.get_pos())
#print(pygame.mouse.get_pressed())
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
#if exit key is pressed
LastKeyPressed = event.key
pygame.draw.rect(Display,WHITE,(200,300,250,100)) #hide word
pygame.draw.rect(Display,WHITE,(220,150,200,100)) # hide invalid input
UserInput = event.key
#print(chr(event.key))
#This is use to check if it changes the ASCII int value to the char
if re.search("[a-z]",chr(event.key)):
if ((chr(event.key).upper() in TheWord) or (chr(event.key).lower() in TheWord)):
for i in range(len(TheWord)):
if ((TheWord[i] == (chr(event.key)).upper()) or (TheWord[i] == (chr(event.key)).lower())):
EmptyList[i] = TheWord[i]
else:
Condition = Condition + 1
gate_selected=gate_choose()
qubit_selected=qubit_choose()
gateslist.append(gate_selected+qubit_selected)
qc=ansatz(gateslist)
qc.draw('mpl').savefig('current.png')
plot_histogram(get_expectation(gateslist)).savefig('histogram.png')
finishgame=comparison(gateslist)
Display.fill(WHITE)
Hidden = Font.render("".join(EmptyList),True,BLACK)
HiddenRect = Hidden.get_rect()
HiddenRect.center = (250,350)
Display.blit(Hidden,HiddenRect)
else:
if (event.key == K_0 or event.key == 256):#256 is 0 in numpad
Display.blit(Font.render("EXIT?",True,RED),(340,220))
Display.blit(Font2.render("Yes",True,BLUE),(340,270))
Display.blit(Font2.render("No",True,BLUE),(415,270))
else:
Input = Font2.render("INVALID INPUT!!!",True,RED)
InputRect = Input.get_rect()
InputRect.center = (350,100)
Display.blit(Input, InputRect)
Display.blit(Hidden,HiddenRect)
elif event.type == KEYUP:
pygame.draw.rect(Display,WHITE,(260,50,300,100)) # hide invalid input
elif event.type == MOUSEBUTTONDOWN:
#print("mouse pressed")
#print(pygame.mouse.get_pressed())
if (LastKeyPressed == K_0 or LastKeyPressed == 256):#256 is 0 in numpad
if (pygame.mouse.get_pressed() == LEFT_CLICK):
#Yes
if (pygame.mouse.get_pos()[0] > 340 and\
pygame.mouse.get_pos()[1] > 270 and\
pygame.mouse.get_pos()[0] < 385 and\
pygame.mouse.get_pos()[1] < 285):
pygame.draw.rect(Display,WHITE,(340,270,35,25)) #hide yes
Display.blit(Font2.render("Yes",True,GREEN),(340,270))
#print(pygame.mouse.get_pos())
#print("YES")
#NO
elif (pygame.mouse.get_pos()[0] > 415 and\
pygame.mouse.get_pos()[1] > 270 and\
pygame.mouse.get_pos()[0] < 450 and\
pygame.mouse.get_pos()[1] < 285):
pygame.draw.rect(Display,WHITE,(415,270,35,25)) #hide no
Display.blit(Font2.render("No",True,GREEN),(415,270))
#print(pygame.mouse.get_pos())
#print("NO")
elif event.type == MOUSEBUTTONUP:#Yes
if (LastKeyPressed == K_0 or LastKeyPressed == 256):#256 is 0 in numpad
#Yes
if (pygame.mouse.get_pos()[0] > 340 and\
pygame.mouse.get_pos()[1] > 270 and\
pygame.mouse.get_pos()[0] < 385 and\
pygame.mouse.get_pos()[1] < 285):
#quit game
pygame.quit()
sys.exit()
#No
elif (pygame.mouse.get_pos()[0] > 415 and\
pygame.mouse.get_pos()[1] > 270 and\
pygame.mouse.get_pos()[0] < 450 and\
pygame.mouse.get_pos()[1] < 285):
pygame.draw.rect(Display,WHITE,(415,270,35,25)) #hide no
Display.blit(Font2.render("No",True,GREEN),(415,270))
pygame.draw.rect(Display,WHITE,(300,200,200,100)) #hide exit, yes,no
Hidden = Font.render("".join(EmptyList),True,BLACK)
HiddenRect = Hidden.get_rect()
HiddenRect.center = (250,350)
Display.blit(Hidden,HiddenRect)
LastKeyPressed = ""
else:
pygame.draw.rect(Display,WHITE,(340,270,35,25)) #hide yes
Display.blit(Font2.render("Yes",True,BLUE),(340,270))
pygame.draw.rect(Display,WHITE,(415,270,35,25)) #hide no
Display.blit(Font2.render("No",True,BLUE),(415,270))
#Check if the word is word and the condition
if not finishgame:
Display.fill(WHITE)
Hangman(Condition+1)
Over = Font2.render("GAME OVER!!! the cat is Dead",True,RED)
OverRect = Over.get_rect()
OverRect.center = (250,350)
Display.blit(Over,OverRect)
img2 = pygame.image.load('catdead.png')
img2 = pygame.transform.scale(img2, (160, 120))
Display.blit(img2, (10,300))
Off = 1
#stop the BGM
elif (TheWord == "".join(EmptyList)):
Display.fill(WHITE)
Cong = Font.render("CONGRATS!!! You saved the cat",True,GREEN)
CongRect = Cong.get_rect()
CongRect.center = (250,150)
Display.blit(Cong,CongRect)
Word = Font2.render("The word is:",True,BLACK)
WordRect = Word.get_rect()
WordRect.center = (250,250)
Display.blit(Word,WordRect)
Word2 = Font.render(TheWord,True,BLACK)
Word2Rect = Word2.get_rect()
Word2Rect.center = (250,285)
Display.blit(Word2,Word2Rect)
img2 = pygame.image.load('catwinning.png')
img2 = pygame.transform.scale(img2, (300, 280))
Display.blit(img2, (150,320))
Off = 1
#stop the BGM
#pygame.mixer.music.load('Music\KM\Loping Sting.mp3')
#pygame.mixer.music.play(0,0)
pygame.display.update()
pygame.time.Clock().tick(30) #30fps
if (Off == 1):
#wait 5 seconds
time.sleep(5)
#quit game
pygame.quit()
sys.exit()
main()