-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path9-points.py
234 lines (209 loc) · 7.18 KB
/
9-points.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
import pygame, time
from constants import *
from buzzer import SuperArduino, ShadowSuperArduino
"""
Ceci est le code pour le 9 points gagnants
"""
super_arduino = SuperArduino("/dev/cu.usbmodem1413101")
#super_arduino = ShadowSuperArduino("/dev/cu.usbmodem14112101")
# Liste de joueurs à changer si besoin
LISTE_JOUEURS = [
("Emile", "images/Emile.jpg"),
("Quentin", "images/Quentin.jpg"),
("Daniel", "images/Daniel.jpg"),
("Camille", "images/Camille.jpg")
]
successes, failures = pygame.init()
print("{0} successes and {1} failures".format(successes, failures))
# Taille d'écran
W = 1920
H = 1080
TITLE_SIZE = H//12
GAME_FONT = pygame.font.SysFont('Comic Sans MS', TITLE_SIZE)
SCORE_FONT = pygame.font.SysFont('Comic Sans MS', int(TITLE_SIZE*0.5))
screen = pygame.display.set_mode((W, H), pygame.FULLSCREEN | pygame.SCALED)
PLAYER_BORDER = H*0.06
PLAYER_LONG = (W - 5*PLAYER_BORDER)/4
# 720 / 4 180
#
clock = pygame.time.Clock()
FPS = 30 # Frames per second.
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
def write_title(screen, title, y_pos):
text_qpuc = GAME_FONT.render(title, False, SEASHELL_RGB)
text_rect = text_qpuc.get_rect(center=(W/2, y_pos))
screen.blit(text_qpuc, text_rect)
def draw_hexagon(screen, x, y, color=GOLD_RGB, width=100):
# height = width / 2
width_scale = width/100
height_scale = width/100
pygame.draw.polygon(
screen,
color,
(
(x-25*width_scale, y+0),
(x+25*width_scale, y+0),
(x+50*width_scale, y+25*height_scale),
(x+25*width_scale, y+50*height_scale),
(x-25*width_scale, y+50*height_scale),
(x-50*width_scale, y+25*height_scale),
(x-25*width_scale, y+0)
)
)
def draw_player_zone(screen, i=0):
rectangle_height = H-5*TITLE_SIZE/2-2*PLAYER_BORDER
pygame.draw.rect(
screen,
GOLD_RGB,
pygame.Rect(
PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG),
5*TITLE_SIZE/2,
PLAYER_LONG,
rectangle_height
)
)
pygame.draw.rect(
screen,
POWDER,
pygame.Rect(
PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG),
5*TITLE_SIZE/2+rectangle_height*0.01,
PLAYER_LONG,
rectangle_height*0.98
)
)
angle = 0
target_width = PLAYER_LONG-1*PLAYER_BORDER
name, img_src = LISTE_JOUEURS[i]
img = pygame.image.load(img_src).convert()
rect = img.get_rect()
scale = target_width / rect.width
img = pygame.transform.rotozoom(img, angle, scale)
screen.blit(
img,
(
1.5*PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG),
5*TITLE_SIZE/2+0.5*PLAYER_BORDER
)
)
x = PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG)+PLAYER_LONG/2
hexagon_width = W/10
y = H-2*PLAYER_BORDER-1*hexagon_width/4
text_qpuc = SCORE_FONT.render(name, False, BLUE_RGB)
text_rect = text_qpuc.get_rect(center=(x, y))
screen.blit(text_qpuc, text_rect)
def draw_score(screen, i, val=0, is_on=0):
hexagon_width = W/10
color = [GOLD_RGB, GREEN_RGB, PINK_RGB][is_on]
draw_hexagon(
screen,
PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG)+PLAYER_LONG/2,
H-2*PLAYER_BORDER-hexagon_width,
color,
width=hexagon_width
)
draw_hexagon(
screen,
PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG)+PLAYER_LONG/2,
H-2*PLAYER_BORDER-hexagon_width*0.96,
SEASHELL_RGB,
width=hexagon_width*0.84
)
draw_hexagon(
screen,
PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG)+PLAYER_LONG/2,
H-2*PLAYER_BORDER-hexagon_width*0.95,
BLUE_RGB,
width=hexagon_width*0.8
)
x = PLAYER_BORDER+i*(PLAYER_BORDER+PLAYER_LONG)+PLAYER_LONG/2
hexagon_width = W/10
y = H-2*PLAYER_BORDER-3*hexagon_width/4
text_qpuc = SCORE_FONT.render(str(val), False, SEASHELL_RGB)
text_rect = text_qpuc.get_rect(center=(x, y))
screen.blit(text_qpuc, text_rect)
def turn_on_play(liste_on):
super_arduino.turn_on_buzzer([x==0 for x in liste_on])
print("play")
return True
liste_score = [0, 0, 0, 0]
liste_on = [0, 0, 0, 0] # 0 : Gold, 1 : Green, 2 : Red
# liste_on = [True, True, True, True]
on_game = True
screen.fill(BLUE_RGB)
write_title(screen, "Questions pour un champion", TITLE_SIZE/2)
write_title(screen, "9 points gagnants", 3*TITLE_SIZE/2)
# Affichage Logo
target_heigth = TITLE_SIZE*2
img_src = "images/ENSAE2.png"
img = pygame.image.load(img_src).convert_alpha()
rect = img.get_rect()
img = pygame.transform.rotozoom(img, 0, target_heigth/rect.height)
screen.blit(img, (PLAYER_BORDER, PLAYER_BORDER//2))
img_src = "images/TUXAE.png"
img = pygame.image.load(img_src).convert_alpha()
rect = img.get_rect()
img = pygame.transform.rotozoom(img, 0, target_heigth/rect.height)
screen.blit(img, (W - PLAYER_BORDER-img.get_width(), PLAYER_BORDER//2))
for i in range(4):
draw_player_zone(screen, i=i)
draw_score(screen, i, val=liste_score[i])
while True:
clock.tick(FPS)
if on_game:
res = super_arduino.get_winner(turn_on=False)
if res is not None:
liste = [False for i in range(4)]
liste[res] = True
print(res, liste)
super_arduino.turn_on_buzzer(liste)
on_game = False
liste_on[res] = 1
for i in range(4):
draw_score(screen, i, val=liste_score[i], is_on=liste_on[i])
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
liste_score[0] += 1
if event.key == pygame.K_z:
liste_score[1] += 1
if event.key == pygame.K_e:
liste_score[2] += 1
if event.key == pygame.K_r:
liste_score[3] += 1
if event.key == pygame.K_q:
liste_score[0] -= 1
if event.key == pygame.K_s:
liste_score[1] -= 1
if event.key == pygame.K_d:
liste_score[2] -= 1
if event.key == pygame.K_f:
liste_score[3] -= 1
if event.key == pygame.K_w:
liste_on[0] = 2
on_game = turn_on_play(liste_on)
if event.key == pygame.K_x:
liste_on[1] = 2
on_game = turn_on_play(liste_on)
if event.key == pygame.K_c:
liste_on[2] = 2
on_game = turn_on_play(liste_on)
if event.key == pygame.K_v:
liste_on[3] = 2
on_game = turn_on_play(liste_on)
if event.key == pygame.K_SPACE:
liste_on = [True, True, True, True]
liste_on = [0, 0, 0, 0]
super_arduino.turn_on_buzzer([True, True, True, True])
on_game = turn_on_play(liste_on)
if event.key == pygame.K_ESCAPE:
quit()
for i in range(4):
draw_score(screen, i, val=liste_score[i], is_on=liste_on[i])
pygame.display.update() # Or pygame.display.flip()