Skip to content
Snippets Groups Projects
Commit 2bb10026 authored by Usman Irfan's avatar Usman Irfan
Browse files

Updated Length

parent 45f03a2f
No related branches found
No related tags found
No related merge requests found
......@@ -27,8 +27,6 @@ class Food():
# @param location is a list that gives the location of present food
# @param screenSize is the size of the screen
def redraw_food(self, x, y, location,screenSize):
if( abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
location[0] = randint(0, screenSize - self.size)
location[1] = randint(0, screenSize - self.size)
return location
......@@ -13,12 +13,20 @@ size = 20
#velocity and score
vel = 10
score = 0
global a
a = 0
#defining a list to keep track of snake's head
snake_loc = []
#variable to increment snake's length, initially it would be 1
snake_length = 1
#initial x and y coordinates of the snake
x = randint(0,screenSize - size)
y = randint(0, screenSize - size)
speed = 40
# 0 gives (- direction)
......@@ -28,15 +36,18 @@ direction = 0
axis = 0
score = 0
location = []
# parameters for initializing food on the screen
food_location = []
food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size)
location = [food_x, food_y]
food_location = [food_x, food_y]
##initialize snake
snake = Snake(size, 0, 20, 0)
food = Food(size)
#Loop through the events as long as the game is running
run = True
while run:
......@@ -66,6 +77,8 @@ while run:
else:
x += vel*snake.direct
#Boundary conditions for snake hitting window edge
if x < 0:
#x = 0
......@@ -80,22 +93,37 @@ while run:
#x = 500 - size
x = 0
#consumption of food block
food.redraw_food(x, y, location, screenSize)
if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
score += 1
if(abs(x - food_location[0]) < 15 and abs(y - food_location[1]) < 15):
score += 10
print(score)
snake_length += 5
win.fill(white)
#consumption of food block
food.redraw_food(x, y, food_location, screenSize)
snake_head = []
snake_head.append(x)
snake_head.append(y)
snake_loc.append(snake_head)
#Draw food item
food.draw_food(location)
food.draw_food(food_location)
if (len(snake_loc)) > snake_length:
del snake_loc[0]
#food.update_score(x,y,location, score)
#food.update_score(x,y,food_location, score)
#Draw snake
snake.draw(x,y)
snake.draw(snake_loc)
#update display
pygame.display.update()
......
......@@ -23,8 +23,9 @@ class Snake():
## @brief Draw method uses pygame to draw the snake object
# @param x The x-coordinate where the block should be drawn
# @param y The y-coordinate where the block should be drawn
def draw(self,x,y):
pygame.draw.rect(win, red , (x,y, self.size, self.size))
def draw(self,snake_loc):
for x,y in snake_loc:
pygame.draw.rect(win, red , [x,y, self.size, self.size])
......
No preview for this file type
File added
No preview for this file type
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment