Skip to content
Snippets Groups Projects
Gameplay.py 5.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hameed Andy's avatar
    Hameed Andy committed
    ## @file Snake_2.o.py
    #  @author Andy Hameed, Usman Irfan
    #  @brief implements gameplay and connects the different components of the game
    #  @date 11/09/2018
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    from random import randint
    from Snake import *
    
    Usman Irfan's avatar
    Usman Irfan committed
    from Food import *
    
    's avatar
    committed
    import ScoreDisplay
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
    def game(speed, colour,food_colour, backgroundColour):
    
    Hameed Andy's avatar
    Hameed Andy committed
        pygame.event.clear()
    
        image = pygame.image.load("Images/barrier.png")
    
    Usman Irfan's avatar
    Usman Irfan committed
        black1 = (48,47,47)
    
    's avatar
    committed
        x = randint(0, grid_length) * size
        y = randint(0, grid_length) * size
    
    Usman Irfan's avatar
    Usman Irfan committed
        #defining a list to update snanke's length
        snake_loc = []
        #variable to increment snake's length, initially it would be 1
        snake_length = 1
    
        # 0 gives (- direction)
        # 1 gives (+ direction)
        direction = 1
        # 0 - x-axis , 1 - y-axis
        axis = 0
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
        score = 0
    
    's avatar
    committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
        # parameters for initializing food on the screen
        food_location = []
        food_x = randint(0, grid_length - 1) * size
        food_y = randint(0, grid_length - 1) * size
        food_location = [food_x, food_y]
    
    Hameed Andy's avatar
    Hameed Andy committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
        ##initialize snake and draw snake body somewhere on the screen
    
    Usman Irfan's avatar
    Usman Irfan committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
        snake = Snake(size, 0, 20, 1)
        pygame.draw.rect(win, colour , [x,y, size, size])
        food = Food(size)
    
    Usman Irfan's avatar
    Usman Irfan committed
    
    
        still = True
    
    Usman Irfan's avatar
    Usman Irfan committed
    
        #Loop through the events as long as the game is running
        run = True 
        while run:
            
            #delay controls part of speed
            pygame.time.delay(speed) 
    
            for event in pygame.event.get():
    
                if event.type == pygame.QUIT:
                    run = False
    
                #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):
                        # if snake is moving up or down, turn left, otherwise don't turn
    
                        if (snake.axis or still): snake.direct = -1; still = False
    
    Usman Irfan's avatar
    Usman Irfan committed
                        snake.axis = 0
                    if (event.key == pygame.K_RIGHT):
                        #if snake is moving up or down turn right, otherwise dont turn
    
                        if (snake.axis or still): snake.direct = 1; still = False 
    
    Usman Irfan's avatar
    Usman Irfan committed
                        snake.axis = 0
                    if (event.key == pygame.K_UP):
                        #if snake is moving left or right turn up, otherwise dont turn
    
                        if (not snake.axis or still): snake.direct = -1; still = False 
    
    Usman Irfan's avatar
    Usman Irfan committed
                        snake.axis = 1
                    if (event.key == pygame.K_DOWN):
                        #if snake is moving left or right turn down, otherwise dont turn
    
                        if (not snake.axis or still): snake.direct = 1; still = False 
    
    Usman Irfan's avatar
    Usman Irfan committed
                        snake.axis = 1
    
    
    Usman Irfan's avatar
    Usman Irfan committed
            #Snake moving depending on axis and direction
            if (snake.axis):
                y += (size)*snake.direct
            else:
                x += (size)*snake.direct
    
    Usman Irfan's avatar
    Usman Irfan committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
            boundary_condition = (x < 0 or y < 0 or y > screenSize - size or x > screenSize - size)
            maze_x = x == 100
    
    Usman Irfan's avatar
    Usman Irfan committed
            maze_y = abs(y - 210) <= 110
    
            maze2_x = x == 380
            
    
    Usman Irfan's avatar
    Usman Irfan committed
            if  (speed == 100):
                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
    
    Usman Irfan's avatar
    Usman Irfan committed
    
    
    Usman Irfan's avatar
    Usman Irfan committed
            elif (speed == 70):
    
    Usman Irfan's avatar
    Usman Irfan committed
                #Boundary conditions for snake hitting window edge
    
    Usman Irfan's avatar
    Usman Irfan committed
                if (boundary_condition):
    
    Hameed Andy's avatar
    Hameed Andy committed
                    run = False
    
    's avatar
    committed
                    ScoreDisplay.display(score,speed, colour,food_colour, backgroundColour)
    
    Usman Irfan's avatar
    Usman Irfan committed
                    
    
    Usman Irfan's avatar
    Usman Irfan committed
            elif (speed == 71):
                if ((maze_x and maze_y) or boundary_condition or (maze2_x and maze_y)):
    
    Usman Irfan's avatar
    Usman Irfan committed
                    run = False
                    ScoreDisplay.display(score,speed, colour,food_colour, backgroundColour)         
    
    Usman Irfan's avatar
    Usman Irfan committed
    
        #-------------------------------------------------
    
            if(abs(x - food_location[0]) < 15 and abs(y - food_location[1]) < 15):
    
    Usman Irfan's avatar
    Usman Irfan committed
                if (speed == 100):
                    score += 5
                elif (speed == 70):
                    score += 7
                else:
                    score += 10
    
    Usman Irfan's avatar
    Usman Irfan committed
    
                #increment the length by 3 unit every time
                snake_length += 3
    
                
            win.fill(backgroundColour)
    
    Usman Irfan's avatar
    Usman Irfan committed
            if speed == 71:
                win.blit(image,(100,100))
                win.blit(image,(380,100))
    
            sc_color = [0,0,0] if backgroundColour[0] == 255 else [255,255,255] 
    
    Hameed Andy's avatar
    Hameed Andy committed
            font = pygame.font.Font("Roboto-Light.ttf",30)
            text = font.render(" " + str(score),True,sc_color)
    
    Usman Irfan's avatar
    Usman Irfan committed
            win.blit(text,(0,0))
            
            if ([x,y] in snake_loc) and snake_length > 1:        
    
    's avatar
    committed
               ScoreDisplay.display(score,speed, colour,food_colour, backgroundColour)
    
    Usman Irfan's avatar
    Usman Irfan committed
            
            snake_head = []
            snake_head.append(x)
            snake_head.append(y)
    
            snake_loc.append(snake_head)
            
            #function to print 
            #consumption of food block
            food.redraw_food(x, y, food_location, screenSize, snake_loc)
    
            snake_blocks = len(snake_loc)
    
            #Draw food item
    
    Usman Irfan's avatar
    Usman Irfan committed
            food.draw_food(food_colour, food_location)
    
    Usman Irfan's avatar
    Usman Irfan committed
    
            
            if snake_blocks > snake_length:
                #keep updating the new block
                del snake_loc[0]
            '''
            Logic for updating the length is taken from:
            CodeWithHarry, CodeWithHarry. “Snakes Game: Length Increment Logic - Python Game Development Using Pygame In Hindi #17.”
            YouTube, YouTube, 2 Oct. 2018,
            www.youtube.com/watch?v=mkGJb0W03DM&index=17&list=PLu0W_9lII9ailUQcxEPZrWgDoL36BtPYb.
            '''
            
            #Draw snake
    
    Usman Irfan's avatar
    Usman Irfan committed
    
    
    's avatar
    committed
            snake.draw(colour,snake_loc)
    
    Usman Irfan's avatar
    Usman Irfan committed
            #update display
            pygame.display.update()
    
        pygame.quit()
    
    's avatar
    committed
    
    
    's avatar
    committed
    #game(70, [255,0,0], [255,255,0],[0,0,0])