import pygame
import random
import sys

# Initialize pygame
pygame.init()

# Set window size and title
screen_size = (600, 600)
window = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Python Snake Game")

# Set colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)

# Set snake and food properties
snake_block = 10
snake_speed = 15

# Set font style
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)

# Set initial direction
direction = "right"

# Initialize snake position and body
snake_position = [100, 50]
body = [[100, 50], [90, 50], [80, 50]]

# Initialize food position
food_position = [random.randrange(1, (screen_size[0]//snake_block)) * snake_block,
                random.randrange(1, (screen_size[1]//snake_block)) * snake_block]
food_spawn = True

# Initialize score
score = 0

# Game over function
def game_over():
    my_font = pygame.font.SysFont('times new roman', 45)
    game_over_surface = my_font.render('Your Score is: ' + str(score), True, red)
    game_over_rect = game_over_surface.get_rect()
    game_over_rect.midtop = (screen_size[0]/2, screen_size[1]/4)
    window.blit(game_over_surface, game_over_rect)
    pygame.display.flip()
    pygame.time.wait(3000)
    pygame.quit()
    sys.exit()

# Display score function
def show_score(choice, color, font, size, x, y):
    score_surface = font.render('Score : ' + str(score), True, color)
    score_rect = score_surface.get_rect()
    if choice == 1:
        score_rect.midtop = (x, y)
    else:
        score_rect.midtop = (x, y)
    window.blit(score_surface, score_rect)

# Game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                direction = "up"
            if event.key == pygame.K_DOWN:
                direction = "down"
            if event.key == pygame.K_LEFT:
                direction = "left"
            if event.key == pygame.K_RIGHT:
                direction = "right"
    # Snake movement
    if direction == "up":
        snake_position[1] -= snake_block
    if direction == "down":
        snake_position[1] += snake_block
    if direction == "left":
        snake_position[0] -= snake_block
    if direction == "right":
        snake_position[0] += snake_block
    
    # Snake body growing mechanism
    body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        score += 1
        food_spawn = False
    else:
        body.pop()
        
    # Spawn food
    if not food_spawn:
        food_position = [random.randrange(1, (screen_size[0]//snake_block)) * snake_block,
                        random.randrange(1, (screen_size[1]//snake_block)) * snake_block]
    food_spawn = True
    
    # Game over conditions
    if snake_position[0] > screen_size[0] or snake_position[0] < 0:
        game_over()
    if snake_position[1] > screen_size[1] or snake_position[1] < 0:
        game_over()
        
    # Touching the snake body
    for block in body[1:]:
        if snake_position[0] == block[0] and snake_position[1] == block[1]:
            game_over()
    
    # Drawing snake
    window.fill(black)
    for pos in body:
        pygame.draw.rect(window, white, pygame.Rect(pos[0], pos[1], snake_block, snake_block))
        
    # Drawing food
    pygame.draw.rect(window, white, pygame.Rect(food_position[0], food_position[1], snake_block, snake_block))
    
    # Display score
    show_score(1, white, score_font, 20, screen_size[0]/10, screen_size[1]/10)
    
    # Refresh game screen
    pygame.display.update()
    
    # Frame per second
    fps = pygame.time.Clock()
    fps.tick(snake_speed)

