import pygame
import random

pygame.init()
WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("The Tortoise and the Hare")
clock = pygame.time.Clock()

# Colors
WHITE = (255, 255, 255)
GREEN = (34, 139, 34)
GRAY = (100, 100, 100)
BLACK = (0, 0, 0)
RED = (220, 30, 30)

# Font
try:
    font = pygame.font.SysFont("Arial", 26)
    font_big = pygame.font.SysFont("Arial", 36)
except:
    font = pygame.font.Font(None, 26)
    font_big = pygame.font.Font(None, 36)

# Track
START_X = 50
FINISH_X = 720

# Racers
tortoise = {"x": START_X, "y": 260, "speed": 1.2}
rabbit = {"x": START_X, "y": 160, "speed": 3.5, "sleeping": False, "sleep_timer": 0}

game_over = False
winner = ""

def draw_tortoise(x, y):
    pygame.draw.ellipse(screen, GREEN, (x, y, 45, 30))
    pygame.draw.circle(screen, GREEN, (x+48, y+15), 8)
    pygame.draw.rect(screen, GREEN, (x+5, y+28, 8, 10))
    pygame.draw.rect(screen, GREEN, (x+32, y+28, 8, 10))

def draw_rabbit(x, y, sleep):
    pygame.draw.ellipse(screen, GRAY, (x, y, 42, 28))
    pygame.draw.circle(screen, GRAY, (x+44, y+14), 9)
    pygame.draw.rect(screen, GRAY, (x+40, y-6, 5, 16))
    pygame.draw.rect(screen, GRAY, (x+48, y-6, 5, 16))
    if sleep:
        text = font.render("Zzz", True, BLACK)
        screen.blit(text, (x+55, y-10))

running = True
while running:
    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Track line
    pygame.draw.line(screen, BLACK, (START_X, 100), (START_X, 320), 3)
    pygame.draw.line(screen, RED, (FINISH_X, 100), (FINISH_X, 320), 3)
    screen.blit(font.render("Start", True, BLACK), (START_X-15, 330))
    screen.blit(font.render("Finish", True, RED), (FINISH_X+5, 330))

    if not game_over:
        # Tortoise keeps moving
        tortoise["x"] += tortoise["speed"]

        # Rabbit logic
        if not rabbit["sleeping"]:
            rabbit["x"] += rabbit["speed"]
            if random.random() < 0.008:
                rabbit["sleeping"] = True
                rabbit["sleep_timer"] = random.randint(120, 300)
        else:
            rabbit["sleep_timer"] -= 1
            if rabbit["sleep_timer"] <= 0:
                rabbit["sleeping"] = False

        # Check winner
        if tortoise["x"] >= FINISH_X:
            game_over = True
            winner = "Tortoise"
        if rabbit["x"] >= FINISH_X:
            game_over = True
            winner = "Hare"

    draw_tortoise(tortoise["x"], tortoise["y"])
    draw_rabbit(rabbit["x"], rabbit["y"], rabbit["sleeping"])

    screen.blit(font.render("Tortoise", True, GREEN), (20, 260))
    screen.blit(font.render("Hare", True, GRAY), (20, 160))

    if game_over:
        win_text = font_big.render(f"Race Over! Winner: {winner}", True, RED)
        screen.blit(win_text, ((WIDTH - win_text.get_width())//2, 50))
        tip_text = font.render("Close window to restart", True, BLACK)
        screen.blit(tip_text, ((WIDTH - tip_text.get_width())//2, 100))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()