Skip to content
Snippets Groups Projects
Commit e5af3971 authored by Hameed Andy's avatar Hameed Andy
Browse files

First Draft of Class implementation

parent 6eff3f9a
No related branches found
No related tags found
No related merge requests found
## @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
...@@ -3,21 +3,10 @@ ...@@ -3,21 +3,10 @@
# @brief implements gameplay and connects the different components of the game # @brief implements gameplay and connects the different components of the game
# @date 11/09/2018 # @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 # One size for all blocks created for snake
size = 20 size = 20
...@@ -27,8 +16,8 @@ vel = 10 ...@@ -27,8 +16,8 @@ vel = 10
score = 0 score = 0
#initial x and y coordinates of the snake #initial x and y coordinates of the snake
x_init = randint(0,screenSize - size) x = randint(0,screenSize - size)
y_init = randint(0, screenSize - size) y = randint(0, screenSize - size)
speed = 40 speed = 40
...@@ -44,7 +33,8 @@ food_x = randint(0,screenSize - size) ...@@ -44,7 +33,8 @@ food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size) food_y = randint(0,screenSize - size)
location = [food_x, food_y] location = [food_x, food_y]
##initialize snake
snake = Snake(size, 0, 20, 0)
#Loop through the events as long as the game is running #Loop through the events as long as the game is running
run = True run = True
...@@ -62,19 +52,19 @@ while run: ...@@ -62,19 +52,19 @@ while run:
#Each event type has an integer assigned to it. KEYDOWN has the code 2 #Each event type has an integer assigned to it. KEYDOWN has the code 2
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_LEFT): if (event.key == pygame.K_LEFT):
axis = 0;direction=-1 snake.axis = 0; snake.direct = -1
if (event.key == pygame.K_RIGHT): if (event.key == pygame.K_RIGHT):
axis = 0;direction=1 snake.axis = 0; snake.direct = 1
if (event.key == pygame.K_UP): if (event.key == pygame.K_UP):
axis = 1;direction=-1 snake.axis = 1; snake.direct = -1
if (event.key == pygame.K_DOWN): if (event.key == pygame.K_DOWN):
axis = 1;direction=1 snake.axis = 1 ; snake.direct = 1
#Snake moving depending on axis and direction #Snake moving depending on axis and direction
if (axis): if (snake.axis):
y += vel*direction y += vel*snake.direct
else: else:
x += vel*direction x += vel*snake.direct
#Boundary conditions for snake hitting window edge #Boundary conditions for snake hitting window edge
if x < 0: if x < 0:
...@@ -98,16 +88,16 @@ while run: ...@@ -98,16 +88,16 @@ while run:
food_x = randint(0,screenSize - size) food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size) food_y = randint(0,screenSize - size)
location = [food_x, food_y] location = [food_x, food_y]
print('score = ', score)
## --------------------------DELETE -------------------------- ## --------------------------DELETE --------------------------
#extend length or size depending on the direction of the snake #extend length or size depending on the direction of the snake
if (axis): ## if (axis):
size += 20 ## size += 20
else: ## else:
size += 20 ## size += 20
##---------------------------DELETE---------------------------- ##---------------------------DELETE----------------------------
print('score = ', score)
...@@ -117,7 +107,7 @@ while run: ...@@ -117,7 +107,7 @@ while run:
pygame.draw.rect(win,blue, (location[0], location[1], size, size)) pygame.draw.rect(win,blue, (location[0], location[1], size, size))
#Draw snake #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 #we have to update the display to see the drawing of our object. Since it does
......
## @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)
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