-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game.py
More file actions
147 lines (118 loc) · 4.21 KB
/
snake_game.py
File metadata and controls
147 lines (118 loc) · 4.21 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
import pygame
import random
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 600, 400
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game by Abhee")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Game settings
BLOCK_SIZE = 20
BASE_SPEED = 10
# Fonts
FONT = pygame.font.SysFont("bahnschrift", 25)
SCORE_FONT = pygame.font.SysFont("comicsansms", 25)
class Snake:
def __init__(self):
self.body = [[WIDTH // 2, HEIGHT // 2]]
self.direction = [0, 0]
self.grow = False
def move(self):
head = [self.body[-1][0] + self.direction[0], self.body[-1][1] + self.direction[1]]
self.body.append(head)
if not self.grow:
self.body.pop(0)
else:
self.grow = False
def change_direction(self, dx, dy):
# Prevent reverse direction
if len(self.body) > 1 and [dx, dy] == [-self.direction[0], -self.direction[1]]:
return
self.direction = [dx, dy]
def check_collision(self):
head = self.body[-1]
# Wall collision
if head[0] < 0 or head[0] >= WIDTH or head[1] < 0 or head[1] >= HEIGHT:
return True
# Self collision
if head in self.body[:-1]:
return True
return False
def draw(self):
for segment in self.body:
pygame.draw.rect(SCREEN, GREEN, [segment[0], segment[1], BLOCK_SIZE, BLOCK_SIZE])
def draw_food(position):
pygame.draw.rect(SCREEN, RED, [position[0], position[1], BLOCK_SIZE, BLOCK_SIZE])
def random_food():
x = random.randrange(0, WIDTH - BLOCK_SIZE, BLOCK_SIZE)
y = random.randrange(0, HEIGHT - BLOCK_SIZE, BLOCK_SIZE)
return [x, y]
def show_score(score, high_score):
score_text = SCORE_FONT.render(f"Score: {score} High Score: {high_score}", True, BLACK)
SCREEN.blit(score_text, [10, 10])
def game_loop():
clock = pygame.time.Clock()
snake = Snake()
food = random_food()
score = 0
high_score = 0
speed = BASE_SPEED
running = True
while running:
SCREEN.fill(WHITE)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake.change_direction(-BLOCK_SIZE, 0)
elif event.key == pygame.K_RIGHT:
snake.change_direction(BLOCK_SIZE, 0)
elif event.key == pygame.K_UP:
snake.change_direction(0, -BLOCK_SIZE)
elif event.key == pygame.K_DOWN:
snake.change_direction(0, BLOCK_SIZE)
# Move snake
snake.move()
# Check collision
if snake.check_collision():
SCREEN.fill(WHITE)
msg = FONT.render("Game Over! Press C to Play Again or Q to Quit", True, RED)
SCREEN.blit(msg, [WIDTH / 6, HEIGHT / 3])
show_score(score, high_score)
pygame.display.update()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
waiting = False
running = False
elif event.key == pygame.K_c:
game_loop()
break
# Check if food eaten
if snake.body[-1] == food:
snake.grow = True
score += 1
high_score = max(high_score, score)
food = random_food()
speed += 0.5 # Increase speed gradually
# Draw everything
snake.draw()
draw_food(food)
show_score(score, high_score)
pygame.display.update()
clock.tick(speed)
if __name__ == "__main__":
game_loop()
pygame.quit()