-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViews.py
319 lines (278 loc) · 19.4 KB
/
Views.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
import pygame as pg
import Circuit_Logic as cl
import ctypes
import numpy
import math
from numpy import array
import itertools
class renderer():
colordict = {
"white" : (255, 255, 255),
"black" : (0, 0, 0),
"grey" : (150, 150, 150),
"light grey" : (200,200,200),
"blue" : (0, 137, 255),
"red" : (255, 50, 50),
"yellow" : (240, 240, 50),
"purple" : (138,43,226),
"pink" : (255,105,180),
"dark green" : (50,205,50)
}
gatedict = {
"H" : colordict["blue"],
"X" : colordict["red"],
"T" : colordict["yellow"],
"Z" : colordict["purple"],
"S" : colordict["pink"],
"if" : colordict["black"],
"SWAP" : colordict["dark green"]
}
def __init__(self):
ctypes.windll.user32.SetProcessDPIAware()
# Initialise pygame
pg.init()
# Initialise l'affichage
self.display = pg.display.set_mode((1920, 1080))
pg.display.set_caption('QPuzzler')
self.disp_Width = self.display.get_width()
self.disp_Height = self.display.get_height()
self.fps_Limiter = pg.time.Clock()
# Initialise la police de texte
pg.font.init()
# Les couleurs que nous allons utiliser
self.colordict = {
"white" : (255, 255, 255),
"black" : (0, 0, 0),
"grey" : (150, 150, 150),
"light grey" : (200,200,200),
"blue" : (0, 137, 255),
"red" : (255, 50, 50),
"yellow" : (240, 240, 50),
"purple" : (138,43,226),
"pink" : (255,105,180),
"dark green" : (50,205,50)
}
# Assigne des couleurs au gates
self.gatedict = {
"H" : self.colordict["blue"],
"X" : self.colordict["red"],
"T" : self.colordict["yellow"],
"Z" : self.colordict["purple"],
"S" : self.colordict["pink"],
"if" : self.colordict["black"],
"SWAP" : self.colordict["dark green"]
}
normal_Font = pg.font.Font("square.ttf", 48)
menu_Font = pg.font.Font("square.ttf", 192)
self.fontdict = {
"menu" : menu_Font,
"normal" : normal_Font
}
def draw_text(self, display, text, color, font, X, Y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.center = (X, Y)
display.blit(textobj, textrect)
def draw_quantum_state(self, state, x, y, vertically = True, background = colordict["white"]): #used to represent on screen the state of a qbit
new_surface = pg.Surface((42*(len(state)+1),60))
new_surface.fill(background)
pg.draw.line(new_surface,self.colordict["black"], (5,5), (5,55), 2)
pg.draw.line(new_surface,self.colordict["black"], (42*(len(state)+1)-5,5), (42*(len(state)+1)-5,55), 2)
pg.draw.line(new_surface,self.colordict["black"], (5,5), (32,5), 2)
pg.draw.line(new_surface,self.colordict["black"], (5,55), (32,55), 2)
pg.draw.line(new_surface,self.colordict["black"], (42*(len(state)+1)-5,5), (42*(len(state)+1)-30,5), 2)
pg.draw.line(new_surface,self.colordict["black"], (42*(len(state)+1)-5,55), (42*(len(state)+1)-30,55), 2)
if vertically:
new_surface = pg.transform.rotate(new_surface,-90)
for state_index in range(len(state)):
arrow_vector = (math.copysign(numpy.real(state[state_index])**2, numpy.real(state[state_index])), math.copysign(numpy.imag(state[state_index])**2, numpy.imag(state[state_index])))
if vertically:
arrow = ((30 + 20*arrow_vector[0], 42*(state_index+1) - 20*arrow_vector[1]),(30 - 20*arrow_vector[0], 42*(state_index+1) + 20*arrow_vector[1]))
else:
arrow = ((42*(state_index+1) + 20*arrow_vector[0], 30 - 20*arrow_vector[1]),(42*(state_index+1) - 20*arrow_vector[0], 30 + 20*arrow_vector[1]))
pg.draw.line(new_surface,self.colordict["black"],arrow[0], arrow[1], 3)
pg.draw.circle(new_surface,self.colordict["black"], arrow[0], 5)
rect = new_surface.get_rect()
rect.center = (x,y)
self.display.blit(new_surface, rect)
def draw_gate(self, level, gate, held_rectangle):
if isinstance(gate, cl.I_Gate):
pass
elif isinstance(gate,cl.Conditional_Gate):
pg.draw.rect(self.display, self.colordict["grey"], gate.rectangle, 10)
self.draw_text(self.display, str(gate), self.colordict["grey"], self.fontdict["normal"], gate.rectangle.centerx, gate.rectangle.centery)
if gate.current_Track or gate is held_rectangle:
pg.draw.rect(self.display, self.colordict["grey"], gate.aux_rectangle, 10)
if gate.rectangle.y < gate.aux_rectangle.y:
pg.draw.line(self.display,self.colordict["grey"], gate.rectangle.midbottom, gate.aux_rectangle.midtop, 10)
else:
pg.draw.line(self.display,self.colordict["grey"], gate.rectangle.midtop, gate.aux_rectangle.midbottom, 10)
elif isinstance(gate, cl.SWAP_Gate):
pg.draw.rect(self.display, self.gatedict[str(gate)], gate.rectangle)
pg.draw.rect(self.display, self.colordict["black"], gate.rectangle, 10)
self.draw_text(self.display, str(gate), self.colordict["black"], self.fontdict["normal"], gate.rectangle.centerx, gate.rectangle.centery)
if gate is held_rectangle or not gate in level.available_gates:
pg.draw.line(self.display, self.colordict["black"], gate.rectangle.midbottom, gate.aux_gate.rectangle.midtop, 10)
if gate is held_rectangle:
pg.draw.rect(self.display, self.gatedict[str(gate)], gate.aux_gate.rectangle)
pg.draw.rect(self.display, self.colordict["black"], gate.aux_gate.rectangle, 10)
self.draw_text(self.display, str(gate), self.colordict["black"], self.fontdict["normal"], gate.aux_gate.rectangle.centerx, gate.aux_gate.rectangle.centery)
else:
pg.draw.rect(self.display, self.gatedict[str(gate)], gate.rectangle)
pg.draw.rect(self.display, self.colordict["black"], gate.rectangle, 10)
self.draw_text(self.display, str(gate), self.colordict["black"], self.fontdict["normal"], gate.rectangle.centerx, gate.rectangle.centery)
if gate in level.available_gates and not gate is held_rectangle:
self.draw_text(self.display, str(gate.cost), self.colordict["black"], self.fontdict["normal"], gate.rectangle.centerx, gate.rectangle.centery + 100)
def main_menu_view(self, main_Menu_Buttons):
self.fps_Limiter.tick(60)
# Tout ce qui doit être affiché
self.display.fill(self.colordict["white"])
for button in main_Menu_Buttons:
pg.draw.rect(self.display, self.colordict["black"], button, 10) # La 4e donnée remplace le rectangle rempli par une bordure
self.draw_text(self.display, "QPUZZLER", self.colordict["black"], self.fontdict["menu"], self.disp_Width/2, self.disp_Height/5)
self.draw_text(self.display, "SELECTION NIVEAU", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, 3*self.disp_Height/6)
self.draw_text(self.display, "OPTIONS", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, 4*self.disp_Height/6)
self.draw_text(self.display, "SORTIE", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, 5*self.disp_Height/6)
pg.display.update()
def level_select_view(self, levels, level_Select_Buttons, chosen_level, level_starters):
self.fps_Limiter.tick(60)
# Tout ce qui doit être affiché
self.display.fill(self.colordict["white"])
for button in level_Select_Buttons:
pg.draw.rect(self.display, self.colordict["black"], button, 10) # La 4e donnée remplace le rectangle rempli par une bordure
for button in level_starters:
if button is chosen_level:
pg.draw.rect(self.display, self.colordict["red"], button, 10)
else: pg.draw.rect(self.display, self.colordict["black"], button, 10)
self.draw_text(self.display, "RETOUR", self.colordict["black"], self.fontdict["normal"], self.disp_Width/5, 7*self.disp_Height/8)
self.draw_text(self.display, "ACCEPTER", self.colordict["black"], self.fontdict["normal"], 4*self.disp_Width/5, 7* self.disp_Height/8)
i = 0
j = 0
for x in range(len(levels)):
if level_starters[x] is chosen_level:
self.draw_text(self.display, levels[x].name, self.colordict["red"], self.fontdict["normal"], self.disp_Width/5 + (240*i), self.disp_Height/4 + (240*j))
else: self.draw_text(self.display, levels[x].name, self.colordict["black"], self.fontdict["normal"], self.disp_Width/5 + (240*i), self.disp_Height/4 + (240*j))
i += 1
if i > 5:
i = 0
j += 1
pg.display.update()
def options_menu_view(self, option_Buttons):
self.fps_Limiter.tick(60)
# Tout ce qui doit être affiché
self.display.fill(self.colordict["white"])
for button in option_Buttons:
pg.draw.rect(self.display, self.colordict["black"], button, 10) # La 4e donnée remplace le rectangle rempli par une bordure
self.draw_text(self.display, "OPTIONS", self.colordict["black"], self.fontdict["menu"], self.disp_Width/2, self.disp_Height/5)
self.draw_text(self.display, "AIDE", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, self.disp_Height/2)
self.draw_text(self.display, "RETOUR", self.colordict["black"], self.fontdict["normal"], self.disp_Width/5, 7*self.disp_Height/8)
self.draw_text(self.display, "ACCEPTER", self.colordict["black"], self.fontdict["normal"], 4*self.disp_Width/5, 7*self.disp_Height/8)
pg.display.update()
def help_screen_view(self, help_Buttons, chosen_button_text):
self.fps_Limiter.tick(60)
#render section
self.display.fill(self.colordict["white"])
for button in help_Buttons:
pg.draw.rect(self.display, self.colordict["black"], button, 10)
self.draw_text(self.display, "", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, self.disp_Height/2)
self.draw_text(self.display, "RETOUR", self.colordict["black"], self.fontdict["normal"], 210, self.disp_Height-60)
self.draw_text(self.display, "QUANTUM BIT", self.colordict["black"], self.fontdict["normal"], 210, 60)
self.draw_text(self.display, "PORTE SWAP", self.colordict["black"], self.fontdict["normal"], 210, 180)
self.draw_text(self.display, "PORTE H", self.colordict["black"], self.fontdict["normal"], 210, 300)
self.draw_text(self.display, "PORTE X", self.colordict["black"], self.fontdict["normal"], 210, 420)
self.draw_text(self.display, "PORTE T", self.colordict["black"], self.fontdict["normal"], 210, 540)
self.draw_text(self.display, "PORTE Z", self.colordict["black"], self.fontdict["normal"], 210, 660)
self.draw_text(self.display, "PORTE S", self.colordict["black"], self.fontdict["normal"], 210, 780)
self.draw_text(self.display, "PORTE IF", self.colordict["black"], self.fontdict["normal"], 210, 900)
i = 0
for x in range(len(chosen_button_text)):
self.draw_text(self.display, chosen_button_text[x], self.colordict["black"], self.fontdict["normal"], self.disp_Width/2 + 200, 200 + (100*i))
i += 1
pg.display.update()
def level_view(self, level, held_rectangle):
self.fps_Limiter.tick(60)
first_layer_elements = []
self.display.fill(self.colordict["white"])
pg.draw.rect(self.display, self.colordict["white"], pg.Rect(10, 10, 400, self.disp_Height-240))
pg.draw.rect(self.display, self.colordict["black"], pg.Rect(10, 10, 400, self.disp_Height-240), 10)
self.draw_text(self.display, level.name, self.colordict["black"], self.fontdict["normal"], 210, 60)
for track in level.tracks:
self.draw_quantum_state(track.input[0].state, 210, track.rectangle.centery)
self.draw_text(self.display, str(level.total_Cost), self.colordict["black"], self.fontdict["normal"], 210, self.disp_Height - 280)
self.draw_text(self.display, level.goal_text, self.colordict["black"], self.fontdict["normal"], self.disp_Width/2 + 200, self.disp_Height - 280)
pg.draw.rect(self.display, self.colordict["white"], pg.Rect(10, self.disp_Height-210, self.disp_Width-340, 200))
pg.draw.rect(self.display, self.colordict["black"], pg.Rect(10, self.disp_Height-210, self.disp_Width-340, 200), 10)
pg.draw.rect(self.display, self.colordict["grey"], pg.Rect(430, 10, 80, 50), 10)
pg.draw.line(self.display, self.colordict["grey"],(470, 20),(470,50), 10)
pg.draw.line(self.display, self.colordict["grey"],(455, 35),(485,35), 10)
pg.draw.rect(self.display, self.colordict["black"], pg.Rect(self.disp_Width - 310, self.disp_Height-100, 300, 90), 10)
pg.draw.rect(self.display, self.colordict["black"], pg.Rect(self.disp_Width - 310, self.disp_Height-210, 300, 90), 10)
self.draw_text(self.display,"executer", self.colordict["black"], self.fontdict["normal"], self.disp_Width-150, self.disp_Height - 55)
self.draw_text(self.display,"aide", self.colordict["black"], self.fontdict["normal"], self.disp_Width-150, self.disp_Height - 159)
for track in level.tracks:
pg.draw.rect(self.display, self.colordict["light grey"], track.rectangle, 10)
for track in level.tracks:
for gate in track.gates:
if isinstance(gate, cl.Conditional_Gate):
first_layer_elements.append(gate)
continue
self.draw_gate(level, gate, held_rectangle)
for gate in level.available_gates:
self.draw_gate(level, gate, held_rectangle)
for gate in first_layer_elements:
self.draw_gate(level, gate, held_rectangle)
pg.display.update()
def level_end_screen_view(self, level, red_squares, test_number):
self.fps_Limiter.tick(60)
background = pg.Rect(self.disp_Width/2, self.disp_Height/2, self.disp_Width/2, self.disp_Height/2)
background.center = (self.disp_Width/2, self.disp_Height/2)
pg.draw.rect(self.display, self.colordict["white"], background)
pg.draw.rect(self.display, self.colordict["black"], background, 10)
self.draw_text(self.display, "initial", self.colordict["black"], self.fontdict["normal"],self.disp_Width/4 + self.disp_Width/8, self.disp_Width/4 + self.disp_Width/7)
self.draw_text(self.display, "bonne reponse", self.colordict["black"], self.fontdict["normal"],self.disp_Width/4 + 2* self.disp_Width/8, self.disp_Width/4 + self.disp_Width/7)
self.draw_text(self.display, "reponse", self.colordict["black"], self.fontdict["normal"],self.disp_Width/4 + 3* self.disp_Width/8, self.disp_Width/4 + self.disp_Width/7)
quantum_states = [level.tracks, level.output, level.snapshots]
for column in range(len(quantum_states)):
for row in range(len(quantum_states[column])):
if hasattr(quantum_states[column][row], "input"):
if red_squares > 0:
self.draw_quantum_state(quantum_states[column][row].input[test_number].state, self.disp_Width/4 + (column+1)* self.disp_Width/8, self.disp_Height/4 + (row+1) * self.disp_Height/(2*(1+len(quantum_states[column]))), vertically = False, background= self.colordict["red"])
red_squares -=5
else:
self.draw_quantum_state(quantum_states[column][row].input[test_number].state, self.disp_Width/4 + (column+1)* self.disp_Width/8, self.disp_Height/4 + (row+1) * self.disp_Height/(2*(1+len(quantum_states[column]))), vertically = False)
else:
if red_squares > 0:
self.draw_quantum_state(quantum_states[test_number], self.disp_Width/4 + (column+1)* self.disp_Width/8, self.disp_Height/4 + (row+1) * self.disp_Height/(2*(1+len(quantum_states[column]))), background = self.colordict["red"])
red_squares -= 5
else:
self.draw_quantum_state(quantum_states[test_number], self.disp_Width/4 + (column+1)* self.disp_Width/8, self.disp_Height/4 + (row+1) * self.disp_Height/(2*(1+len(quantum_states[column]))))
pg.display.update()
if red_squares > 0:
return True
def loss_View(self, uncorrect_arrays):
background = pg.Rect(self.disp_Width/2, self.disp_Height/2, self.disp_Width/2, self.disp_Height/2)
background.center = (self.disp_Width/2, self.disp_Height/2)
pg.draw.rect(self.display, self.colordict["white"], background)
pg.draw.rect(self.display, self.colordict["black"], background, 10)
#pg.draw.rect(self.display, self.colordict["black"], back_button, 10)
#self.draw_text(self.display, "back", self.colordict["black"], self.fontdict["normal"], back_button.centerx, back_button.centery)
self.draw_text(self.display, "erreur", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, self.disp_Height/2 - self.disp_Height/8)
self.draw_text(self.display, "initial", self.colordict["black"], self.fontdict["normal"],self.disp_Width/4 + self.disp_Width/8, self.disp_Width/4 + self.disp_Width/7)
self.draw_text(self.display, "bonne reponse", self.colordict["black"], self.fontdict["normal"],self.disp_Width/4 + 2* self.disp_Width/8, self.disp_Width/4 + self.disp_Width/7)
self.draw_text(self.display, "votre reponse", self.colordict["black"], self.fontdict["normal"],self.disp_Width/4 + 3* self.disp_Width/8 + 50, self.disp_Width/4 + self.disp_Width/7)
for array_index in range(len(uncorrect_arrays)):
if isinstance(uncorrect_arrays[array_index], numpy.ndarray):
self.draw_quantum_state(uncorrect_arrays[array_index], self.disp_Width/4 + (array_index+1)* self.disp_Width/8, self.disp_Height/2)
else:
for subarray_index in range(len(uncorrect_arrays[array_index])):
self.draw_quantum_state(uncorrect_arrays[array_index][subarray_index].state, self.disp_Width/4 + (array_index+1)* self.disp_Width/8, self.disp_Height/4 + (subarray_index+1) * self.disp_Height/(2*(1+len(uncorrect_arrays[array_index]))), vertically = False)
pg.display.update()
def victory_View(self, next_level_button):
background = pg.Rect(self.disp_Width/2, self.disp_Height/2, self.disp_Width/2, self.disp_Height/2)
background.center = (self.disp_Width/2, self.disp_Height/2)
pg.draw.rect(self.display, self.colordict["white"], background)
pg.draw.rect(self.display, self.colordict["black"], background, 10)
self.draw_text(self.display, "Niveau complete", self.colordict["black"], self.fontdict["normal"], self.disp_Width/2, self.disp_Height/2)
pg.draw.rect(self.display, self.colordict["black"], next_level_button, 10)
self.draw_text(self.display, "Prochain niveau", self.colordict["black"], self.fontdict["normal"],next_level_button.centerx, next_level_button.centery)
pg.display.update()