-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
122 lines (93 loc) · 3.53 KB
/
snake.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
import tkinter # Bibliothèque GUI standard de Python
import random
ROWS = 25
COLS = 25
TILE_SIZE = 25
WINDOW_WIDTH = TILE_SIZE * COLS
WINDOWS_HEIGHT = TILE_SIZE * ROWS
class Tile:
def __init__(self, x, y):
self.x = x
self.y = y
# Fenêtre de jeu | Game window
window = tkinter.Tk() # Crée la fenêtre principale
window.title("Nassim Snake")
window.resizable(False, False) # Empêche le redimensionnement de la fenêtre
# Canvas : zone de dessin pour des formes et des graphismes
canvas = tkinter.Canvas(window, bg="black", width=WINDOW_WIDTH, height=WINDOWS_HEIGHT, border=0, highlightthickness=0)
canvas.pack() # Ajoute le canvas à la fenêtre
window.update()
# Jeu | Game
snake = Tile(TILE_SIZE * 5, TILE_SIZE * 5) # Début du serpent
food = Tile(TILE_SIZE * 10, TILE_SIZE * 10) # Nourriture
snake_body = []
# Pour le contrôle
velocityX = 0
velocityY = 0
game_over = False
score = 0
def change_direction(e): #e = event
# print(e) = affiche la touche pressée
# print(e.keysym) = affiche le symbole de la touche pressée
global velocityX, velocityY, game_over
if(game_over):
return
if (e.keysym == "Up" and velocityY != 1):
velocityX = 0
velocityY = -1
elif (e.keysym == "Down" and velocityY != -1):
velocityX = 0
velocityY = 1
elif (e.keysym == "Left" and velocityX != 1):
velocityX = -1
velocityY = 0
elif (e.keysym == "Right" and velocityX != -1):
velocityX = 1
velocityY = 0
def move():
global snake, food, snake_body, game_over, score
if(game_over):
return
if (snake.x < 0 or snake.x >= WINDOW_WIDTH or snake.y < 0 or snake.y >= WINDOWS_HEIGHT):
game_over = True
return
for tile in snake_body:
if (snake.x == tile.x and snake.y == tile.y):
game_over = True
return
# Gestion des collisions
if (snake.x == food.x and snake.y == food.y):
snake_body.append(Tile(food.x, food.y))
food.x = random.randint(0, COLS-1) * TILE_SIZE
food.y = random.randint(0, ROWS-1) * TILE_SIZE
score += 1
# Corps du serpent
for i in range(len(snake_body)-1, -1, -1):
tile = snake_body[i]
if (i == 0):
tile.x = snake.x
tile.y = snake.y
else:
prev_tile = snake_body[i-1]
tile.x = prev_tile.x
tile.y = prev_tile.y
snake.x += velocityX * TILE_SIZE
snake.y += velocityY * TILE_SIZE
def draw():
global snake, food, snake_body, game_over, score
move()
canvas.delete("all")
# Dessine le serpent
canvas.create_rectangle(snake.x, snake.y, snake.x + TILE_SIZE, snake.y + TILE_SIZE, fill = "blue")
#Dessine la nourriture
canvas.create_rectangle(food.x, food.y, food.x + TILE_SIZE, food.y + TILE_SIZE, fill = "red")
for tile in snake_body:
canvas.create_rectangle(tile.x, tile.y, tile.x + TILE_SIZE, tile.y + TILE_SIZE, fill = "blue")
if(game_over):
canvas.create_text(85, 20, font = "Arial 20", text = "GAME OVER", fill = "white")
else:
canvas.create_text(30, 20, font = "Arial 10", text = f"Score : {score}", fill = "white")
window.after(100, draw) #10 FPS
draw()
window.bind("<KeyRelease>", change_direction) # Pour les changements de directions avec les flèches
window.mainloop() # Garde la fenêtre ouverte