-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.py
186 lines (151 loc) · 6.19 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
"""
Pygame Snake!
(My first Python program!)
Made by Pi
A simple Snake ripoff I made with Pygame.
May be buggy and unoptimised as hell, but any help would be welcome.
And no, I don't care if you change something.
"""
import pygame
import random
pygame.font.init()
# Set the window settings
WIDTH, HEIGHT = 400, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Python Snake")
step = 10 # The grid in which the game runs on
class Snake:
def __init__(self, x, y):
self.size = 10
self.x = x
self.y = y
def draw(self, window): # Draw the snake's piece onto the screen
pygame.draw.rect(window, (0, 196, 72), (self.x, self.y, self.size, self.size))
def get_x(self):
return self.x
def get_y(self):
return self.y
class Food:
def __init__(self):
self.size = 10
self.x = random.randrange(0, WIDTH - self.size, step)
self.y = random.randrange(0, HEIGHT - self.size, step)
def respawn(self): # Set new random coordinates for the food
self.x = random.randrange(0, WIDTH - self.size, step)
self.y = random.randrange(0, HEIGHT - self.size, step)
def draw(self, window): # Draw the food to the screen
pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, self.size, self.size))
def get_x(self):
return self.x
def get_y(self):
return self.y
def main():
# Set everything right before starting
snake_body = []
clock = pygame.time.Clock()
food = Food()
snake_size = 4
direction = 'right'
new_x, new_y = 250, 250
alive = True
flag = True
score = 0
score_font = pygame.font.SysFont('Candara', 15, 1)
def head(window, direction, xx, yy): # The eyes of the snake
if direction == 'up' or direction == 'down':
pygame.draw.rect(window, (255, 255, 255), (xx + 1, yy + 4, 3, 3))
pygame.draw.rect(window, (255, 255, 255), (xx + 6, yy + 4, 3, 3))
elif direction == 'left' or direction == 'right':
pygame.draw.rect(window, (255, 255, 255), (xx + 4, yy + 1, 3, 3))
pygame.draw.rect(window, (255, 255, 255), (xx + 4, yy + 6, 3, 3))
def render(direction): # Draw everything to the screen
pygame.draw.rect(WIN, (0, 0, 0), (0, 0, WIDTH, HEIGHT))
score_label = score_font.render(f'Score: {score}', 1, (255, 255, 255))
WIN.blit(score_label, (10, 10))
if len(snake_body) <= snake_size:
snake = Snake(new_x, new_y)
snake_body.insert(0, snake)
if len(snake_body) > snake_size:
snake_body.pop()
for snake in snake_body:
snake.draw(WIN)
food.draw(WIN)
head(WIN, direction, new_x, new_y)
pygame.display.update()
def keyboard(keys, direction): # Avoids the snake from ramming into itself
yes = False
if keys[pygame.K_DOWN] and direction != 'up':
yes = True
if keys[pygame.K_UP] and direction != 'down':
yes = True
if keys[pygame.K_RIGHT] and direction != 'left':
yes = True
if keys[pygame.K_LEFT] and direction != 'right':
yes = True
return yes
while alive:
clock.tick(10)
render(direction)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
keys = pygame.key.get_pressed() # The snake's movement
if keyboard(keys, direction):
if keys[pygame.K_DOWN] and direction != 'up':
new_y += step
direction = 'down'
elif keys[pygame.K_UP] and direction != 'down':
new_y -= step
direction = 'up'
elif keys[pygame.K_LEFT] and direction != 'right':
new_x -= step
direction = 'left'
elif keys[pygame.K_RIGHT] and direction != 'left':
new_x += step
direction = 'right'
else: # The snake's automatic movement
if direction == 'down':
new_y += step
elif direction == 'up':
new_y -= step
elif direction == 'left':
new_x -= step
elif direction == 'right':
new_x += step
if new_x < 0 or new_x > WIDTH or new_y > HEIGHT or new_y < 0:
high_score = score # Kill the snake if it leaves the screen
alive = False
if new_x == food.get_x() and new_y == food.get_y():
food.respawn() # Move the food to a new place
snake_size += 1 # Make the snake longer
score += 1
if not flag: # Only work after the first frame
for snake in snake_body:
if new_x == snake.get_x() and new_y == snake.get_y():
alive = False # Kill the snake if it hits itself
high_score = score
flag = False # After the first frame, start checking for collisions
return (high_score, score)
def main_menu():
font = pygame.font.SysFont("Candara", 25, 1)
high_font = pygame.font.SysFont('Candara', 15, 1)
high = old_high = scr = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
old_high = high
ret = main()
high, scr = ret[0], ret[1]
if high < old_high:
high = old_high
high_label = high_font.render(f'High Score: {high}', 1, (255, 255, 255))
score_label = high_font.render(f'Last Game: {scr}', 1, (255, 255, 255))
pygame.draw.rect(WIN, (4, 71, 17), (0, 0, WIDTH, HEIGHT))
title = font.render('Pygame Snake! Click to continue', 1, (255, 255, 255))
WIN.blit(title, (WIDTH/2 - title.get_width()/2, HEIGHT/2 - title.get_height()))
WIN.blit(high_label, (10, 10))
WIN.blit(score_label, (WIDTH - score_label.get_width() - 10, 10))
pygame.display.update()
main_menu()