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

Update food

parent 4b3a79cb
No related branches found
No related tags found
No related merge requests found
## @file Food.py
# @author Usman Irfan
# @brief implements an abstract data type for a snake's food
# @date 21/02/2018
from random import randint
from init import *
## @brief An Abstract Data type which represents a one-unit of food
class Food():
## @brief Food constructor
# @details Initializez the size of the food, this needs to be the same as snake's
# block size
# @param blockSize the width and height of the square block representing the food
def __init__(self, blockSize):
self.size = blockSize
## @brief Draw method uses pygame to draw the food object on the window
# @param location A list which consists the x and y location of the food
def draw_food(self, location):
pygame.draw.rect(win, blue , (location[0],location[1], self.size, self.size))
## @brief redraw_food method redraws the food on the screen randomly
# @param x is the location of snake's x-axis head location
# @param y is the location of snake's y-axis head location
# @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):
location[0] = randint(0, screenSize - self.size)
location[1] = randint(0, screenSize - self.size)
return location
......@@ -6,6 +6,7 @@
from random import randint
from Snake import *
from Food import *
from init import *
# One size for all blocks created for snake
......@@ -27,15 +28,15 @@ direction = 0
# 0 - x-axis , 1 - y-axis
axis = 0
score = 0
location = []
food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size)
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:
......@@ -83,13 +84,13 @@ while run:
#all colors are defined in RGB with Pygame
#consumption of food block
if( abs(x - food_x) < 15 and abs(y - food_y) < 15):
score += 1
food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size)
location = [food_x, food_y]
print('score = ', score)
food.redraw_food(x, y, location, screenSize)
if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
score += 1
print(score)
## --------------------------DELETE --------------------------
#extend length or size depending on the direction of the snake
## if (axis):
......@@ -104,8 +105,9 @@ while run:
win.fill(white)
#Draw food item
pygame.draw.rect(win,blue, (location[0], location[1], size, size))
food.draw_food(location)
#food.update_score(x,y,location, score)
#Draw snake
snake.draw(x,y)
......
File added
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