-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPygame.py
More file actions
226 lines (189 loc) · 7.29 KB
/
Copy pathPygame.py
File metadata and controls
226 lines (189 loc) · 7.29 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
# Import
import pygame
import time
import sys
import os
from Tiles import Menu
# from Arrow import Arrow
# from Label import Label # Si j'importe pas ça marche quand même je ne sais pas pourquoi ...
from Mode import *
# pygame setup
pygame.init()
# Initialisation de la fenêtre
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# screen = pygame.display.set_mode((1280, 720))
width_screen, height_screen = pygame.display.get_surface().get_size()
pygame.display.set_caption("Menu Pygame")
clock = pygame.time.Clock()
running = True
dt = 0
def afficher_texte (message, couleur, y, taille=36, x=None, police=None):
if police is None:
police = police_grande
texte = police.render(message, True, couleur)
if x is None:
x = width_screen // 2
texte_rect = texte.get_rect(center=(x, y))
screen.blit(texte, texte_rect)
return texte_rect
def afficher_texte_petit (message, couleur, y, x=None):
texte = police_petite.render(message, True, couleur)
if x is None:
x = width_screen // 2
texte_rect = texte.get_rect(center=(x, y))
screen.blit(texte, texte_rect)
return texte_rect
# Charger la police "Press Start 2P"
FONT_PATH = os.path.join(os.path.dirname(__file__), 'PressStart2P-Regular.ttf')
police_grande = pygame.font.Font(FONT_PATH, 36)
police_petite = pygame.font.Font(FONT_PATH, 18)
# Définir la taille du rectangle
# SCORE_RECT_WIDTH = 200
# SCORE_RECT_HEIGHT = 50
# Création des labels
rate = Label("rate.png", screen)
mauvais = Label("mauvais.png", screen)
ok = Label("ok.png", screen)
bien = Label("bien.png", screen)
tb = Label("tb.png", screen)
parfait = Label("parfait.png", screen)
labels = [rate, mauvais, ok, bien, tb, parfait]
allow_left = True
allow_down = True
allow_up = True
allow_right = True
# Couleurs
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (169, 169, 169)
PURPLE = "purple"
jeu = None
keys = None
k = None
arrows = []
menu = Menu()
while running:
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
# Affichage de la bannière "Menu"
pygame.draw.rect(screen, BLACK, (0, 0, width_screen, 100))
afficher_texte("Menu", WHITE, 50)
# Affichage de "FingerMania"
afficher_texte("FingerMania", WHITE, 200, taille=100)
# Affichage des boutons
bouton_y_positions = [300, 400, 500]
keys = pygame.key.get_pressed()
if (menu.mode == None):
for i, y in enumerate(bouton_y_positions):
pygame.draw.rect(screen, WHITE, (50, y, (width_screen - 100), 80))
pygame.draw.rect(screen, BLACK, (50, y, (width_screen - 100), 80), 3)
afficher_texte("Mode {}".format(i + 1), PURPLE, y + 30)
# Affichage des caractéristiques
caract = ""
if i == 0:
caract = "Classique"
elif i == 1:
caract = "Multiflèche"
elif i == 2:
caract = "Contre la Montre"
afficher_texte_petit(caract, PURPLE, y + 60)
elif (menu.level == None and menu.mode != 3):
for i, y in enumerate(bouton_y_positions):
pygame.draw.rect(screen, WHITE, (50, y, (width_screen - 100), 80))
pygame.draw.rect(screen, BLACK, (50, y, (width_screen - 100), 80), 3)
afficher_texte("Niveau {}".format(i + 1), PURPLE, y + 30)
# Affichage des caractéristiques
caract = ""
if i == 0:
caract = "Niveau 1"
elif i == 1:
caract = "Niveau 2"
elif i == 2:
caract = "Niveau 3"
afficher_texte_petit(caract, PURPLE, y + 60)
else:
if jeu.timer == None:
print("Initialisation d'une partie")
jeu.init()
jeu.music.play_music()
# print(jeu.arrows)
if len(jeu.arrows) != 0:
jeu.play(screen)
# print(keys)
if keys[pygame.K_LEFT] and allow_left == True:
allow_left = False
k = jeu.arrow_pressed(0)
if not keys[pygame.K_LEFT]:
allow_left = True
if keys[pygame.K_DOWN] and allow_down == True:
allow_down = False
k = jeu.arrow_pressed(1)
if not keys[pygame.K_DOWN]:
allow_down = True
if keys[pygame.K_UP] and allow_up == True:
allow_up = False
k = jeu.arrow_pressed(2)
if not keys[pygame.K_UP]:
allow_up = True
if keys[pygame.K_RIGHT] and allow_right == True:
allow_right = False
k = jeu.arrow_pressed(3)
if not keys[pygame.K_RIGHT]:
allow_right = True
if k is not None:
screen.blit(labels[k].image, (labels[k].x, labels[k].y))
else:
jeu.music.stop_music()
# Dimensions de la fenêtre display_final_score
fenetre_w = 600
fenetre_h = 200
# Calcul des coordonnées pour centrer la fenêtre
x = (width_screen - fenetre_w) // 2
y = (height_screen - fenetre_h) // 4
# Appel de la méthode display_final_score pour afficher le score final
if menu.mode == 1 or menu.mode == 2:
jeu.display_final_score(screen, x, y, fenetre_w, fenetre_h)
elif menu.mode == 3:
jeu.timer = time.time() - jeu.timer
jeu.display_final_time(screen, x, y, fenetre_w, fenetre_h)
pygame.display.flip() # Rafraîchir l'écran pour afficher le score final
time.sleep(5) # Afficher le score final et le rang durant 5 secondes
menu.mode = None
menu.level = None
jeu.timer = None
if keys[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
# Bouton "Quitter" en bas à droite avec une police spécifique
rect_quitter = afficher_texte("Quitter", BLACK, height_screen - 30, police=police_petite, x=width_screen - 100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
for i, bouton_y in enumerate(bouton_y_positions):
if 50 <= x <= width_screen - 50 and bouton_y <= y <= bouton_y + 80: # Si on est sur un des boutons
if (menu.mode == None):
menu.mode = i + 1
match i:
case 0:
jeu = Classic(screen)
case 1:
jeu = Multi_Arrows(screen)
case 2:
jeu = Chrono(screen)
elif (menu.level == None):
jeu.level = i + 1
menu.level = i + 1
# Vérification du clic sur le bouton "Quitter"
if event.type == pygame.MOUSEBUTTONDOWN and rect_quitter.collidepoint(pygame.mouse.get_pos()):
pygame.quit()
sys.exit()
# flip() the display to put your work on screen
pygame.display.flip()
# limits FPS to 60
# dt is delta time in seconds since last frame, used for framerate-
# independent physics.
dt = clock.tick(60) / 1000
pygame.quit()