diff --git a/BlankProjectTemplate/src/Food.py b/BlankProjectTemplate/src/Food.py new file mode 100644 index 0000000000000000000000000000000000000000..561c0fffbb85ca0968d3d1d8b019bc26fa5ca94f --- /dev/null +++ b/BlankProjectTemplate/src/Food.py @@ -0,0 +1,34 @@ +## @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 + diff --git a/BlankProjectTemplate/src/Gameplay.py b/BlankProjectTemplate/src/Gameplay.py index 52a24d3a47937bb67a0816989275b9ef4470b773..68f0b2a14058e69f35fd7d7af5a401654e10ef67 100644 --- a/BlankProjectTemplate/src/Gameplay.py +++ b/BlankProjectTemplate/src/Gameplay.py @@ -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) diff --git a/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..94d697adc16cf576c9da39ab7670614a7ab91a37 Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f5fd95f0b33cb1498f06e45af003482d739bb8db Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c240177ee288458b3dcbdf44904b8803ebfeb896 Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc differ