-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.py
executable file
·241 lines (191 loc) · 6.42 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
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
#!/usr/bin/python3
#############################
# SNAKE GAME #
# Carlos Caminero #
# Contributors: #
# Javier Martinez #
#############################
import pygame
import pygame_menu
import random
import time
import sys
from pygame.locals import *
random.seed(time.time())
#############
# AUTO VARIABLES
#############
WIDTH = 400
TOTAL_HEIGHT = 400
GRID_HEIGHT = 300
LABEL_SCORE_HEIGTH = TOTAL_HEIGHT-GRID_HEIGHT
SQUARE_SIDE = 20
RIGHT=0
LEFT=1
UP=2
DOWN=3
BLACK = (0,0,0)
LIGHT_GREEN = (168, 222, 53)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
rows = int(GRID_HEIGHT/20)
cols = int(WIDTH/20)
red_square = []
score = 0
checkpoint_sound = 0
game_over_sound = 0
class Snake():
def __init__(self):
self.dir = RIGHT
self.body = [[0, 0], [-20, 0], [-40, 0], [-60, 0]]
self.eat_sound = pygame.mixer.Sound(eat_apple_sound_path)
def move(self, objetive):
global screen, score
head = self.body[0]
if self.dir == RIGHT:
if self.body[0][0] < (WIDTH - SQUARE_SIDE):
self.body.insert(0, [head[0] + SQUARE_SIDE, head[1]])
else:
return False
if self.dir == LEFT:
if self.body[0][0] > 0:
self.body.insert(0, [head[0] - SQUARE_SIDE, head[1]])
else:
return False
if self.dir == UP:
if self.body[0][1] > 0:
self.body.insert(0, [head[0], head[1] - SQUARE_SIDE])
else:
return False
if self.dir == DOWN:
if self.body[0][1] < (GRID_HEIGHT - SQUARE_SIDE):
self.body.insert(0, [head[0], head[1] + SQUARE_SIDE])
else:
return False
if self.body[0][0] == objetive[0][0] and self.body[0][1] == objetive[0][1]:
self.eat(objetive)
score += 1
if(score%10 == 0 and score != 0):
pygame.mixer.Sound.play(checkpoint_sound)
pygame.mixer.Sound.play(self.eat_sound)
else:
self.body.pop()
return True
def eat(self, objetive):
objetive.pop()
def set_direction(self, dir):
self.dir = dir
def get_direction(self):
return self.dir
def draw(self):
global screen
for coord in self.body:
pygame.draw.rect(screen, LIGHT_GREEN, [coord[0],coord[1], SQUARE_SIDE, SQUARE_SIDE])
def getHead(self):
return self.body[0]
def isAutoShock(self):
return self.getHead() in self.body[1:]
def showCoords(self):
print(self.body)
def drawGrid():
global screen
for i in range(rows):
for j in range(cols):
xi = j*SQUARE_SIDE
yi = i*SQUARE_SIDE
xf = SQUARE_SIDE*(j+1)
yf = SQUARE_SIDE*(i+1)
pygame.draw.rect(screen, BLUE ,[xi,yi,xf,yf],1)
def show_score(text):
font = pygame.font.Font('freesansbold.ttf', 32)
render_text = font.render(text, True, BLACK)
text_rect = render_text.get_rect(center=(WIDTH/2, TOTAL_HEIGHT-(LABEL_SCORE_HEIGTH/2)))
screen.blit(render_text, text_rect)
def show_checkpoint(text):
font = pygame.font.Font('freesansbold.ttf', 75)
render_text = font.render(text, True, RED)
text_rect = render_text.get_rect(center=(WIDTH/2, TOTAL_HEIGHT-(LABEL_SCORE_HEIGTH/2)))
screen.blit(render_text, text_rect)
def draw_label_score():
pygame.draw.rect(screen, WHITE, [0, GRID_HEIGHT, WIDTH, TOTAL_HEIGHT])
if(score%10 == 0 and score != 0):
show_checkpoint(str(score))
else:
show_score("Score: "+str(score))
def generateSquare(square):
x = random.randint(0, cols - 1)
y = random.randint(0, rows - 1)
square.append((x*SQUARE_SIDE, y*SQUARE_SIDE))
def draw_square(location, color):
global screen
pygame.draw.rect(screen, color, [location[0][0], location[0][1], SQUARE_SIDE, SQUARE_SIDE])
def draw_snake(snake):
snake.draw()
def draw_background(color):
screen.fill(color)
def game_init():
""" initializing parameters """
global snake, red_square, score, checkpoint_sound, game_over_sound
checkpoint_sound = pygame.mixer.Sound(checkpoint_sound_path)
game_over_sound = pygame.mixer.Sound(game_over_sound_path)
score = 0
snake = Snake()
generateSquare(square=red_square)
def game_loop():
""" the principal loop of game """
global score
pygame.mixer.music.load(music_sound_path)
pygame.mixer.music.play(-1)
end = False
clock = pygame.time.Clock()
while not end:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
end = True
if event.key == K_a and snake.get_direction() != RIGHT:
snake.set_direction(LEFT)
elif event.key == K_d and snake.get_direction() != LEFT:
snake.set_direction(RIGHT)
elif event.key == K_s and snake.get_direction() != UP:
snake.set_direction(DOWN)
elif event.key == K_w and snake.get_direction() != DOWN:
snake.set_direction(UP)
break # it avoids bug control (3 keys in time)
elif event.type == QUIT:
end = True
# Moving the snake
game_state = snake.move(objetive=red_square)
if snake.isAutoShock():
game_state = False
# Test if GAME OVER
if game_state == False:
pygame.mixer.music.stop()
pygame.mixer.Sound.play(game_over_sound)
time.sleep(1)
end = True
# if there isn't Objetive square, it generates another
if len(red_square) == 0:
generateSquare(square=red_square)
# Draw functions
draw_background(color=WHITE)
drawGrid()
draw_snake(snake)
draw_square(location=red_square, color=RED)
draw_label_score()
pygame.display.flip()
clock.tick(10)
red_square.pop()
# MAIN FUNCTION
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((WIDTH, TOTAL_HEIGHT))
pygame.display.set_caption("Snake Game")
def start_game():
game_init()
game_loop()
menu = pygame_menu.Menu(TOTAL_HEIGHT, WIDTH, "SNAKE", theme=pygame_menu.themes.THEME_BLUE)
menu.add_button('Play', start_game)
menu.add_button('Quit', pygame_menu.events.EXIT)
menu.mainloop(screen)