import pgzrun
import random

WIDTH = 600
HEIGHT = 400
TILE_SIZE = 20

# Spelinstellingen
STEP_ACCELERATIE = 0.01  # hoe snel de slang versnelt
STEP_VERTRAGING = 0.4   # hoeveel vertraging wordt teruggezet bij appel

# Slang en spelvariabelen
snake = [(100, 100)]
richting = "right"
step = TILE_SIZE
vertraging = 5
frames = 0
score = 0
kleur = "green"
obstakels = [(200, 200), (220, 200), (240, 200)]
game_over = False

# Appelpositie
apple_x = random.randint(0, (WIDTH - TILE_SIZE) // TILE_SIZE) * TILE_SIZE
apple_y = random.randint(0, (HEIGHT - TILE_SIZE) // TILE_SIZE) * TILE_SIZE

kleurmap = {
    5: "green",
    4: "blue",
    3: "orange",
    2: "purple",
    1: "red"
}

def draw():
    screen.clear()
    if game_over:
        screen.draw.text("Game Over!", center=(WIDTH//2, HEIGHT//2 - 20), fontsize=60, color="red")
        screen.draw.text(f"Score: {score}", center=(WIDTH//2, HEIGHT//2 + 30), fontsize=40, color="white")
        return

    for segment in snake:
        screen.draw.filled_rect(Rect(segment, (TILE_SIZE, TILE_SIZE)), kleur)

    screen.draw.filled_circle((apple_x + TILE_SIZE//2, apple_y + TILE_SIZE//2), TILE_SIZE//2, "yellow")

    for blok in obstakels:
        screen.draw.filled_rect(Rect(blok, (TILE_SIZE, TILE_SIZE)), "gray")

    screen.draw.text(f"Score: {score}", topleft=(10, 10), fontsize=30, color="white")
    screen.draw.text(f"Snelheid: {int(100 / vertraging)-20}", topleft=(10, 45), fontsize=24, color="lightblue")



def on_key_down(key):
    global richting
    if key == keys.LEFT and richting != "right": richting = "left"
    elif key == keys.RIGHT and richting != "left": richting = "right"
    elif key == keys.UP and richting != "down": richting = "up"
    elif key == keys.DOWN and richting != "up": richting = "down"

def draai_90_graden(richting):
    keuzes = {
        "up": ["left", "right"],
        "down": ["left", "right"],
        "left": ["up", "down"],
        "right": ["up", "down"]
    }
    return random.choice(keuzes[richting])

def geldige_richting(huidige_richting, head_x, head_y):
    richtingen = ["up", "down", "left", "right"]
    random.shuffle(richtingen)
    for r in richtingen:
        if r == "left": nx, ny = head_x - step, head_y
        elif r == "right": nx, ny = head_x + step, head_y
        elif r == "up": nx, ny = head_x, head_y - step
        elif r == "down": nx, ny = head_x, head_y + step

        if 0 <= nx < WIDTH and 0 <= ny < HEIGHT and (nx, ny) not in snake and (nx, ny) not in obstakels:
            return r
    return None

def update():
    global frames, score, apple_x, apple_y, vertraging, kleur, game_over, richting

    if game_over:
        return

    frames += 1
    if frames < vertraging:
        return
    frames = 0

    vertraging = max(1, vertraging - STEP_ACCELERATIE)
    kleur = kleurmap.get(int(vertraging), "red")

    head_x, head_y = snake[0]
    if richting == "left": new_head = (head_x - step, head_y)
    elif richting == "right": new_head = (head_x + step, head_y)
    elif richting == "up": new_head = (head_x, head_y - step)
    elif richting == "down": new_head = (head_x, head_y + step)

    nx, ny = new_head
    if not (0 <= nx < WIDTH and 0 <= ny < HEIGHT):
        nieuwe_richting = geldige_richting(richting, head_x, head_y)
        if not nieuwe_richting:
            game_over = True
            return
        richting = nieuwe_richting
        update()
        return

    if new_head in snake[1:] or new_head in obstakels:
        game_over = True
        return

    snake.insert(0, new_head)

    if Rect(new_head, (TILE_SIZE, TILE_SIZE)).colliderect(Rect((apple_x, apple_y), (TILE_SIZE, TILE_SIZE))):
        score += 1
        vertraging += STEP_VERTRAGING
        vertraging = min(5, vertraging)
        apple_x = random.randint(0, (WIDTH - TILE_SIZE) // TILE_SIZE) * TILE_SIZE
        apple_y = random.randint(0, (HEIGHT - TILE_SIZE) // TILE_SIZE) * TILE_SIZE
    else:
        snake.pop()

pgzrun.go()
