-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
347 lines (290 loc) · 12.8 KB
/
Copy pathmain.py
File metadata and controls
347 lines (290 loc) · 12.8 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import pygame
import random
import json
import os
from enum import Enum
pygame.init()
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
green = (0, 255, 0)
yellow = (255, 255, 0)
blue = (0, 0, 255)
dark_green = (0, 155, 0)
gray = (128, 128, 128)
# Game States
class GameState(Enum):
MENU = 1
DIFFICULTY = 2
PLAYING = 3
PAUSED = 4
GAME_OVER = 5
# Difficulty Levels
class Difficulty(Enum):
EASY = 1
MEDIUM = 2
HARD = 3
EXTREME = 4
DIFFICULTY_SETTINGS = {
Difficulty.EASY: {"speed": 4, "color": green},
Difficulty.MEDIUM: {"speed": 8, "color": yellow},
Difficulty.HARD: {"speed": 12, "color": red},
Difficulty.EXTREME: {"speed": 16, "color": (255, 0, 255)}
}
# Creating window
screen_width = 800
screen_height = 600
gameWindow = pygame.display.set_mode((screen_width, screen_height))
# Game Title
pygame.display.set_caption("🐍 Advanced Snake Game")
pygame.display.update()
clock = pygame.time.Clock()
font_small = pygame.font.SysFont(None, 20)
font_medium = pygame.font.SysFont(None, 40)
font_large = pygame.font.SysFont(None, 60)
# High Score File
HIGH_SCORE_FILE = "highscore.json"
def text_screen(text, color, x, y, font=None):
if font is None:
font = font_small
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [x, y])
def plot_snake(gameWindow, color, snk_list, snake_size):
for x, y in snk_list:
pygame.draw.rect(gameWindow, color, [x, y, snake_size, snake_size])
pygame.draw.rect(gameWindow, black, [x, y, snake_size, snake_size], 2)
class HighScoreManager:
"""Manages high scores persistence"""
@staticmethod
def load_high_score():
if os.path.exists(HIGH_SCORE_FILE):
try:
with open(HIGH_SCORE_FILE, 'r') as f:
data = json.load(f)
return data.get("high_score", 0)
except:
return 0
return 0
@staticmethod
def save_high_score(score):
with open(HIGH_SCORE_FILE, 'w') as f:
json.dump({"high_score": score}, f)
def draw_menu():
"""Draw main menu screen"""
gameWindow.fill(white)
text_screen("🐍 SNAKE GAME 🐍", black, screen_width // 2 - 150, 50, font_large)
text_screen("Press S to Start", black, screen_width // 2 - 100, 180, font_medium)
text_screen("Press Q to Quit", black, screen_width // 2 - 100, 250, font_medium)
high_score = HighScoreManager.load_high_score()
text_screen(f"High Score: {high_score * 10}", red, screen_width // 2 - 100, 380, font_small)
pygame.display.update()
def draw_difficulty_menu():
"""Draw difficulty selection screen"""
gameWindow.fill(white)
text_screen("SELECT DIFFICULTY", black, screen_width // 2 - 150, 50, font_large)
text_screen("1 - EASY", green, screen_width // 2 - 100, 150, font_medium)
text_screen("2 - MEDIUM", yellow, screen_width // 2 - 100, 220, font_medium)
text_screen("3 - HARD", red, screen_width // 2 - 100, 290, font_medium)
text_screen("4 - EXTREME", (255, 0, 255), screen_width // 2 - 100, 360, font_medium)
text_screen("Q - Back to Menu", gray, screen_width // 2 - 100, 450, font_small)
pygame.display.update()
def draw_paused_screen():
"""Draw pause menu"""
gameWindow.fill(black)
gameWindow.set_alpha(128)
overlay = pygame.Surface((screen_width, screen_height))
overlay.set_alpha(128)
overlay.fill(black)
gameWindow.blit(overlay, (0, 0))
text_screen("PAUSED", white, screen_width // 2 - 80, screen_height // 2 - 50, font_large)
text_screen("Press P to Resume", white, screen_width // 2 - 120, screen_height // 2 + 50, font_small)
pygame.display.update()
def draw_game_over_screen(score, high_score):
"""Draw game over screen with score"""
gameWindow.fill(white)
text_screen("GAME OVER!", red, screen_width // 2 - 100, 100, font_large)
text_screen(f"Final Score: {score * 10}", black, screen_width // 2 - 150, 200, font_medium)
if score * 10 > high_score * 10:
text_screen("NEW HIGH SCORE!", red, screen_width // 2 - 150, 280, font_medium)
else:
text_screen(f"High Score: {high_score * 10}", black, screen_width // 2 - 150, 280, font_small)
text_screen("Press ENTER to Replay", black, screen_width // 2 - 140, 380, font_small)
text_screen("Press M for Menu", black, screen_width // 2 - 140, 420, font_small)
text_screen("Press Q to Quit", black, screen_width // 2 - 140, 460, font_small)
pygame.display.update()
def gameloop():
"""Main game loop with state management"""
# Game specific variables
game_state = GameState.MENU
exit_game = False
snake_x = 45
snake_y = 55
velocity_x = 0
velocity_y = 0
snk_list = []
snk_length = 1
score = 0
snake_size = 20
food_x = random.randint(50, screen_width - 50)
food_y = random.randint(50, screen_height - 50)
difficulty = Difficulty.MEDIUM
fps = DIFFICULTY_SETTINGS[difficulty]["speed"]
snake_color = DIFFICULTY_SETTINGS[difficulty]["color"]
high_score = HighScoreManager.load_high_score()
while not exit_game:
if game_state == GameState.MENU:
draw_menu()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
game_state = GameState.DIFFICULTY
if event.key == pygame.K_q:
exit_game = True
elif game_state == GameState.DIFFICULTY:
draw_difficulty_menu()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
difficulty = Difficulty.EASY
game_state = GameState.PLAYING
snake_x, snake_y = 45, 55
velocity_x, velocity_y = 0, 0
snk_list, snk_length = [], 1
score = 0
food_x = random.randint(50, screen_width - 50)
food_y = random.randint(50, screen_height - 50)
fps = DIFFICULTY_SETTINGS[difficulty]["speed"]
snake_color = DIFFICULTY_SETTINGS[difficulty]["color"]
elif event.key == pygame.K_2:
difficulty = Difficulty.MEDIUM
game_state = GameState.PLAYING
snake_x, snake_y = 45, 55
velocity_x, velocity_y = 0, 0
snk_list, snk_length = [], 1
score = 0
food_x = random.randint(50, screen_width - 50)
food_y = random.randint(50, screen_height - 50)
fps = DIFFICULTY_SETTINGS[difficulty]["speed"]
snake_color = DIFFICULTY_SETTINGS[difficulty]["color"]
elif event.key == pygame.K_3:
difficulty = Difficulty.HARD
game_state = GameState.PLAYING
snake_x, snake_y = 45, 55
velocity_x, velocity_y = 0, 0
snk_list, snk_length = [], 1
score = 0
food_x = random.randint(50, screen_width - 50)
food_y = random.randint(50, screen_height - 50)
fps = DIFFICULTY_SETTINGS[difficulty]["speed"]
snake_color = DIFFICULTY_SETTINGS[difficulty]["color"]
elif event.key == pygame.K_4:
difficulty = Difficulty.EXTREME
game_state = GameState.PLAYING
snake_x, snake_y = 45, 55
velocity_x, velocity_y = 0, 0
snk_list, snk_length = [], 1
score = 0
food_x = random.randint(50, screen_width - 50)
food_y = random.randint(50, screen_height - 50)
fps = DIFFICULTY_SETTINGS[difficulty]["speed"]
snake_color = DIFFICULTY_SETTINGS[difficulty]["color"]
elif event.key == pygame.K_q:
game_state = GameState.MENU
elif game_state == GameState.PAUSED:
draw_paused_screen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
game_state = GameState.PLAYING
elif game_state == GameState.PLAYING:
# KEY MAPPINGS
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
game_state = GameState.PAUSED
elif event.key == pygame.K_RIGHT:
if velocity_x == 0:
velocity_x = 1
velocity_y = 0
elif event.key == pygame.K_LEFT:
if velocity_x == 0:
velocity_x = -1
velocity_y = 0
elif event.key == pygame.K_UP:
if velocity_y == 0:
velocity_y = -1
velocity_x = 0
elif event.key == pygame.K_DOWN:
if velocity_y == 0:
velocity_y = 1
velocity_x = 0
snake_x = snake_x + velocity_x * snake_size
snake_y = snake_y + velocity_y * snake_size
# Check food collision
if abs(snake_x - food_x) < snake_size and abs(snake_y - food_y) < snake_size:
score += 1
food_x = random.randint(50, screen_width - 50)
food_y = random.randint(50, screen_height - 50)
snk_length += 1
# Increase difficulty as score increases
if score % 5 == 0:
fps = min(fps + 1, 20)
gameWindow.fill(white)
# Draw border
pygame.draw.rect(gameWindow, black, [0, 0, screen_width, screen_height], 3)
# Draw score and difficulty
text_screen(f"Score: {score * 10}", red, 10, 10, font_small)
text_screen(f"High Score: {high_score * 10}", blue, 10, 40, font_small)
difficulty_text = difficulty.name
text_screen(f"Difficulty: {difficulty_text}", snake_color, screen_width - 200, 10, font_small)
text_screen("P - Pause | Q - Quit", gray, screen_width - 200, 40, font_small)
# Draw food
pygame.draw.rect(gameWindow, red, [food_x, food_y, snake_size, snake_size])
pygame.draw.rect(gameWindow, dark_green, [food_x, food_y, snake_size, snake_size], 2)
# Update snake
head = [snake_x, snake_y]
snk_list.append(head)
if len(snk_list) > snk_length:
del snk_list[0]
# Check collision with self
if head in snk_list[:-1]:
if score * 10 > high_score * 10:
high_score = score
HighScoreManager.save_high_score(high_score)
game_state = GameState.GAME_OVER
# Check boundary collision
if snake_x < 0 or snake_x > screen_width - snake_size or snake_y < 0 or snake_y > screen_height - snake_size:
if score * 10 > high_score * 10:
high_score = score
HighScoreManager.save_high_score(high_score)
game_state = GameState.GAME_OVER
plot_snake(gameWindow, snake_color, snk_list, snake_size)
pygame.display.update()
clock.tick(fps)
continue
elif game_state == GameState.GAME_OVER:
draw_game_over_screen(score, high_score)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
game_state = GameState.DIFFICULTY
elif event.key == pygame.K_m:
game_state = GameState.MENU
elif event.key == pygame.K_q:
exit_game = True
clock.tick(60)
pygame.quit()
quit()
if __name__ == "__main__":
gameloop()