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

clean up code

parent b0ea8211
No related branches found
No related tags found
No related merge requests found
## Author: Usman Irfan, Andy Hameed ## @file Snake_2.o.py
# This module will be used to control snake body movements and gameplay # @author Andy Hameed, Usman Irfan
# @brief implements gameplay and connects the different components of the game
# @date 11/09/2018
import pygame import pygame
from random import randint from random import randint
#initializing PyGame and setting game window dimensions
pygame.init() pygame.init()
#setting width and size of window to screenSize
screenSize = 500 screenSize = 500
win = pygame.display.set_mode((screenSize,screenSize)) win = pygame.display.set_mode((screenSize,screenSize))
pygame.display.set_caption("Snake 2.o")
pygame.display.set_caption("My Game")
#Define color constants #Define color constants
white = (255,255,255) white = (255,255,255)
...@@ -18,27 +18,26 @@ red = (255,0,0) ...@@ -18,27 +18,26 @@ red = (255,0,0)
blue = (0,0,255) blue = (0,0,255)
black = (0,0,0) 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
# One size for all blocks created for snake
size = 20
#velocity and score
vel = 10 vel = 10
score = 0 score = 0
x = randint(0,screenSize - size)
y = randint(0, screenSize - size) #initial x and y coordinates of the snake
x_init = randint(0,screenSize - size)
y_init = randint(0, screenSize - size)
speed = 40 speed = 40
# 0 gives (- direction) # 0 gives (- direction)
# 1 gives (+ direction) # 1 gives (+ direction)
direction = 0 direction = 0
# 0 - x-axis , 1 - y-axis # 0 - x-axis , 1 - y-axis
axis = 0 axis = 0
location = [] location = []
food_x = randint(0,screenSize - size) food_x = randint(0,screenSize - size)
...@@ -96,8 +95,8 @@ while run: ...@@ -96,8 +95,8 @@ while run:
#consumption of food block #consumption of food block
if( abs(x - food_x) < 15 and abs(y - food_y) < 15): if( abs(x - food_x) < 15 and abs(y - food_y) < 15):
score += 1 score += 1
food_x = randint(0,screenSize - size1) food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size1) food_y = randint(0,screenSize - size)
location = [food_x, food_y] location = [food_x, food_y]
## --------------------------DELETE -------------------------- ## --------------------------DELETE --------------------------
...@@ -106,15 +105,16 @@ while run: ...@@ -106,15 +105,16 @@ while run:
size += 20 size += 20
else: else:
size += 20 size += 20
print('score = ', score)
##---------------------------DELETE---------------------------- ##---------------------------DELETE----------------------------
print('score = ', score)
win.fill(white) win.fill(white)
#Draw food item #Draw food item
pygame.draw.rect(win,blue, (location[0], location[1], size1, size1)) pygame.draw.rect(win,blue, (location[0], location[1], size, size))
#Draw snake #Draw snake
pygame.draw.rect(win, red , (x,y,size, size)) pygame.draw.rect(win, red , (x,y,size, size))
......
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