Skip to content

Commit 4c0b5aa

Browse files
committed
Autoplay Added
1 parent 4eef6cc commit 4c0b5aa

File tree

1 file changed

+119
-25
lines changed

1 file changed

+119
-25
lines changed

Snake_2d/snake.py

Lines changed: 119 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242

4343
# ----------- Snake Class ----------------
44+
# self.history[0][0] is the location of the head of the snake
45+
4446
class Snake:
4547
def __init__(self, x_start, y_start):
4648
self.x = x_start
@@ -61,14 +63,16 @@ def reset(self):
6163
self.y_dir = 0
6264
self.history = [[self.x, self.y]]
6365
self.length = 1
64-
66+
67+
#function to show the body of snake
6568
def show(self):
6669
for i in range(self.length):
6770
if not i == 0:
6871
pygame.draw.rect(display, snake_colour, (self.history[i][0], self.history[i][1], self.w, self.h))
6972
else:
7073
pygame.draw.rect(display, snake_head, (self.history[i][0], self.history[i][1], self.w, self.h))
7174

75+
7276
def check_eaten(self):
7377
if abs(self.history[0][0] - food_x) < scale and abs(self.history[0][1] - food_y) < scale:
7478
return True
@@ -91,6 +95,80 @@ def update(self):
9195
i -= 1
9296
self.history[0][0] += self.x_dir*scale
9397
self.history[0][1] += self.y_dir*scale
98+
99+
def autoplay(self):
100+
101+
if abs(food_x-self.history[0][0]) < 10 and abs(food_y-self.history[0][1]) < 10:
102+
# if self.check_eaten():
103+
# food.new_location()
104+
# score += 1
105+
# self.grow()
106+
print("")
107+
elif abs(food_x-self.history[0][0]) < 10:
108+
109+
# if self.y_dir==1 or self.y_dir==-1:
110+
# self.y_dir=0
111+
# self.x_dir=1
112+
if self.x_dir==1 or self.x_dir==-1:
113+
if food_y>self.history[0][1]:
114+
self.y_dir=1
115+
else:
116+
self.y_dir=-1
117+
self.x_dir=0
118+
elif abs(food_y-self.history[0][1]) < 10 :
119+
120+
# if self.x_dir==1 or self.x_dir==-1:
121+
# self.x_dir=0
122+
# self.y_dir=1
123+
if self.y_dir==1 or self.y_dir==-1:
124+
self.y_dir=0
125+
if food_x>self.history[0][0]:
126+
self.x_dir=1
127+
else:
128+
self.x_dir=-1
129+
130+
131+
elif food_x-self.history[0][0] >= 10 and food_y-self.history[0][1] >= 10:
132+
133+
if self.x_dir==-1:
134+
self.y_dir=1
135+
self.x_dir=0
136+
elif self.y_dir==-1:
137+
self.y_dir=0
138+
self.x_dir=1
139+
elif self.history[0][0]-food_x >= 10 and food_y-self.history[0][1] >= 10:
140+
141+
if self.x_dir==1:
142+
self.y_dir=1
143+
self.x_dir=0
144+
elif self.y_dir==1:
145+
self.y_dir=0
146+
self.x_dir=-1
147+
elif self.history[0][0]-food_x >= 10 and self.history[0][1]-food_y >= 10:
148+
149+
if self.x_dir==1:
150+
self.y_dir=-1
151+
self.x_dir=0
152+
elif self.y_dir==1:
153+
self.y_dir=0
154+
self.x_dir=-1
155+
156+
elif food_x-self.history[0][0] >= 10 and self.history[0][1]-food_y >= 10:
157+
158+
if self.x_dir==-1:
159+
self.y_dir=-1
160+
self.x_dir=0
161+
elif self.y_dir==1:
162+
self.y_dir=0
163+
self.x_dir=1
164+
165+
self.update()
166+
167+
168+
169+
170+
171+
94172

95173

96174
# ----------- Food Class --------------
@@ -116,47 +194,61 @@ def gameLoop():
116194

117195
global score
118196

119-
snake = Snake(width/2, height/2)
197+
snake = Snake(width/2, height/2) #starting from mid of grid
120198
food = Food()
121199
food.new_location()
200+
ap=False
122201

123202
while loop:
203+
204+
display.fill(background)
205+
snake.show()
206+
food.show()
207+
show_score()
208+
124209
for event in pygame.event.get():
125210
if event.type == pygame.QUIT:
126211
pygame.quit()
127212
sys.exit()
213+
128214
if event.type == pygame.KEYDOWN:
129215
if event.key == pygame.K_q:
130216
pygame.quit()
131217
sys.exit()
132-
if snake.y_dir == 0:
133-
if event.key == pygame.K_UP:
134-
snake.x_dir = 0
135-
snake.y_dir = -1
136-
if event.key == pygame.K_DOWN:
137-
snake.x_dir = 0
138-
snake.y_dir = 1
139-
140-
if snake.x_dir == 0:
141-
if event.key == pygame.K_LEFT:
142-
snake.x_dir = -1
143-
snake.y_dir = 0
144-
if event.key == pygame.K_RIGHT:
145-
snake.x_dir = 1
146-
snake.y_dir = 0
147-
148-
display.fill(background)
149-
150-
snake.show()
151-
snake.update()
152-
food.show()
153-
show_score()
218+
if event.key==pygame.K_SPACE: #autoplay start
219+
ap=True
220+
if event.key==pygame.K_TAB: #autoplay end
221+
ap=False
222+
else:
223+
if snake.y_dir == 0:
224+
if event.key == pygame.K_UP:
225+
snake.x_dir = 0
226+
snake.y_dir = -1
227+
if event.key == pygame.K_DOWN:
228+
snake.x_dir = 0
229+
snake.y_dir = 1
230+
231+
if snake.x_dir == 0:
232+
if event.key == pygame.K_LEFT:
233+
snake.x_dir = -1
234+
snake.y_dir = 0
235+
if event.key == pygame.K_RIGHT:
236+
snake.x_dir = 1
237+
snake.y_dir = 0
238+
239+
240+
if ap:
241+
snake.autoplay()
242+
else:
243+
snake.update()
244+
154245

155246
if snake.check_eaten():
156247
food.new_location()
157248
score += 1
158249
snake.grow()
159250

251+
160252
if snake.death():
161253
score = 0
162254
font = pygame.font.SysFont("Copperplate Gothic Bold", 50)
@@ -166,6 +258,8 @@ def gameLoop():
166258
time.sleep(3)
167259
snake.reset()
168260

261+
262+
#updating the values if snake goes out of board
169263
if snake.history[0][0] > width:
170264
snake.history[0][0] = 0
171265
if snake.history[0][0] < 0:
@@ -177,6 +271,6 @@ def gameLoop():
177271
snake.history[0][1] = height
178272

179273
pygame.display.update()
180-
clock.tick(10)
274+
clock.tick(10) #at most 10 frames should pass in 1 sec, it is used to control the speed of snake
181275

182276
gameLoop()

0 commit comments

Comments
 (0)