-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake-game.py
192 lines (152 loc) · 5.32 KB
/
snake-game.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
import pygame
import time
import random
# Initialize pygame
pygame.init()
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
GREEN = (0, 255, 0)
BLUE = (50, 153, 213)
DARK_GREEN = (0, 155, 0)
# Screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
# Snake settings
SNAKE_BLOCK = 10
SNAKE_SPEED = 15
# Fonts
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# Game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Snake Game')
# Button class for Start, Restart, Finish
class Button:
def __init__(self, x, y, w, h, text, color, hover_color, action=None):
self.rect = pygame.Rect(x, y, w, h)
self.text = text
self.color = color
self.hover_color = hover_color
self.action = action
def draw(self):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if self.rect.collidepoint(mouse):
pygame.draw.rect(screen, self.hover_color, self.rect)
if click[0] == 1 and self.action:
self.action()
else:
pygame.draw.rect(screen, self.color, self.rect)
# Display button text
text_surf = font_style.render(self.text, True, WHITE)
screen.blit(text_surf, (self.rect.x + (self.rect.width - text_surf.get_width()) // 2,
self.rect.y + (self.rect.height - text_surf.get_height()) // 2))
# Function to display score
def your_score(score):
value = score_font.render(f"Your Score: {score}", True, WHITE)
screen.blit(value, [0, 0])
# Function to draw snake with a rounded or segmented look
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.circle(screen, GREEN, (x[0] + snake_block // 2, x[1] + snake_block // 2), snake_block // 2)
# Function to display a message
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [SCREEN_WIDTH / 6, SCREEN_HEIGHT / 3])
# Function to handle the game loop
def game_loop():
game_over = False
game_close = False
# Initial position of the snake
x1 = SCREEN_WIDTH / 2
y1 = SCREEN_HEIGHT / 2
# Movement
x1_change = 0
y1_change = 0
# Snake body
snake_list = []
length_of_snake = 1
# Food
foodx = round(random.randrange(0, SCREEN_WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
foody = round(random.randrange(0, SCREEN_HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
# Clock for controlling the speed
clock = pygame.time.Clock()
while not game_over:
while game_close:
screen.fill(BLUE)
message("You Lost! Press R-Restart or Q-Quit", RED)
your_score(length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_r:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -SNAKE_BLOCK
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = SNAKE_BLOCK
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -SNAKE_BLOCK
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = SNAKE_BLOCK
x1_change = 0
if x1 >= SCREEN_WIDTH or x1 < 0 or y1 >= SCREEN_HEIGHT or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(BLUE)
# Draw food
pygame.draw.rect(screen, RED, [foodx, foody, SNAKE_BLOCK, SNAKE_BLOCK])
# Snake movement
snake_head = [x1, y1]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
our_snake(SNAKE_BLOCK, snake_list)
your_score(length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, SCREEN_WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
foody = round(random.randrange(0, SCREEN_HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
length_of_snake += 1
clock.tick(SNAKE_SPEED)
pygame.quit()
quit()
# Button actions
def start_game():
game_loop()
def quit_game():
pygame.quit()
quit()
# Main menu
def main_menu():
screen.fill(BLUE)
start_button = Button(200, 150, 200, 50, "Start", DARK_GREEN, GREEN, start_game)
quit_button = Button(200, 250, 200, 50, "Quit", RED, DARK_GREEN, quit_game)
running = True
while running:
screen.fill(BLUE)
start_button.draw()
quit_button.draw()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if __name__ == "__main__":
main_menu()