diff --git a/BlankProjectTemplate/src/Snake_2.o_Demo.py b/BlankProjectTemplate/src/Snake_2.o_Demo.py index 5e0b834c7088d272de27fa6075e16b04aa1f425f..78cbcd14e3dbc3c00659e1774c4587a466610537 100644 --- a/BlankProjectTemplate/src/Snake_2.o_Demo.py +++ b/BlankProjectTemplate/src/Snake_2.o_Demo.py @@ -1,30 +1,39 @@ -#standard set up +## Author: Usman Irfan, Andy Hameed +# This module will be used to control snake body movements and gameplay + import pygame from random import randint pygame.init() -#setting width and height of window - -screen_x = 500 -screen_y = 500 -win = pygame.display.set_mode((screen_x,screen_y)) +#setting width and size of window to screenSize +screenSize = 500 +win = pygame.display.set_mode((screenSize,screenSize)) pygame.display.set_caption("My Game") +#Define color constants white = (255,255,255) +red = (255,0,0) +blue = (0,0,255) black = (0,0,0) +#width1, size1 correspond to constant width and size for the food block +#width and size change over time as the snake consumes food + +width, width1 = 20,20 # can replace with size - one number +size, size1 = 20,20 +size = 20 # One size for all block created for snake + -width = 20 -height = 20 vel = 10 score = 0 -x = randint(0,screen_x - width) -y = randint(0, screen_y - height) +x = randint(0,screenSize - size) +y = randint(0, screenSize - size) speed = 40 -# 0 - (- direction) , 1 - (+ direction) +# 0 gives (- direction) +# 1 gives (+ direction) direction = 0 # 0 - x-axis , 1 - y-axis axis = 0 @@ -32,14 +41,14 @@ axis = 0 location = [] -food_x = randint(0,screen_x - width) -food_y = randint(0,screen_y - height) +food_x = randint(0,screenSize - size) +food_y = randint(0,screenSize - size) location = [food_x, food_y] -run = True - +#Loop through the events as long as the game is running +run = True while run: pygame.time.delay(speed) #create a delay to prevent any unwanted behaviour @@ -47,6 +56,7 @@ while run: #we loop through the list of events that happen by the user's actions for event in pygame.event.get(): + #Check if the event is a quit command and quit the game if it is if event.type == pygame.QUIT: run = False @@ -67,37 +77,49 @@ while run: else: x += vel*direction - + #Boundary conditions for snake hitting window edge if x < 0: #x = 0 - x = screen_x - height - + x = screenSize - size if y < 0: #y = 0 - y = screen_y - width - - if y > screen_y - width: - #y = 500 - width + y = screenSize - size + if y > screenSize - size: + #y = 500 - size y = 0 - if x > screen_x - height: - #x = 500 - height + if x > screenSize - size: + #x = 500 - size x = 0 - - #all colors are defined in RGB with Pygame + #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,screen_x - width) - food_y = randint(0,screen_y - height) + food_x = randint(0,screenSize - size1) + food_y = randint(0,screenSize - size1) location = [food_x, food_y] + + ## --------------------------DELETE -------------------------- + #extend length or size depending on the direction of the snake + if (axis): + size += 20 + else: + size += 20 + print('score = ', score) + ##---------------------------DELETE---------------------------- win.fill(white) - pygame.draw.rect(win,(0,0,255), (location[0], location[1], width, height)) - pygame.draw.rect(win,(255,0,0), (x,y,width, height)) - + + #Draw food item + pygame.draw.rect(win,blue, (location[0], location[1], size1, size1)) + + #Draw snake + pygame.draw.rect(win, red , (x,y,size, size)) + + #we have to update the display to see the drawing of our object. Since it does #not automatically update pygame.display.update()