From e5af3971b46fd0370c8d92cf134c152a0e503fe8 Mon Sep 17 00:00:00 2001 From: andyh98 <hameea1@mcmaster.ca> Date: Fri, 9 Nov 2018 15:32:17 -0500 Subject: [PATCH] First Draft of Class implementation --- BlankProjectTemplate/src/Snake.py | 41 ++++++++++++++++++ BlankProjectTemplate/src/Snake_2.o_Demo.py | 50 +++++++++------------- BlankProjectTemplate/src/init.py | 18 ++++++++ 3 files changed, 79 insertions(+), 30 deletions(-) create mode 100644 BlankProjectTemplate/src/Snake.py create mode 100644 BlankProjectTemplate/src/init.py diff --git a/BlankProjectTemplate/src/Snake.py b/BlankProjectTemplate/src/Snake.py new file mode 100644 index 0000000..56eead7 --- /dev/null +++ b/BlankProjectTemplate/src/Snake.py @@ -0,0 +1,41 @@ +## @file Snake.py +# @author Andy Hameed +# @brief implements an abstract data type for a snake +# @date 21/02/2018 + + +from init import * + +## @brief An Abstract Data type representing a snake character object +class Snake(): + + ## @brief Snake constructor + # @details Initializes a Snake object with its initial attributes + # @param blockSize the width and height of the square block representing the snake + # @param direct The direction of the snake's movement + # @param speed The initial speed of the snake's movement + def __init__(self, blockSize, direct, speed, axis): + self.speed = speed + self.direct = direct + self.size = blockSize + self.axis = axis + + ## @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)) + + + ## @brief Snake consumes a food block + ## def consume(): + + +## +##class Block(): +## +## def __init__(self, width, height): +## self.w = width +## swlf.h = height + + diff --git a/BlankProjectTemplate/src/Snake_2.o_Demo.py b/BlankProjectTemplate/src/Snake_2.o_Demo.py index 2db388b..52a24d3 100644 --- a/BlankProjectTemplate/src/Snake_2.o_Demo.py +++ b/BlankProjectTemplate/src/Snake_2.o_Demo.py @@ -3,21 +3,10 @@ # @brief implements gameplay and connects the different components of the game # @date 11/09/2018 -import pygame -from random import randint - -#initializing PyGame and setting game window dimensions -pygame.init() -screenSize = 500 -win = pygame.display.set_mode((screenSize,screenSize)) -pygame.display.set_caption("Snake 2.o") - -#Define color constants -white = (255,255,255) -red = (255,0,0) -blue = (0,0,255) -black = (0,0,0) +from random import randint +from Snake import * +from init import * # One size for all blocks created for snake size = 20 @@ -27,8 +16,8 @@ vel = 10 score = 0 #initial x and y coordinates of the snake -x_init = randint(0,screenSize - size) -y_init = randint(0, screenSize - size) +x = randint(0,screenSize - size) +y = randint(0, screenSize - size) speed = 40 @@ -44,7 +33,8 @@ 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) #Loop through the events as long as the game is running run = True @@ -62,19 +52,19 @@ while run: #Each event type has an integer assigned to it. KEYDOWN has the code 2 if event.type == pygame.KEYDOWN: if (event.key == pygame.K_LEFT): - axis = 0;direction=-1 + snake.axis = 0; snake.direct = -1 if (event.key == pygame.K_RIGHT): - axis = 0;direction=1 + snake.axis = 0; snake.direct = 1 if (event.key == pygame.K_UP): - axis = 1;direction=-1 + snake.axis = 1; snake.direct = -1 if (event.key == pygame.K_DOWN): - axis = 1;direction=1 + snake.axis = 1 ; snake.direct = 1 #Snake moving depending on axis and direction - if (axis): - y += vel*direction + if (snake.axis): + y += vel*snake.direct else: - x += vel*direction + x += vel*snake.direct #Boundary conditions for snake hitting window edge if x < 0: @@ -98,16 +88,16 @@ while run: food_x = randint(0,screenSize - size) food_y = randint(0,screenSize - size) location = [food_x, food_y] + print('score = ', score) ## --------------------------DELETE -------------------------- #extend length or size depending on the direction of the snake - if (axis): - size += 20 - else: - size += 20 +## if (axis): +## size += 20 +## else: +## size += 20 ##---------------------------DELETE---------------------------- - print('score = ', score) @@ -117,7 +107,7 @@ while run: pygame.draw.rect(win,blue, (location[0], location[1], size, size)) #Draw snake - pygame.draw.rect(win, red , (x,y,size, size)) + snake.draw(x,y) #we have to update the display to see the drawing of our object. Since it does diff --git a/BlankProjectTemplate/src/init.py b/BlankProjectTemplate/src/init.py new file mode 100644 index 0000000..4caab43 --- /dev/null +++ b/BlankProjectTemplate/src/init.py @@ -0,0 +1,18 @@ +## @file Colors.py +# @author Andy Hameed +# @brief Define color constants +# @date 11/09/2018 + +import pygame + +#initializing PyGame and setting game window dimensions +pygame.init() +screenSize = 500 +win = pygame.display.set_mode((screenSize,screenSize)) +pygame.display.set_caption("Snake 2.o") + +#Define color constants +white = (255,255,255) +red = (255,0,0) +blue = (0,0,255) +black = (0,0,0) -- GitLab