-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake_interface.py
198 lines (145 loc) · 5.54 KB
/
snake_interface.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
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import random
pygame.init() # Init the pygame engine
# Set the FPS
FPS = 5
CLOCK = pygame.time.Clock()
# Screen information
WIDTH = 500
HEIGHT = 500
# Define color
WHITE = (255, 255, 255)
BLACK = (50, 50, 50)
GREY = (125, 125, 125)
GREEN = (0, 125, 0)
RED = (125, 0, 0)
# Create font
FONT = pygame.font.SysFont('Courier New', int((WIDTH+HEIGHT)//2*0.06), bold=True)
# Display the screen
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("snake")
# Pixel size
PIXEL_SIZE = 20
NB_PIXEL_COL = WIDTH // PIXEL_SIZE # width
NB_PIXEL_ROW = HEIGHT // PIXEL_SIZE # height
def generate_apple(snake):
"""Generate the apple"""
col = random.randint(0, NB_PIXEL_COL - 1)
row = random.randint(0, NB_PIXEL_ROW - 1)
apple = pygame.Rect(col*PIXEL_SIZE, row*PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE)
#Ckeck if the apple pos is not in the snake
while apple.collidelistall(snake):
col = random.randint(0, NB_PIXEL_COL - 1)
row = random.randint(0, NB_PIXEL_ROW - 1)
apple = pygame.Rect(col*PIXEL_SIZE, row*PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE)
return apple
def move_snake(snake, direction, eating):
"""Move the snake"""
head = snake[0].copy()
if direction == "up":
head.y -= PIXEL_SIZE
if direction == "down":
head.y += PIXEL_SIZE
if direction == "left":
head.x -= PIXEL_SIZE
if direction == "right":
head.x += PIXEL_SIZE
snake.insert(0, head)
if not eating:
snake.pop()
eating = False
return snake, direction, eating
def check_collision(snake, apple, score, eating, game_active):
"""Manage the collision with the snake"""
# Collision with apple
if snake[0].colliderect(apple):
eating = True
apple = generate_apple(snake)
score += 1
# Collision with itself
if snake[0].collidelistall(snake[1:]) and len(snake) > 1:
game_active = False
print("collision avec le serpent")
# Collision with the borders
if snake[0].x < 0 or snake[0].x >= WIDTH or snake[0].y < 0 or snake[0].y >= HEIGHT:
game_active = False
print("collision avec le mur")
return snake, apple, score, eating, game_active
# Init snake
snake = [pygame.Rect(NB_PIXEL_COL//2*PIXEL_SIZE, (NB_PIXEL_ROW//2-1)*PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE)]
direction = "left"
eating = False
# Init apple
apple = generate_apple(snake)
# Init score
score = 0
# Main game
run = True
game_active = True
restart = False
while run:
# Display the score
pygame.display.set_caption(f"snake | score : {score}")
# Copy direction to avoid going backward
new_direction = direction
# Exit if the window is closed
for event in pygame.event.get():
if event.type == QUIT:
run = False
if game_active:
# Change the direction of the snake according to the key pressed
if event.type == pygame.KEYDOWN:
if event.key == K_UP and direction != "down":
new_direction = "up"
if event.key == K_DOWN and direction != "up":
new_direction = "down"
if event.key == K_LEFT and direction != "right":
new_direction = "left"
if event.key == K_RIGHT and direction != "left":
new_direction = "right"
else :
# Restart the game if the Enter key is pressed
if event.type == pygame.KEYDOWN:
if event.key == K_RETURN:
restart = True
# Update direction with the (latest) new direction
direction = new_direction
# When the game is active
if game_active:
# Draw the background
SCREEN.fill(GREY)
for col in range(NB_PIXEL_COL):
for row in range(NB_PIXEL_ROW):
pygame.draw.rect(SCREEN, BLACK, (col*PIXEL_SIZE, row*PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE), width=1)
# Draw the snake
for r_snake in snake:
pygame.draw.rect(SCREEN, GREEN, r_snake)
# Draw the apple
pygame.draw.rect(SCREEN, RED, apple)
# Move the snake
snake, direction, eating = move_snake(snake, direction, eating)
# Check collisions
snake, apple, score, eating, game_active = check_collision(snake, apple, score, eating, game_active)
pygame.display.update()
# When the game is over
else:
# Draw the game over text
text_end = FONT.render(f"Game Over | score : {score}", True, WHITE, BLACK)
SCREEN.blit(text_end, (WIDTH//2 - text_end.get_width() // 2, HEIGHT//2 - text_end.get_height()//2))
pygame.display.update()
# Restart a game if the player press Enter
if restart:
# Init snake
snake = [pygame.Rect(NB_PIXEL_COL//2*PIXEL_SIZE, (NB_PIXEL_ROW//2-1)*PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE)]
direction = "left"
eating = False
# Init apple
apple = generate_apple(snake)
# Init score
score = 0
restart = False
game_active = True
CLOCK.tick(FPS)
pygame.quit()