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

snake moves along a grid, aligned with food item

parent 2bb10026
No related branches found
No related tags found
No related merge requests found
......@@ -28,5 +28,5 @@ class 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)
location[0] = randint(0, grid_length - 1) * self.size
location[1] = randint(0, grid_length - 1) * self.size
......@@ -3,16 +3,13 @@
# @brief implements gameplay and connects the different components of the game
# @date 11/09/2018
from random import randint
from Snake import *
from Food import *
# One size for all blocks created for snake
size = 20
#velocity and score
vel = 10
global a
a = 0
......@@ -22,12 +19,7 @@ snake_loc = []
#variable to increment snake's length, initially it would be 1
snake_length = 1
#initial x and y coordinates of the snake
x = randint(0,screenSize - size)
y = randint(0, screenSize - size)
speed = 40
speed = 30
# 0 gives (- direction)
# 1 gives (+ direction)
......@@ -39,12 +31,15 @@ score = 0
# parameters for initializing food on the screen
food_location = []
food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size)
food_x = randint(0, grid_length - 1) * size
food_y = randint(0, grid_length - 1) * size
food_location = [food_x, food_y]
##initialize snake
##initialize snake and draw snake body somewhere on the screen
print(x/size, y/size)
snake = Snake(size, 0, 20, 0)
pygame.draw.rect(win, red , [x,y, size, size])
food = Food(size)
......@@ -73,12 +68,13 @@ while run:
#Snake moving depending on axis and direction
if (snake.axis):
y += vel*snake.direct
y += (size)*snake.direct
else:
x += vel*snake.direct
x += (size)*snake.direct
print("x: ",x,"y: ",y)
print("food x: ",food_location[0],"food y: ",food_location[1])
#Boundary conditions for snake hitting window edge
if x < 0:
#x = 0
......
......@@ -4,6 +4,7 @@
# @date 11/09/2018
import pygame
from random import randint
#initializing PyGame and setting game window dimensions
pygame.init()
......@@ -16,3 +17,11 @@ white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
black = (0,0,0)
# One size for all blocks created for snake
size = 20
grid_length = screenSize/size
#make initial position sit within the grid
x = randint(0, grid_length) * size
y = randint(0, grid_length) * 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