-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
439 lines (355 loc) · 15.5 KB
/
index.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
import pygame
import time
import random
from pygame.math import Vector2
class SNAKE:
def __init__(self):
self.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
self.direction = Vector2(0, 0)
self.new_block = False
self.head_up = pygame.image.load(
'Graphics/head_up.png').convert_alpha()
self.head_down = pygame.image.load(
'Graphics/head_down.png').convert_alpha()
self.head_right = pygame.image.load(
'Graphics/head_right.png').convert_alpha()
self.head_left = pygame.image.load(
'Graphics/head_left.png').convert_alpha()
self.tail_up = pygame.image.load(
'Graphics/tail_up.png').convert_alpha()
self.tail_down = pygame.image.load(
'Graphics/tail_down.png').convert_alpha()
self.tail_right = pygame.image.load(
'Graphics/tail_right.png').convert_alpha()
self.tail_left = pygame.image.load(
'Graphics/tail_left.png').convert_alpha()
self.body_vertical = pygame.image.load(
'Graphics/body_vertical.png').convert_alpha()
self.body_horizontal = pygame.image.load(
'Graphics/body_horizontal.png').convert_alpha()
self.body_tr = pygame.image.load(
'Graphics/body_tr.png').convert_alpha()
self.body_tl = pygame.image.load(
'Graphics/body_tl.png').convert_alpha()
self.body_br = pygame.image.load(
'Graphics/body_br.png').convert_alpha()
self.body_bl = pygame.image.load(
'Graphics/body_bl.png').convert_alpha()
self.crunch_sound = pygame.mixer.Sound('Sound/crunch.wav')
def draw_snake(self):
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)
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.body_vertical, block_rect)
elif previous_block.y == next_block.y:
screen.blit(self.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.body_tl, block_rect)
if previous_block.x == -1 and next_block.y == 1 or previous_block.y == 1 and next_block.x == -1:
screen.blit(self.body_bl, block_rect)
if previous_block.x == 1 and next_block.y == -1 or previous_block.y == -1 and next_block.x == 1:
screen.blit(self.body_tr, block_rect)
if previous_block.x == 1 and next_block.y == 1 or previous_block.y == 1 and next_block.x == 1:
screen.blit(self.body_br, block_rect)
def update_head_graphics(self):
head_relation = self.body[1] - self.body[0]
if head_relation == Vector2(1, 0):
self.head = self.head_left
elif head_relation == Vector2(-1, 0):
self.head = self.head_right
elif head_relation == Vector2(0, 1):
self.head = self.head_up
elif head_relation == Vector2(0, -1):
self.head = self.head_down
def update_tail_graphics(self):
tail_relation = self.body[-2] - self.body[-1]
if tail_relation == Vector2(1, 0):
self.tail = self.tail_left
elif tail_relation == Vector2(-1, 0):
self.tail = self.tail_right
elif tail_relation == Vector2(0, 1):
self.tail = self.tail_up
elif tail_relation == Vector2(0, -1):
self.tail = self.tail_down
def move_snake(self):
if self.new_block == True:
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 play_crunch_sound(self):
self.crunch_sound.play()
def reset(self):
self.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
self.direction = Vector2(0, 0)
class FRUIT:
def __init__(self):
self.randomize()
def draw_fruit(self):
fruit_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(mouse, fruit_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES1:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES2:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES3:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES4:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES5:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES6:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES7:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class STONES8:
def __init__(self):
self.randomize()
def draw_stones(self):
stones_rect = pygame.Rect(
int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
screen.blit(stone, stones_rect)
# pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class MAIN:
def __init__(self):
self.snake = SNAKE()
self.fruit = FRUIT()
self.stones1 = STONES1()
self.stones2 = STONES2()
self.stones3 = STONES3()
self.stones4 = STONES4()
self.stones5 = STONES5()
self.stones6 = STONES6()
self.stones7 = STONES7()
self.stones8 = STONES8()
def update(self):
self.check_fail()
self.snake.move_snake()
self.check_collision()
self.check_fail()
def draw_elements(self):
self.draw_sand()
self.fruit.draw_fruit()
self.stones1.draw_stones()
self.stones2.draw_stones()
self.stones3.draw_stones()
self.stones4.draw_stones()
self.stones5.draw_stones()
self.stones6.draw_stones()
self.stones7.draw_stones()
self.stones8.draw_stones()
self.snake.draw_snake()
self.draw_score()
def check_collision(self):
if self.fruit.pos == self.snake.body[0]:
self.fruit.randomize()
self.snake.add_block()
self.snake.play_crunch_sound()
if self.fruit.pos == self.stones1.pos or self.fruit.pos == self.stones2.pos or self.fruit.pos == self.stones3.pos or self.fruit.pos == self.stones4.pos or self.fruit.pos == self.stones5.pos or self.fruit.pos == self.stones6.pos or self.fruit.pos == self.stones7.pos or self.fruit.pos == self.stones8.pos:
self.fruit.randomize()
for block in self.snake.body[1:]:
if block == self.fruit.pos:
self.fruit.randomize()
if block == self.stones1.pos or block == self.stones2.pos or block == self.stones3.pos or block == self.stones4.pos or block == self.stones5.pos or block == self.stones6.pos or block == self.stones7.pos or block == self.stones8.pos:
self.stones1.randomize()
self.stones2.randomize()
self.stones3.randomize()
self.stones4.randomize()
self.stones5.randomize()
self.stones6.randomize()
self.stones7.randomize()
self.stones8.randomize()
def check_fail(self):
if not 0 <= self.snake.body[0].x < cell_number or not 0 <= self.snake.body[0].y < cell_number:
self.game_over()
if self.snake.body[0] == self.stones1.pos or self.snake.body[0] == self.stones2.pos or self.snake.body[0] == self.stones3.pos or self.snake.body[0] == self.stones4.pos or self.snake.body[0] == self.stones5.pos or self.snake.body[0] == self.stones6.pos or self.snake.body[0] == self.stones7.pos or self.snake.body[0] == self.stones8.pos:
self.game_over()
for block in self.snake.body[1:]:
if block == self.snake.body[0]:
self.game_over()
def game_over(self):
self.snake.reset()
def draw_sand(self):
grass_color = (252, 174, 24)
for row in range(cell_number):
if row % 2 == 0:
for col in range(cell_number):
if col % 2 == 0:
grass_rect = pygame.Rect(
col * cell_size, row * cell_size, cell_size, cell_size)
pygame.draw.rect(screen, grass_color, grass_rect)
else:
for col in range(cell_number):
if col % 2 != 0:
grass_rect = pygame.Rect(
col * cell_size, row * cell_size, cell_size, cell_size)
pygame.draw.rect(screen, grass_color, grass_rect)
def draw_score(self):
score_text = str(len(self.snake.body) - 3)
score_surface = game_font.render(score_text, True, (56, 74, 12))
score_x = int(cell_size * cell_number - 60)
score_y = int(cell_size * cell_number - 40)
score_rect = score_surface.get_rect(center=(score_x, score_y))
mouse_rect = mouse.get_rect(
midright=(score_rect.left, score_rect.centery))
bg_rect = pygame.Rect(mouse_rect.left, mouse_rect.top,
mouse_rect.width + score_rect.width + 6, mouse_rect.height)
pygame.draw.rect(screen, (252, 174, 24), bg_rect)
screen.blit(score_surface, score_rect)
screen.blit(mouse, mouse_rect)
pygame.draw.rect(screen, (56, 74, 12), bg_rect, 2)
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
cell_size = 40
cell_number = 20
screen = pygame.display.set_mode(
(cell_number * cell_size, cell_number * cell_size))
pygame.display.set_caption("Snake")
image = pygame.image.load("logo.png").convert()
pygame.display.set_icon(image)
clock = pygame.time.Clock()
mouse = pygame.image.load('Graphics/mouse.png').convert_alpha()
stone = pygame.image.load('Graphics/stone.png').convert_alpha()
game_font = pygame.font.Font(None, 25)
SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE, 100)
main_game = MAIN()
playing = True
running = True
RED = (255, 0, 0)
pygame.mixer.music.load('Sound/background.wav')
pygame.mixer.music.play(loops=-1)
while running:
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == SCREEN_UPDATE:
main_game.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if main_game.snake.direction.y != 1:
main_game.snake.direction = Vector2(0, -1)
if event.key == pygame.K_RIGHT:
if main_game.snake.direction.x != -1:
main_game.snake.direction = Vector2(1, 0)
if event.key == pygame.K_DOWN:
if main_game.snake.direction.y != -1:
main_game.snake.direction = Vector2(0, 1)
if event.key == pygame.K_LEFT:
if main_game.snake.direction.x != 1:
main_game.snake.direction = Vector2(-1, 0)
screen.fill((255, 182, 1))
main_game.draw_elements()
pygame.display.update()
clock.tick(60)
pygame.display.flip()
gameover = game_font.render(
"Press R to Respawn", False, (255, 255, 255))
rect = gameover.get_rect()
rect.center = screen.get_rect().center
screen.blit(gameover, rect)
pygame.display.flip()
for event in pygame.event.get():
# event input clavier
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
playing = True