-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamesnake.py
More file actions
400 lines (343 loc) · 17.5 KB
/
gamesnake.py
File metadata and controls
400 lines (343 loc) · 17.5 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
import pygame, sys, random, json, os
from pygame.math import Vector2
# --- CONFIGURATION ONE UI 7 ---
CELL_SIZE = 30
CELL_NUMBER = 20
SCREEN_WIDTH = CELL_NUMBER * CELL_SIZE
SCREEN_HEIGHT = CELL_NUMBER * CELL_SIZE
FPS = 60
# COULEURS ONE UI 7
UI_BG = (242, 242, 247) # Fond clair One UI
UI_CARD = (255, 255, 255) # Cartes blanches
UI_ACCENT = (10, 132, 255) # Bleu Samsung
UI_TEXT = (28, 28, 30) # Texte presque noir
UI_SUBTEXT = (142, 142, 147) # Texte secondaire
UI_DANGER = (255, 69, 58) # Rouge alertes
GREEN_DARK = (162, 209, 73) # Herbe sombre
GREEN_LIGHT = (170, 215, 81) # Herbe claire
SNAKE_SHADOW = (140, 180, 60)
class SNAKE:
def __init__(self, assets):
self.assets = assets
self.reset()
def draw_snake(self, screen):
self.update_head_graphics()
self.update_tail_graphics()
for index, block in enumerate(self.body):
x_pos = int(block.x * CELL_SIZE)
y_pos = int(block.y * CELL_SIZE)
block_rect = pygame.Rect(x_pos, y_pos, CELL_SIZE, CELL_SIZE)
# Effet d'ombre doux pour le serpent
shadow_rect = block_rect.copy()
shadow_rect.x += 2
shadow_rect.y += 2
pygame.draw.rect(screen, SNAKE_SHADOW, shadow_rect, border_radius=10)
if index == 0:
screen.blit(self.head, block_rect)
elif index == len(self.body) - 1:
screen.blit(self.tail, block_rect)
else:
previous_block = self.body[index + 1] - block
next_block = self.body[index - 1] - block
if previous_block.x == next_block.x:
screen.blit(self.assets['body_vertical'], block_rect)
elif previous_block.y == next_block.y:
screen.blit(self.assets['body_horizontal'], block_rect)
else:
if (previous_block.x == -1 and next_block.y == -1) or (previous_block.y == -1 and next_block.x == -1):
screen.blit(self.assets['body_tl'], block_rect)
elif (previous_block.x == -1 and next_block.y == 1) or (previous_block.y == 1 and next_block.x == -1):
screen.blit(self.assets['body_bl'], block_rect)
elif (previous_block.x == 1 and next_block.y == -1) or (previous_block.y == -1 and next_block.x == 1):
screen.blit(self.assets['body_tr'], block_rect)
elif (previous_block.x == 1 and next_block.y == 1) or (previous_block.y == 1 and next_block.x == 1):
screen.blit(self.assets['body_br'], block_rect)
def update_head_graphics(self):
if len(self.body) > 1:
head_relation = self.body[1] - self.body[0]
if head_relation == Vector2(1, 0): self.head = self.assets['head_left']
elif head_relation == Vector2(-1, 0): self.head = self.assets['head_right']
elif head_relation == Vector2(0, 1): self.head = self.assets['head_up']
elif head_relation == Vector2(0, -1): self.head = self.assets['head_down']
else: self.head = self.assets['head_right']
def update_tail_graphics(self):
if len(self.body) > 1:
tail_relation = self.body[-2] - self.body[-1]
if tail_relation == Vector2(1, 0): self.tail = self.assets['tail_left']
elif tail_relation == Vector2(-1, 0): self.tail = self.assets['tail_right']
elif tail_relation == Vector2(0, 1): self.tail = self.assets['tail_up']
elif tail_relation == Vector2(0, -1): self.tail = self.assets['tail_down']
else: self.tail = self.assets['tail_left']
def move_snake(self):
if self.direction != Vector2(0, 0):
if self.new_block:
body_copy = self.body[:]
body_copy.insert(0, body_copy[0] + self.direction)
self.body = body_copy[:]
self.new_block = False
else:
body_copy = self.body[:-1]
body_copy.insert(0, body_copy[0] + self.direction)
self.body = body_copy[:]
def add_block(self):
self.new_block = True
def reset(self):
self.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
self.direction = Vector2(0, 0)
self.new_block = False
class FRUIT:
def __init__(self, assets):
self.assets = assets
self.pos = Vector2(15, 10)
def draw_fruit(self, screen):
fruit_rect = pygame.Rect(int(self.pos.x * CELL_SIZE), int(self.pos.y * CELL_SIZE), CELL_SIZE, CELL_SIZE)
screen.blit(self.assets['apple'], fruit_rect)
def randomize(self, avoid_positions):
while True:
new_pos = Vector2(random.randint(0, CELL_NUMBER - 1), random.randint(0, CELL_NUMBER - 1))
if new_pos not in avoid_positions:
self.pos = new_pos
break
class OBSTACLE:
def __init__(self, count):
self.positions = []
self.count = count
def randomize(self, avoid_positions):
self.positions = []
while len(self.positions) < self.count:
pos = Vector2(random.randint(0, CELL_NUMBER - 1), random.randint(0, CELL_NUMBER - 1))
if pos not in avoid_positions and pos not in self.positions:
self.positions.append(pos)
def draw_obstacles(self, screen):
for pos in self.positions:
rect = pygame.Rect(int(pos.x * CELL_SIZE), int(pos.y * CELL_SIZE), CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, (100, 110, 120), rect, border_radius=12)
pygame.draw.rect(screen, (200, 200, 200), (rect.x+4, rect.y+4, 8, 8), border_radius=4)
class MAIN:
def __init__(self, screen, assets, sounds):
self.screen = screen
self.assets = assets
self.sounds = sounds
self.snake = SNAKE(assets)
self.fruit = FRUIT(assets)
self.obstacles = OBSTACLE(0)
self.state = "MENU"
self.difficulty = "Medium"
self.score = 0
self.highscores = self.load_highscores()
self.current_speed = 140
self.last_update_time = pygame.time.get_ticks()
self.next_direction = Vector2(0,0)
def load_highscores(self):
if os.path.exists("highscores.json"):
try:
with open("highscores.json", "r") as f:
return json.load(f)
except: return []
return []
def save_highscore(self):
if self.score > 0:
self.highscores.append(self.score)
self.highscores = sorted(list(set(self.highscores)), reverse=True)[:5]
with open("highscores.json", "w") as f:
json.dump(self.highscores, f)
def start_game(self, difficulty):
self.difficulty = difficulty
self.state = "PLAYING"
self.score = 0
self.snake.reset()
self.next_direction = Vector2(0,0)
num_obstacles = 0
if difficulty == "Medium": num_obstacles = 8
elif difficulty == "Hard": num_obstacles = 16
self.obstacles = OBSTACLE(num_obstacles)
self.obstacles.randomize(self.snake.body + [self.fruit.pos])
self.fruit.randomize(self.snake.body + self.obstacles.positions)
self.current_speed = 200 if difficulty == "Easy" else (140 if difficulty == "Medium" else 90)
self.last_update_time = pygame.time.get_ticks()
def update(self):
if self.state == "PLAYING":
speed_boost = min((self.score // 3) * 5, 50)
actual_interval = max(self.current_speed - speed_boost, 50)
now = pygame.time.get_ticks()
if now - self.last_update_time > actual_interval:
if self.next_direction != Vector2(0,0):
self.snake.direction = self.next_direction
self.snake.move_snake()
self.check_collision()
self.check_fail()
self.last_update_time = now
def draw_elements(self):
if self.state in ["PLAYING", "PAUSED", "GAME_OVER"]:
self.draw_grass()
self.obstacles.draw_obstacles(self.screen)
self.fruit.draw_fruit(self.screen)
self.snake.draw_snake(self.screen)
self.draw_ui_overlay()
if self.state == "MENU":
self.draw_one_ui_menu()
elif self.state == "DIFFICULTY":
self.draw_one_ui_dialog("Difficulté", ["1. Facile", "2. Moyen", "3. Difficile"])
elif self.state == "HIGHSCORES":
self.draw_one_ui_dialog("Meilleurs Scores", [f"{i+1}. {s}" for i, s in enumerate(self.highscores)] if self.highscores else ["Aucun score"])
elif self.state == "PAUSED":
self.draw_one_ui_dialog("Pause", ["P. Reprendre", "M. Menu principal"])
elif self.state == "GAME_OVER":
self.draw_one_ui_dialog("Terminé", [f"Score Final: {self.score}", "R. Recommencer", "M. Menu"], title_color=UI_DANGER)
def draw_grass(self):
for row in range(CELL_NUMBER):
for col in range(CELL_NUMBER):
rect = pygame.Rect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(self.screen, GREEN_LIGHT if (row + col) % 2 == 0 else GREEN_DARK, rect)
def draw_ui_overlay(self):
# Barre de statut One UI (haut)
status_rect = pygame.Rect(15, 15, SCREEN_WIDTH - 30, 45)
self.draw_rounded_rect(self.screen, (255, 255, 255, 180), status_rect, 22)
score_surf = self.assets['font_small'].render(f"Score: {self.score}", True, UI_TEXT)
self.screen.blit(score_surf, (35, 26))
pause_hint = self.assets['font_small'].render("P pour Pause", True, UI_SUBTEXT)
self.screen.blit(pause_hint, (SCREEN_WIDTH - pause_hint.get_width() - 35, 26))
def draw_one_ui_menu(self):
self.screen.fill(UI_BG)
# Cercle décoratif Samsung-style
pygame.draw.circle(self.screen, (230, 230, 235), (SCREEN_WIDTH//2, 120), 180)
title = self.assets['font_large'].render("Ab Snake", True, UI_TEXT)
subtitle = self.assets['font_medium'].render("André Abed KT", True, UI_ACCENT)
self.screen.blit(title, title.get_rect(center=(SCREEN_WIDTH//2, 100)))
self.screen.blit(subtitle, subtitle.get_rect(center=(SCREEN_WIDTH//2, 160)))
# Cartes Options dynamiques
options = [("1", "Commencer le jeu"), ("2", "Classement"), ("3", "Quitter")]
for i, (key, text) in enumerate(options):
card_rect = pygame.Rect(40, 280 + i*90, SCREEN_WIDTH - 80, 75)
self.draw_rounded_rect(self.screen, UI_CARD, card_rect, 25)
# Petit badge de touche One UI
badge_rect = pygame.Rect(card_rect.x + 20, card_rect.centery - 15, 30, 30)
pygame.draw.rect(self.screen, UI_ACCENT, badge_rect, border_radius=10)
k_surf = self.assets['font_small'].render(key, True, (255,255,255))
self.screen.blit(k_surf, k_surf.get_rect(center=badge_rect.center))
# Texte de l'option
t_surf = self.assets['font_medium'].render(text, True, UI_TEXT)
self.screen.blit(t_surf, (card_rect.x + 70, card_rect.centery - 15))
def draw_one_ui_dialog(self, title, items, title_color=UI_ACCENT):
# Overlay translucide pour focaliser l'attention
overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 80))
self.screen.blit(overlay, (0,0))
# Carte dialogue (Squircle style)
dialog_w, dialog_h = 340, 100 + len(items)*50
dialog_rect = pygame.Rect((SCREEN_WIDTH - dialog_w)//2, (SCREEN_HEIGHT - dialog_h)//2, dialog_w, dialog_h)
self.draw_rounded_rect(self.screen, UI_CARD, dialog_rect, 35)
# Titre du dialogue
t_surf = self.assets['font_medium'].render(title, True, title_color)
self.screen.blit(t_surf, t_surf.get_rect(center=(SCREEN_WIDTH//2, dialog_rect.y + 45)))
# Liste d'items
for i, item in enumerate(items):
i_surf = self.assets['font_small'].render(item, True, UI_TEXT)
self.screen.blit(i_surf, i_surf.get_rect(center=(SCREEN_WIDTH//2, dialog_rect.y + 100 + i*45)))
def draw_rounded_rect(self, surface, color, rect, radius):
shape_surf = pygame.Surface(rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, (0, 0, rect.width, rect.height), border_radius=radius)
surface.blit(shape_surf, rect.topleft)
def check_collision(self):
if self.fruit.pos == self.snake.body[0]:
self.fruit.randomize(self.snake.body + self.obstacles.positions)
self.snake.add_block()
self.score += 1
self.sounds['crunch'].play()
def check_fail(self):
head = self.snake.body[0]
if not 0 <= head.x < CELL_NUMBER or not 0 <= head.y < CELL_NUMBER: self.game_over()
for block in self.snake.body[1:]:
if block == head: self.game_over()
for obs in self.obstacles.positions:
if obs == head: self.game_over()
def game_over(self):
self.save_highscore()
self.state = "GAME_OVER"
pygame.mixer.music.fadeout(1000)
def load_assets():
assets = {}
graphics = ['apple', 'head_up', 'head_down', 'head_right', 'head_left', 'tail_up', 'tail_down', 'tail_right', 'tail_left', 'body_vertical', 'body_horizontal', 'body_tr', 'body_tl', 'body_br', 'body_bl']
for g in graphics:
img = pygame.image.load(f'Graphics/{g}.png').convert_alpha()
assets[g] = pygame.transform.scale(img, (CELL_SIZE, CELL_SIZE))
try:
assets['font_small'] = pygame.font.Font('Font/Poppins-Regular.ttf', 18)
assets['font_medium'] = pygame.font.Font('Font/Poppins-Regular.ttf', 24)
assets['font_large'] = pygame.font.Font('Font/Poppins-Regular.ttf', 52)
except:
assets['font_small'] = pygame.font.SysFont('Arial', 18)
assets['font_medium'] = pygame.font.SysFont('Arial', 24)
assets['font_large'] = pygame.font.SysFont('Arial', 52)
return assets
def load_sounds():
sounds = {}
pygame.mixer.pre_init(44100, -16, 2, 512)
# Création d'un objet factice pour éviter les erreurs si le son est absent
class DummySound:
def play(self): pass
def set_volume(self, vol): pass
def stop(self): pass
# Tentative de chargement du bruitage
try:
sounds['crunch'] = pygame.mixer.Sound('Sound/crunch.wav')
except:
print("Note: Sound/crunch.wav introuvable, le jeu sera muet.")
sounds['crunch'] = DummySound()
# Tentative de chargement de la musique
try:
if os.path.exists('Sound/snake_game_sound(256k).mp3'):
pygame.mixer.music.load('Sound/snake_game_sound(256k).mp3')
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)
else:
print("Note: Musique introuvable, démarrage sans fond sonore.")
except Exception as e:
print(f"Erreur audio : {e}")
return sounds
def main():
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Ab Snake")
clock = pygame.time.Clock()
assets = load_assets()
sounds = load_sounds()
game = MAIN(screen, assets, sounds)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
if event.type == pygame.KEYDOWN:
if game.state == "MENU":
if event.key == pygame.K_1: game.state = "DIFFICULTY"
if event.key == pygame.K_2: game.state = "HIGHSCORES"
if event.key == pygame.K_3: pygame.quit(); sys.exit()
elif game.state == "DIFFICULTY":
if event.key == pygame.K_1: game.start_game("Easy")
if event.key == pygame.K_2: game.start_game("Medium")
if event.key == pygame.K_3: game.start_game("Hard")
if game.state == "PLAYING": pygame.mixer.music.play(-1)
elif game.state == "HIGHSCORES":
if event.key in [pygame.K_m, pygame.K_ESCAPE]: game.state = "MENU"
elif game.state == "PAUSED":
if event.key in [pygame.K_p, pygame.K_ESCAPE]:
game.state = "PLAYING"; pygame.mixer.music.unpause()
if event.key == pygame.K_m: game.state = "MENU"
elif game.state == "GAME_OVER":
if event.key == pygame.K_r: game.start_game(game.difficulty); pygame.mixer.music.play(-1)
if event.key == pygame.K_m: game.state = "MENU"
elif game.state == "PLAYING":
if event.key in [pygame.K_p, pygame.K_ESCAPE]:
game.state = "PAUSED"; pygame.mixer.music.pause()
if event.key == pygame.K_UP and game.snake.direction.y != 1: game.next_direction = Vector2(0, -1)
if event.key == pygame.K_DOWN and game.snake.direction.y != -1: game.next_direction = Vector2(0, 1)
if event.key == pygame.K_LEFT and game.snake.direction.x != 1: game.next_direction = Vector2(-1, 0)
if event.key == pygame.K_RIGHT and game.snake.direction.x != -1: game.next_direction = Vector2(1, 0)
game.update()
screen.fill(UI_BG)
game.draw_elements()
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()