-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathsnake.py
276 lines (218 loc) · 7.95 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
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
# -----------------------------------------------------------------------------
#
# Snake - 2D
# Language - Python
# Modules - pygame, sys, random, copy, time
#
# Controls - Arrow Keys
#
# By - Jatin Kumar Mandav
#
# Website - https://jatinmandav.wordpress.com
#
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
# Twitter - @jatinmandav
#
# -----------------------------------------------------------------------------
import pygame
import sys
import copy
import random
import time
pygame.init()
width = 500
height = 500
scale = 10
score = 0
food_x = 10
food_y = 10
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()
background = (23, 32, 42)
snake_colour = (236, 240, 241)
food_colour = (148, 49, 38)
snake_head = (247, 220, 111)
# ----------- Snake Class ----------------
# self.history[0][0] is the location of the head of the snake
class Snake:
def __init__(self, x_start, y_start):
self.x = x_start
self.y = y_start
self.w = 10
self.h = 10
self.x_dir = 1
self.y_dir = 0
self.history = [[self.x, self.y]]
self.length = 1
def reset(self):
self.x = width/2-scale
self.y = height/2-scale
self.w = 10
self.h = 10
self.x_dir = 1
self.y_dir = 0
self.history = [[self.x, self.y]]
self.length = 1
#function to show the body of snake
def show(self):
for i in range(self.length):
if not i == 0:
pygame.draw.rect(display, snake_colour, (self.history[i][0], self.history[i][1], self.w, self.h))
else:
pygame.draw.rect(display, snake_head, (self.history[i][0], self.history[i][1], self.w, self.h))
def check_eaten(self):
if abs(self.history[0][0] - food_x) < scale and abs(self.history[0][1] - food_y) < scale:
return True
def grow(self):
self.length += 1
self.history.append(self.history[self.length-2])
def death(self):
i = self.length - 1
while i > 0:
if abs(self.history[0][0] - self.history[i][0]) < self.w and abs(self.history[0][1] - self.history[i][1]) < self.h and self.length > 2:
return True
i -= 1
def update(self):
i = self.length - 1
while i > 0:
self.history[i] = copy.deepcopy(self.history[i-1])
i -= 1
self.history[0][0] += self.x_dir*scale
self.history[0][1] += self.y_dir*scale
def autoplay(self):
if abs(food_x-self.history[0][0]) < 10 and abs(food_y-self.history[0][1]) < 10:
# if self.check_eaten():
# food.new_location()
# score += 1
# self.grow()
print("")
elif abs(food_x-self.history[0][0]) < 10:
# if self.y_dir==1 or self.y_dir==-1:
# self.y_dir=0
# self.x_dir=1
if self.x_dir==1 or self.x_dir==-1:
if food_y>self.history[0][1]:
self.y_dir=1
else:
self.y_dir=-1
self.x_dir=0
elif abs(food_y-self.history[0][1]) < 10 :
# if self.x_dir==1 or self.x_dir==-1:
# self.x_dir=0
# self.y_dir=1
if self.y_dir==1 or self.y_dir==-1:
self.y_dir=0
if food_x>self.history[0][0]:
self.x_dir=1
else:
self.x_dir=-1
elif food_x-self.history[0][0] >= 10 and food_y-self.history[0][1] >= 10:
if self.x_dir==-1:
self.y_dir=1
self.x_dir=0
elif self.y_dir==-1:
self.y_dir=0
self.x_dir=1
elif self.history[0][0]-food_x >= 10 and food_y-self.history[0][1] >= 10:
if self.x_dir==1:
self.y_dir=1
self.x_dir=0
elif self.y_dir==1:
self.y_dir=0
self.x_dir=-1
elif self.history[0][0]-food_x >= 10 and self.history[0][1]-food_y >= 10:
if self.x_dir==1:
self.y_dir=-1
self.x_dir=0
elif self.y_dir==1:
self.y_dir=0
self.x_dir=-1
elif food_x-self.history[0][0] >= 10 and self.history[0][1]-food_y >= 10:
if self.x_dir==-1:
self.y_dir=-1
self.x_dir=0
elif self.y_dir==1:
self.y_dir=0
self.x_dir=1
self.update()
# ----------- Food Class --------------
class Food:
def new_location(self):
global food_x, food_y
food_x = random.randrange(1, width/scale-1)*scale
food_y = random.randrange(1, height/scale-1)*scale
def show(self):
pygame.draw.rect(display, food_colour, (food_x, food_y, scale, scale))
def show_score():
font = pygame.font.SysFont("Copperplate Gothic Bold", 20)
text = font.render("Score: " + str(score), True, snake_colour)
display.blit(text, (scale, scale))
# ----------- Main Game Loop -------------
def gameLoop():
loop = True
global score
snake = Snake(width/2, height/2) #starting from mid of grid
food = Food()
food.new_location()
ap=False
while loop:
display.fill(background)
snake.show()
food.show()
show_score()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.key==pygame.K_SPACE: #autoplay start
ap=True
if event.key==pygame.K_TAB: #autoplay end
ap=False
else:
if snake.y_dir == 0:
if event.key == pygame.K_UP:
snake.x_dir = 0
snake.y_dir = -1
if event.key == pygame.K_DOWN:
snake.x_dir = 0
snake.y_dir = 1
if snake.x_dir == 0:
if event.key == pygame.K_LEFT:
snake.x_dir = -1
snake.y_dir = 0
if event.key == pygame.K_RIGHT:
snake.x_dir = 1
snake.y_dir = 0
if ap:
snake.autoplay()
else:
snake.update()
if snake.check_eaten():
food.new_location()
score += 1
snake.grow()
if snake.death():
score = 0
font = pygame.font.SysFont("Copperplate Gothic Bold", 50)
text = font.render("Game Over!", True, snake_colour)
display.blit(text, (width/2-50, height/2))
pygame.display.update()
time.sleep(3)
snake.reset()
#updating the values if snake goes out of board
if snake.history[0][0] > width:
snake.history[0][0] = 0
if snake.history[0][0] < 0:
snake.history[0][0] = width
if snake.history[0][1] > height:
snake.history[0][1] = 0
if snake.history[0][1] < 0:
snake.history[0][1] = height
pygame.display.update()
clock.tick(10) #at most 10 frames should pass in 1 sec, it is used to control the speed of snake
gameLoop()