-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
274 lines (245 loc) · 9.78 KB
/
main.py
File metadata and controls
274 lines (245 loc) · 9.78 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
# coding: utf-8
#
# A simple snake game with
VERSION = "0.1"
try:
import sys
import os
import time
import itertools
import random
from configuration import (WINDOW_SIZE, HEAD_IMG, IMAGE_FOLDER,
TAIL_IMG, BODY_STRAIGHT_IMG, MAP_SIZE,
CASE_SIZE, TURN_IMG, GAME_OVER, FRUITS_IMG,
KEYS)
import pygame
from pygame.locals import *
except ImportError as err:
print("Impossible de charger le module. %s" % (err))
sys.exit(2)
pygame.init()
screen = pygame.display.set_mode(WINDOW_SIZE)
def load_png(image):
""" Charge une image à partir d'un fichier
Param:
image -> str -> chemin vers une image
Return:
pygame.Surface -> Objet de l'image
"""
fullname = os.path.join(IMAGE_FOLDER, image)
try:
image = pygame.image.load(fullname)
if image.get_alpha() is None:
image = image.convert()
else:
image = image.convert_alpha()
except pygame.error as message:
print("Impossible to load image: ", message)
sys.exit(2)
return image
game_over = load_png(GAME_OVER)
class Snake():
"""Classe permettant la gestion du serpent à afficher"""
def __init__(self, initial_size=3):
"""Initialisation of the class
param:
initial_size -> int -> the lenght of the snake at the begining
"""
self.images = {"head": load_png(HEAD_IMG),
"body_straight": load_png(BODY_STRAIGHT_IMG),
"tail": load_png(TAIL_IMG),
"body_turn": load_png(TURN_IMG)}
self.initial_size = initial_size
self.coords = [[int(MAP_SIZE[0] / 2), int(MAP_SIZE[1] / 2)]
for i in range(initial_size)]
self.surface = pygame.surface.Surface(WINDOW_SIZE)
self.surface.set_colorkey((0, 0, 0))
self.directions = [0 for i in range(initial_size)]
def rotate(self, image, angle):
return pygame.transform.rotate(
self.images[image],
angle)
def update(self):
"""Update the surface of the snake"""
self.surface.fill((0, 0, 0))
# head
self.surface.blit(pygame.transform.rotate(self.images["head"],
-90 * self.directions[0]),
(CASE_SIZE[0] * self.coords[0][0],
CASE_SIZE[1] * self.coords[0][1]))
# body
size_x, size_y = CASE_SIZE
for i in range(len(self.coords[1:-1])):
x, y = self.coords[i + 1]
direction = self.directions[i + 1]
direction_prec = self.directions[i]
if direction == direction_prec: # straight
self.surface.blit(self.rotate("body_straight", -90*direction),
(size_x * x, size_y * y))
# turn right>top or botom>left
if (direction == 0 and direction_prec == 1) or \
(direction == 3 and direction_prec == 2):
self.surface.blit(self.rotate("body_turn",0),
(size_x * x, size_y * y))
# turn left>top or botom>right
if (direction == 0 and direction_prec == 3) or \
(direction == 1 and direction_prec == 2):
self.surface.blit(self.rotate("body_turn",-90),
(size_x * x, size_y * y))
# turn left>botom or top>right
if (direction == 2 and direction_prec == 3) or \
(direction == 1 and direction_prec == 0):
self.surface.blit(self.rotate("body_turn",180),
(size_x * x, size_y * y))
# turn top>left or right>botom
if (direction == 3 and direction_prec == 0) or \
(direction == 2 and direction_prec == 1):
self.surface.blit(self.rotate("body_turn",90),
(size_x * x, size_y * y))
# Tail
self.surface.blit(pygame.transform.rotate(self.images["tail"],
-90 * self.directions[-2]),
(size_x * self.coords[-1][0],
size_y * self.coords[-1][1]))
def deplacer(self, direction=0):
"""Move the snake
0 = top
1 = right
2 = bottom
3 = left"""
# forbiden about-face
if self.directions[0] != direction:
if self.directions[0] + direction == 2:
direction = self.directions[0]
elif self.directions[0] + direction == 4:
direction = self.directions[0]
elif direction not in (0, 1, 2, 3):
direction = self.directions[0]
# insert new direction
self.directions.insert(0, direction)
self.directions = self.directions[:-1]
# insert new coords
if direction == 0:
new_coords = (self.coords[0][0],
self.coords[0][1] - 1)
self.coords.insert(0, new_coords)
self.coords = self.coords[:-1]
elif direction == 1:
new_coords = (self.coords[0][0] + 1,
self.coords[0][1])
self.coords.insert(0, new_coords)
self.coords = self.coords[:-1]
elif direction == 2:
new_coords = (self.coords[0][0],
self.coords[0][1] + 1)
self.coords.insert(0, new_coords)
self.coords = self.coords[:-1]
elif direction == 3:
new_coords = (self.coords[0][0] - 1,
self.coords[0][1])
self.coords.insert(0, new_coords)
self.coords = self.coords[:-1]
def grow(self):
"""Grow the snake"""
self.directions.append(self.directions[-1])
self.coords.append(self.coords[-1])
def collision(self):
for coord in self.coords[1:]:
if coord == self.coords[0]:
return True
if self.coords[0][0] < 0 or \
self.coords[0][1] < 0 or \
self.coords[0][0] > MAP_SIZE[0] or \
self.coords[0][1] > MAP_SIZE[1]:
return True
return False
class Fruits():
"""Class for fruits"""
def __init__(self):
self.fruits_coords = []
self.fruits_image = []
self.images = [load_png(image) for image in FRUITS_IMG]
self.surface = pygame.surface.Surface(WINDOW_SIZE)
self.surface.fill((0, 0, 0))
self.surface.set_colorkey((0, 0, 0))
def add_fruit(self):
"""Add a fruit on the map"""
condition = True
ligne, case = (0, 0)
while (ligne, case) in self.fruits_coords or condition:
ligne = random.randint(0, MAP_SIZE[0]-1)
case = random.randint(0, MAP_SIZE[1]-1)
condition = False
self.fruits_coords.append((ligne, case))
self.fruits_image.append(random.choice(self.images))
self.update()
def collision(self, x, y):
"""test if ther is a colision between coordinates, y and x, and a fruit on map"""
for i in range(len(self.fruits_coords)):
if self.fruits_coords[i] == (x, y):
del self.fruits_coords[i]
del self.fruits_image[i]
return True
def update(self):
self.surface.fill((0, 0, 0))
for i in range(len(self.fruits_coords)):
self.surface.blit(self.fruits_image[i],
(self.fruits_coords[i][0]*CASE_SIZE[0],
self.fruits_coords[i][1]*CASE_SIZE[1]))
class Game():
""" Main class for the game """
def __init__(self, difficult):
"""initialisation of the game"""
self.snake = Snake(initial_size=6)
self.fruits = Fruits()
self.points = 0
self.font = pygame.font.SysFont("monospace", 24)
self.speed = 1
self.difficult = difficult
def run(self):
"""run the game"""
pygame.display.set_caption('Znake')
pygame.mouse.set_visible(0)
temps_prec = 0
clock = pygame.time.Clock()
move = False
black = pygame.surface.Surface(WINDOW_SIZE)
self.fruits.add_fruit()
self.fruits.add_fruit()
self.fruits.add_fruit()
self.fruits.add_fruit()
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
while pygame.event.wait().__dict__.get('key') is not K_ESCAPE:
continue
if event.key in KEYS:
self.snake.deplacer(KEYS.index(event.key))
move = True
if not move:
self.snake.deplacer(-1)
if self.fruits.collision(*self.snake.coords[0]):
self.points += 10
self.snake.grow()
self.fruits.add_fruit()
self.speed += 0.1*self.difficult
self.points += 1
move = False
self.snake.update()
screen.blit(black, (0, 0))
score_display = self.font.render(str(self.points), 1, (0,0,255))
screen.blit(score_display, (0, 0))
screen.blit(self.fruits.surface, (0, 0))
screen.blit(self.snake.surface, (0, 0))
if self.snake.collision():
screen.blit(game_over, (0, 0))
pygame.display.flip()
return
pygame.display.flip()
clock.tick(self.speed)
if __name__ == '__main__':
game = Game(difficult = 30)
game.run()