import pygame
import random
stop_game = False
import time
pygame.init()
pygame.font.init()
score = 0
fpsClock = pygame.time.Clock()
finished = False
surface = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Snake Game Beta")
direction = "right"
easy = 10
medium = 15
hard = 20
impossible = 35
s_impossible = 70
background_Colour = pygame.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
Snake_Colour = pygame.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
Food_Colour = pygame.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
snake_head = [400,300]
snake_body = [snake_head, [390, 300],[380, 300],[370, 300],[360, 300]]

def generate_apple():
    randomx = random.randint(0, 80) * 10
    randomy = random.randint(0, 60) * 10
    apple_position = [randomx, randomy]
    return apple_position

apple = generate_apple()
def game_over():
    global stop_game
    game_over_font = pygame.font.SysFont("Rockwell", 100, True, False)
    game_over_Colour = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    game_Over_surface = game_over_font.render("GAME OVER!", True, game_over_Colour)
    game_over_rect = game_Over_surface.get_rect()
    score_font = pygame.font.SysFont("Rockwell", 60, True, False)
    score_Colour = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    score_surface = score_font.render("SCORE:" + str(score), True, score_Colour)
    score_rect = score_surface.get_rect()
    score_rect.midtop = (400, 80)
    surface.blit(score_surface, score_rect)
    game_over_rect.midtop = (400, 20)
    surface.blit(game_Over_surface, game_over_rect)
    stop_game = True
    pygame.display.flip()

def restart():
    global direction, background_Colour, Snake_Colour, Food_Colour, snake_head, snake_body, apple, score, stop_game
    direction = "right"
    background_Colour = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    Snake_Colour = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    Food_Colour = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    snake_head = [400, 300]
    snake_body = [snake_head, [390, 300], [380, 300], [370, 300], [360, 300]]
    apple = generate_apple()
    score = 0
    stop_game = False

while not finished:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                if direction == "up":
                    direction = "left"
                elif direction == "left":
                    direction = "down"
                elif direction == "down":
                    direction = "right"
                elif direction == "right":
                    direction = "up"
            elif event.key == pygame.K_d:
                if direction == "up":
                    direction = "right"
                elif direction == "left":
                    direction = "up"
                elif direction == "down":
                    direction = "left"
                elif direction == "right":
                    direction = "down"
            elif event.key == pygame.K_r and stop_game == True:
                restart()
        
    apple_rect = pygame.Rect(apple[0], apple[1], 10, 10)
    surface.fill(background_Colour)
    pygame.draw.rect(surface, Food_Colour, apple_rect)

    if snake_body[0][0] == apple[0] and snake_body[0][1] == apple[1]:
        snake_body.append(apple)
        score = score + 1
        apple = generate_apple()

    for segment in snake_body:
        segment_rect = pygame.Rect(segment[0], segment[1], 10, 10)
        pygame.draw.rect(surface, Snake_Colour, segment_rect)
    if direction == "right":
        new_head = (snake_body[0][0] + 10,snake_body[0][1])
        snake_body.insert(0,new_head)
        snake_body.pop()

    if direction == "left":
        new_head = (snake_body[0][0] - 10,snake_body[0][1])
        snake_body.insert(0,new_head)
        snake_body.pop()

    if direction == "up":
        new_head = (snake_body[0][0],snake_body[0][1] - 10)
        snake_body.insert(0,new_head)
        snake_body.pop()
    if direction == "down":
        new_head = (snake_body[0][0],snake_body[0][1] + 10)
        snake_body.insert(0,new_head)
        snake_body.pop()

    #snake_body.insert(0, list(snake_head))
    #snake_body.pop()

    for segment in snake_body[1:]:
        if (segment == snake_body[0]):
            direction = "stop"
            game_over()


    if snake_body[0][0] < 0 or snake_body[0][0] > 800 or snake_body[0][1] < 0 or snake_body[0][1] > 600:
        direction = "stop"
        game_over()

    pygame.display.flip()
    fpsClock.tick(10)
