-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
237 lines (198 loc) · 6.95 KB
/
main.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
"""Snake Game
This is a remake of the classic 80's game Snake
programmed in python using the pygame module.
Control the snake using the arrow keys to eat the food.
Every time the snake eats an apple the user's score
increases by 1. When the score reaches a multiple of 10,
the user advances to the next level where the speed of the
snake is increased by 4.
"""
import random
import pygame
pygame.init()
# Set Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# Create window and set attributes
dimensions = (600, 400)
root = pygame.display.set_mode(dimensions)
pygame.display.set_caption("Snake Game")
img = pygame.image.load("img\icon.png")
pygame.display.set_icon(img)
clock = pygame.time.Clock()
# Set snake attributes
snake_size = 12
snake_speed = 12
scores = []
# Set fonts
msg_font = pygame.font.SysFont("Consolas", 20)
score_font = pygame.font.SysFont("Consolas", 25)
level = 1
# Display game information
def display_score(score):
score_text = score_font.render(f"Score: {str(score)}", True, white)
if scores == []:
high_score_text = score_font.render("High Score: ", True, white)
else:
high_score_text = score_font.render(
f"High Score: {str(max(scores))}", True, white
)
level_text = score_font.render(f"Level {str(level)}", True, white)
root.blit(score_text, [0, 0])
root.blit(high_score_text, [210, 0])
root.blit(level_text, [500, 0])
# Draw snake
def draw_snake(snake_size, snake_pxls):
for pxl in snake_pxls:
global snake
snake = pygame.draw.rect(root, green, [pxl[0], pxl[1], snake_size, snake_size])
# Set key variables
l_active = True
r_active = True
u_active = True
d_active = True
def run():
# Set game variables
global l_active
global r_active
global u_active
global d_active
global snake_speed
global level
game_over = False
game_closed = False
x = dimensions[0] / 2
y = dimensions[1] / 2
x_speed = 0
y_speed = 0
snake_pxls = []
snake_len = 1
food_x = round(random.randrange(0, dimensions[0] - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, dimensions[1] - snake_size) / 10.0) * 10.0
# Main game loop
while not game_over:
while game_closed:
global l_active
global r_active
global u_active
global d_active
global scores
global level
global snake_speed
level = 1
snake_speed = 12
l_active = True
r_active = True
u_active = True
d_active = True
scores.append(snake_len - 1)
root.fill(black)
game_over_msg = msg_font.render(
"Game Over! Would you like to start a new game? (y/n)", True, red
)
root.blit(game_over_msg, [20, 150])
display_score(snake_len - 1)
pygame.display.flip()
# Get keyboard input after game over
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
run()
if event.key == pygame.K_n:
game_over = True
game_closed = False
if event.type == pygame.QUIT:
game_over = True
game_closed = False
# Get main keyboard input
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if l_active:
r_active = False
u_active = True
d_active = True
x_speed = -snake_size
y_speed = 0
else:
pass
if event.key == pygame.K_RIGHT:
if r_active:
l_active = False
u_active = True
d_active = True
x_speed = snake_size
y_speed = 0
else:
pass
if event.key == pygame.K_UP:
if u_active:
d_active = False
l_active = True
r_active = True
x_speed = 0
y_speed = -snake_size
else:
pass
if event.key == pygame.K_DOWN:
if d_active:
u_active = False
l_active = True
r_active = True
x_speed = 0
y_speed = snake_size
else:
pass
# Game over if snake touches border
if x >= dimensions[0] or x < 0 or y >= dimensions[1] or y < 0:
pygame.mixer.music.load("sfx\game_over.mp3")
pygame.mixer.music.play()
game_closed = True
# Moves the snake
x += x_speed
y += y_speed
# Background
root.fill(black)
# Draw apple
food = pygame.draw.rect(root, red, [food_x, food_y, snake_size, snake_size])
snake_pxls.append([x, y])
# Make sure the snake doesn't keep growing
if len(snake_pxls) > snake_len:
del snake_pxls[0]
# Game over if snake touches itself
for pxl in snake_pxls[:-1]:
if pxl == [x, y]:
pygame.mixer.music.load("sfx\game_over.mp3")
pygame.mixer.music.play()
game_closed = True
# Draw everything
draw_snake(snake_size, snake_pxls)
display_score(snake_len - 1)
# Detect snake eating apple
collide = snake.colliderect(food)
if collide:
pygame.mixer.music.load("sfx\eat.mp3")
pygame.mixer.music.set_volume(1)
pygame.mixer.music.play()
food_x = (
round(random.randrange(0, dimensions[0] - snake_size) / 10.0) * 10.0
)
food_y = (
round(random.randrange(0, dimensions[1] - snake_size) / 10.0) * 10.0
)
snake_len += 1
if (snake_len - 1) % 10 == 0 and snake_len - 1 != 0:
pygame.mixer.music.load("sfx\level_up.mp3")
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play()
snake_speed += 3
level += 1
clock.tick(snake_speed)
pygame.display.flip()
pygame.quit()
quit()
run()