import random

WIDTH = 20
HEIGHT = 10

snake = [(5, 5)]
direction = (1, 0)

food = (random.randint(0, WIDTH - 1),
        random.randint(0, HEIGHT - 1))

while True:
    # Rysowanie planszy
    print("\n" * 30)

    for y in range(HEIGHT):
        row = ""
        for x in range(WIDTH):
            if (x, y) == snake[0]:
                row += "O"
            elif (x, y) in snake:
                row += "o"
            elif (x, y) == food:
                row += "*"
            else:
                row += "."
        print(row)

    print("Sterowanie: w/a/s/d")

    move = input("Ruch: ").lower()

    if move == "w":
        direction = (0, -1)
    elif move == "s":
        direction = (0, 1)
    elif move == "a":
        direction = (-1, 0)
    elif move == "d":
        direction = (1, 0)

    head_x, head_y = snake[0]
    dx, dy = direction

    new_head = (head_x + dx, head_y + dy)

    # Kolizja ze ścianą
    if not (0 <= new_head[0] < WIDTH and
            0 <= new_head[1] < HEIGHT):
        print("Game Over!")
        break

    # Kolizja z samym sobą
    if new_head in snake:
        print("Game Over!")
        break

    snake.insert(0, new_head)

    # Jedzenie
    if new_head == food:
        while food in snake:
            food = (
                random.randint(0, WIDTH - 1),
                random.randint(0, HEIGHT - 1)
            )
    else:
        snake.pop()

print("Wynik:", len(snake) - 1)