diff --git a/BlankProjectTemplate/src/Food.py b/BlankProjectTemplate/src/Food.py index 2e2b71fb949aa9bd14662c978645c8ea08fc965e..083d18e30ca25dde70690f82cebf8a7460acef0d 100644 --- a/BlankProjectTemplate/src/Food.py +++ b/BlankProjectTemplate/src/Food.py @@ -27,8 +27,6 @@ class Food(): # @param location is a list that gives the location of present 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): + 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) - return location - diff --git a/BlankProjectTemplate/src/Gameplay.py b/BlankProjectTemplate/src/Gameplay.py index 5bf14e5e882b34a16d2da690de417c72218cb0b5..e4a5af2ae35429d870e05a45989d599a1619525d 100644 --- a/BlankProjectTemplate/src/Gameplay.py +++ b/BlankProjectTemplate/src/Gameplay.py @@ -13,12 +13,20 @@ size = 20 #velocity and score vel = 10 -score = 0 +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 #initial x and y coordinates of the snake x = randint(0,screenSize - size) y = randint(0, screenSize - size) + speed = 40 # 0 gives (- direction) @@ -28,15 +36,18 @@ direction = 0 axis = 0 score = 0 -location = [] + +# parameters for initializing food on the screen +food_location = [] food_x = randint(0,screenSize - size) food_y = randint(0,screenSize - size) -location = [food_x, food_y] +food_location = [food_x, food_y] ##initialize snake snake = Snake(size, 0, 20, 0) food = Food(size) + #Loop through the events as long as the game is running run = True while run: @@ -66,6 +77,8 @@ while run: else: x += vel*snake.direct + + #Boundary conditions for snake hitting window edge if x < 0: #x = 0 @@ -80,22 +93,37 @@ while run: #x = 500 - size x = 0 - #consumption of food block - food.redraw_food(x, y, location, screenSize) - - if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15): - score += 1 + 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) + + + + snake_head = [] + snake_head.append(x) + snake_head.append(y) + + snake_loc.append(snake_head) + #Draw food item - food.draw_food(location) + food.draw_food(food_location) + + + if (len(snake_loc)) > snake_length: + del snake_loc[0] - #food.update_score(x,y,location, score) + #food.update_score(x,y,food_location, score) #Draw snake - snake.draw(x,y) + snake.draw(snake_loc) #update display pygame.display.update() diff --git a/BlankProjectTemplate/src/Snake.py b/BlankProjectTemplate/src/Snake.py index 85cfd02162143c43aa11da9ba2b014d5a1eb48cf..ec161f7baa2c83caa8f3ae57c49622171cdcface 100644 --- a/BlankProjectTemplate/src/Snake.py +++ b/BlankProjectTemplate/src/Snake.py @@ -23,8 +23,9 @@ class Snake(): ## @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)) + def draw(self,snake_loc): + for x,y in snake_loc: + pygame.draw.rect(win, red , [x,y, self.size, self.size]) diff --git a/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc index 94d697adc16cf576c9da39ab7670614a7ab91a37..58e5c5cbf5aad9d7103d199b4693b97052885858 100644 Binary files a/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8024b5fc9b0da06f33208b6773a090949e27b5bd Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc index f5fd95f0b33cb1498f06e45af003482d739bb8db..8342bed9a4701190192b5bd777870b2deb9b75e6 100644 Binary files a/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e9dbae58b8cf5bae123abef0101d34290a44d4d4 Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8de1b8ce4e1ef6118e11db4678fb52a3b892c41e Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc differ