import pygame
import random
import sys
import math

pygame.init()
WIDTH = 720
HEIGHT = 520
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tofu Princess")

# Colors
SKY_COLOR = (124, 186, 242)
PRINCESS = (240, 100, 160)
PRINCESS_OUT = (160, 40, 90)
TOFU_BODY = (255, 252, 230)
TOFU_TOP = (245, 220, 180)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GOLD = (255, 215, 0)
RED = (220, 30, 30)

# Font
font_big = pygame.font.Font(None, 48)
font_mid = pygame.font.Font(None, 36)

# Player
pw = 30
ph = 38
px = 100
py = 360
vy = 0
jump_power = -13
gravity = 0.55
on_ground = False

# Tofu settings
tofu_width = 86
tofu_height = 26
tofus = []
base_speed = 2.2
speed_timer = 0

score = 0
high_score = 0
game_over = False

def reset_game():
    global px, py, vy, on_ground, tofus, score, game_over, speed_timer
    px = 100
    py = 360
    vy = 0
    on_ground = False
    score = 0
    game_over = False
    speed_timer = 0
    tofus.clear()
    tofus.append({"x": 40, "y": 400, "dir": 0, "speed": 0})
    tofus.append({"x": 220, "y": 340, "dir": 1, "speed": base_speed})

reset_game()
clock = pygame.time.Clock()
running = True

while running:
    speed_timer += 1
    screen.fill(SKY_COLOR)
    mx, my = pygame.mouse.get_pos()
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                reset_game()
            if not game_over:
                if event.key == pygame.K_SPACE and on_ground:
                    vy = jump_power
                    on_ground = False
        if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
            if event.button == 1 and on_ground:
                vy = jump_power
                on_ground = False

    if not game_over:
        current_speed = base_speed + speed_timer * 0.0008

        vy += gravity
        py += vy
        player_rect = pygame.Rect(px, py, pw, ph)
        on_ground = False

        # 更新豆腐位置
        for t in tofus:
            if t["dir"] != 0:
                t["x"] += t["speed"] * t["dir"]
                if t["x"] < 0 or t["x"] + tofu_width > WIDTH:
                    t["dir"] *= -1
            t_rect = pygame.Rect(t["x"], t["y"], tofu_width, tofu_height)

            # 落地碰撞【重点修改：人物不跟随豆腐移动】
            if player_rect.colliderect(t_rect):
                if vy > 0 and player_rect.bottom - vy <= t_rect.top + 6:
                    py = t_rect.top - ph
                    vy = 0
                    on_ground = True

        # 掉落判定
        if py > HEIGHT:
            game_over = True
            if score > high_score:
                high_score = score

        # 生成新豆腐
        if on_ground and random.random() < 0.006:
            new_y = random.randint(140, 320)
            new_dir = random.choice([-1, 1])
            tofus.append({
                "x": random.randint(0, WIDTH-tofu_width),
                "y": new_y,
                "dir": new_dir,
                "speed": current_speed
            })
            score += 1

    # 绘制豆腐
    for t in tofus:
        x, y = t["x"], t["y"]
        pygame.draw.rect(screen, TOFU_BODY, (x, y+4, tofu_width, tofu_height-4), border_radius=4)
        pygame.draw.rect(screen, TOFU_TOP, (x, y, tofu_width, 6), border_radius=4)
        pygame.draw.rect(screen, BLACK, (x, y, tofu_width, tofu_height), 2, border_radius=4)

    # 绘制公主
    pygame.draw.rect(screen, PRINCESS, (px, py, pw, ph), border_radius=10)
    pygame.draw.rect(screen, PRINCESS_OUT, (px, py, pw, ph), 2, border_radius=10)
    eye_y = py + 11
    pygame.draw.circle(screen, WHITE, (px+9, eye_y), 4)
    pygame.draw.circle(screen, WHITE, (px+23, eye_y), 4)
    pygame.draw.circle(screen, BLACK, (px+10, eye_y), 2)
    pygame.draw.circle(screen, BLACK, (px+24, eye_y), 2)

    # UI
    score_text = font_mid.render(f"Score: {score}", True, WHITE)
    high_text = font_mid.render(f"Best: {high_score}", True, GOLD)
    screen.blit(score_text, (12, 10))
    screen.blit(high_text, (12, 42))

    if game_over:
        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(130)
        overlay.fill(BLACK)
        screen.blit(overlay, (0,0))
        txt1 = font_big.render("Game Over!", True, RED)
        txt2 = font_mid.render("Press R to restart", True, WHITE)
        screen.blit(txt1, txt1.get_rect(center=(WIDTH//2, HEIGHT//2 - 40)))
        screen.blit(txt2, txt2.get_rect(center=(WIDTH//2, HEIGHT//2 + 20)))

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

pygame.quit()
sys.exit()