diff --git a/BlankProjectTemplate/src/Gameplay.py b/BlankProjectTemplate/src/Gameplay.py index 2e87d3b28c74fbe227d7f26ae5d5c033d68e88ee..a10cc55b0fa2e843cbb48aa8285b3688f2e56b3f 100644 --- a/BlankProjectTemplate/src/Gameplay.py +++ b/BlankProjectTemplate/src/Gameplay.py @@ -7,23 +7,17 @@ from random import randint from Snake import * from Food import * - - - -global a - -a = 0 #defining a list to keep track of snake's head snake_loc = [] #variable to increment snake's length, initially it would be 1 snake_length = 1 -speed = 30 +speed = 70 # 0 gives (- direction) # 1 gives (+ direction) -direction = 0 +direction = 1 # 0 - x-axis , 1 - y-axis axis = 0 @@ -38,7 +32,7 @@ food_location = [food_x, food_y] ##initialize snake and draw snake body somewhere on the screen print(x/size, y/size) -snake = Snake(size, 0, 20, 0) +snake = Snake(size, 0, 20, 1) pygame.draw.rect(win, red , [x,y, size, size]) food = Food(size) @@ -58,13 +52,25 @@ 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): - snake.axis = 0; snake.direct = -1 + # if snake is moving up or down, turn left, otherwise don't turn + if (snake.axis): snake.direct = -1 + snake.axis = 0 if (event.key == pygame.K_RIGHT): - snake.axis = 0; snake.direct = 1 + #if snake is moving up or down turn right, otherwise dont turn + if (snake.axis): snake.direct = 1 + snake.axis = 0 if (event.key == pygame.K_UP): - snake.axis = 1; snake.direct = -1 + #if snake is moving left or right turn up, otherwise dont turn + if (not snake.axis): snake.direct = -1 + snake.axis = 1 if (event.key == pygame.K_DOWN): - snake.axis = 1 ; snake.direct = 1 + #if snake is moving left or right turn down, otherwise dont turn + if (not snake.axis): snake.direct = 1 + snake.axis = 1 + +## print("axis is", "y" if snake.axis else "x") +## print("direction is", snake.direct) + #Snake moving depending on axis and direction if (snake.axis): @@ -72,37 +78,48 @@ while run: else: x += (size)*snake.direct - print("x: ",x,"y: ",y) - print("food x: ",food_location[0],"food y: ",food_location[1]) +## 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 - x = screenSize - size - if y < 0: - #y = 0 - y = screenSize - size - if y > screenSize - size: - #y = 500 - size - y = 0 - if x > screenSize - size: - #x = 500 - size - x = 0 + if (x < 0 or + y < 0 or + y > screenSize - size or + x > screenSize - size): + + pygame.quit() #for now, quit the game when snake hits boundary + +## snake.die() + +## +## if x < 0: +## #x = 0 +## x = screenSize - size +## if y < 0: +## #y = 0 +## y = screenSize - size +## if y > screenSize - size: +## #y = 500 - size +## y = 0 +## if x > screenSize - size: +## #x = 500 - size +## x = 0 if(abs(x - food_location[0]) < 15 and abs(y - food_location[1]) < 15): score += 10 print(score) snake_length += 5 - win.fill(white) #consumption of food block food.redraw_food(x, y, food_location, screenSize) - - + if ([x,y] in snake_loc) and snake_length > 1: + pygame.time.delay(1000) + pygame.quit() #quit for now, but should return to main menu + snake_head = [] snake_head.append(x) snake_head.append(y) diff --git a/BlankProjectTemplate/src/Snake.py b/BlankProjectTemplate/src/Snake.py index ec161f7baa2c83caa8f3ae57c49622171cdcface..bc419c43a6107a08ca0ce8d4c47121ff20c446ed 100644 --- a/BlankProjectTemplate/src/Snake.py +++ b/BlankProjectTemplate/src/Snake.py @@ -27,6 +27,10 @@ class Snake(): for x,y in snake_loc: pygame.draw.rect(win, red , [x,y, self.size, self.size]) - + def die(): + #---------------------INSERT CODE ----------------- + # create some pop up message saying game over + return +