Skip to content

Commit

Permalink
Autoplay Added
Browse files Browse the repository at this point in the history
  • Loading branch information
princeprag committed Apr 29, 2021
1 parent 4eef6cc commit 4c0b5aa
Showing 1 changed file with 119 additions and 25 deletions.
144 changes: 119 additions & 25 deletions Snake_2d/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@


# ----------- 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
Expand All @@ -61,14 +63,16 @@ def reset(self):
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
Expand All @@ -91,6 +95,80 @@ def update(self):
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 --------------
Expand All @@ -116,47 +194,61 @@ def gameLoop():

global score

snake = Snake(width/2, height/2)
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 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

display.fill(background)

snake.show()
snake.update()
food.show()
show_score()
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)
Expand All @@ -166,6 +258,8 @@ def gameLoop():
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:
Expand All @@ -177,6 +271,6 @@ def gameLoop():
snake.history[0][1] = height

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

gameLoop()

0 comments on commit 4c0b5aa

Please sign in to comment.