"""
是男人就上一百层 - Python 小游戏
使用 pygame 实现，带图形界面
"""

import pygame
import random
import sys
import math

# ==================== 初始化 ====================
pygame.init()

# ==================== 常量配置 ====================
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 720
FPS = 60

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
SKY_BLUE = (135, 206, 235)
DARK_BLUE = (25, 25, 112)
GOLD = (255, 215, 0)
RED = (220, 50, 50)
GREEN = (50, 180, 80)
ORANGE = (255, 140, 0)
PURPLE = (128, 0, 128)
GRAY = (180, 180, 180)
DARK_GRAY = (60, 60, 60)
BROWN = (139, 69, 19)
LIGHT_BROWN = (205, 133, 63)

# 游戏状态
MENU = 0
PLAYING = 1
GAME_OVER = 2
WIN = 3


# ==================== 玩家类 ====================
class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 36
        self.height = 40
        self.vel_x = 0
        self.vel_y = 0
        self.on_ground = False
        self.jump_power = -15
        self.move_speed = 5
        self.gravity = 0.6
        self.max_fall_speed = 12
        self.direction = 1  # 1=右, -1=左
        self.frame = 0
        self.invincible = 0  # 无敌时间（被钉刺伤害后短暂无敌）
        self.lives = 3
        self.score = 0
        self.alive = True

    def update(self, platforms, spikes, springs, coins, moving_platforms):
        if not self.alive:
            return

        keys = pygame.key.get_pressed()
        self.vel_x = 0

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            self.vel_x = -self.move_speed
            self.direction = -1
        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            self.vel_x = self.move_speed
            self.direction = 1

        # 跳跃
        if (keys[pygame.K_SPACE] or keys[pygame.K_UP] or keys[pygame.K_w]) and self.on_ground:
            self.vel_y = self.jump_power
            self.on_ground = False

        # 重力
        self.vel_y += self.gravity
        if self.vel_y > self.max_fall_speed:
            self.vel_y = self.max_fall_speed

        # 水平移动
        self.x += self.vel_x

        # 左右穿墙
        if self.x + self.width < 0:
            self.x = SCREEN_WIDTH
        elif self.x > SCREEN_WIDTH:
            self.x = -self.width

        # 垂直移动 & 碰撞检测
        self.y += self.vel_y
        self.on_ground = False

        # 与平台碰撞
        player_rect = self.get_rect()
        for plat in platforms:
            plat_rect = pygame.Rect(plat.x, plat.y, plat.width, plat.height)
            if player_rect.colliderect(plat_rect):
                if self.vel_y > 0 and self.y + self.height - self.vel_y <= plat.y + 5:
                    self.y = plat.y - self.height
                    self.vel_y = 0
                    self.on_ground = True

        # 与移动平台碰撞
        for mp in moving_platforms:
            mp_rect = pygame.Rect(mp.x, mp.y, mp.width, mp.height)
            if player_rect.colliderect(mp_rect):
                if self.vel_y > 0 and self.y + self.height - self.vel_y <= mp.y + 5:
                    self.y = mp.y - self.height
                    self.vel_y = 0
                    self.on_ground = True
                    self.x += mp.vel_x  # 随平台移动

        # 与弹簧碰撞
        for spring in springs:
            spring_rect = pygame.Rect(spring.x, spring.y, spring.width, spring.height)
            if player_rect.colliderect(spring_rect):
                if self.vel_y > 0 and self.y + self.height - self.vel_y <= spring.y + 5:
                    self.vel_y = self.jump_power * 1.6
                    self.on_ground = False
                    spring.bounce = 10

        # 与钉刺碰撞
        if self.invincible <= 0:
            for spike in spikes:
                spike_rect = pygame.Rect(spike.x, spike.y, spike.width, spike.height)
                if player_rect.colliderect(spike_rect):
                    self.lives -= 1
                    self.invincible = 90  # 1.5秒无敌
                    self.vel_y = -8  # 被刺到弹起
                    if self.lives <= 0:
                        self.alive = False
                    break
        else:
            self.invincible -= 1

        # 与金币碰撞
        for coin in coins:
            if not coin.collected:
                coin_rect = pygame.Rect(coin.x, coin.y, coin.width, coin.height)
                if player_rect.colliderect(coin_rect):
                    coin.collected = True
                    self.score += 50

        # 动画帧
        if self.vel_x != 0:
            self.frame = (self.frame + 0.2) % 4
        else:
            self.frame = 0

    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

    def draw(self, screen, camera_y):
        draw_y = self.y - camera_y
        if draw_y < -self.height or draw_y > SCREEN_HEIGHT:
            return

        # 无敌闪烁效果
        if self.invincible > 0 and self.invincible % 6 < 3:
            return

        px = int(self.x)
        py = int(draw_y)

        # ---- 绘制角色（火柴人风格） ----
        # 身体
        body_color = (60, 60, 60)
        # 头
        head_r = 10
        pygame.draw.circle(screen, (255, 220, 180), (px + self.width // 2, py + head_r), head_r)
        pygame.draw.circle(screen, body_color, (px + self.width // 2, py + head_r), head_r, 2)

        # 眼睛
        eye_offset = 3 * self.direction
        pygame.draw.circle(screen, BLACK, (px + self.width // 2 + eye_offset - 2, py + head_r - 1), 2)
        pygame.draw.circle(screen, BLACK, (px + self.width // 2 + eye_offset + 2, py + head_r - 1), 2)

        # 身体
        body_top = py + head_r + 2
        body_bot = py + self.height - 8
        pygame.draw.line(screen, body_color, (px + self.width // 2, body_top), (px + self.width // 2, body_bot), 4)

        # 手臂
        arm_y = body_top + 8
        swing = int(math.sin(self.frame * math.pi / 2) * 8) if self.vel_x != 0 else 0
        pygame.draw.line(screen, body_color,
                         (px + self.width // 2, arm_y),
                         (px + self.width // 2 + 12 * self.direction, arm_y + swing), 3)
        pygame.draw.line(screen, body_color,
                         (px + self.width // 2, arm_y),
                         (px + self.width // 2 - 12 * self.direction, arm_y - swing + 5), 3)

        # 腿
        leg_y = body_bot
        leg_swing = int(math.sin(self.frame * math.pi / 2) * 10) if self.vel_x != 0 else 5
        pygame.draw.line(screen, body_color,
                         (px + self.width // 2, leg_y),
                         (px + self.width // 2 - 6 + leg_swing, leg_y + 8), 3)
        pygame.draw.line(screen, body_color,
                         (px + self.width // 2, leg_y),
                         (px + self.width // 2 + 6 - leg_swing, leg_y + 8), 3)

        # 鞋子
        pygame.draw.rect(screen, RED, (px + self.width // 2 - 8 + leg_swing, leg_y + 6, 8, 4))
        pygame.draw.rect(screen, RED, (px + self.width // 2 - 2 - leg_swing, leg_y + 6, 8, 4))


# ==================== 平台类 ====================
class Platform:
    def __init__(self, x, y, width, plat_type="normal"):
        self.x = x
        self.y = y
        self.width = width
        self.height = 16
        self.type = plat_type
        self.broken = False
        self.break_timer = 0

    def draw(self, screen, camera_y):
        draw_y = self.y - camera_y
        if draw_y < -self.height or draw_y > SCREEN_HEIGHT:
            return

        if self.type == "normal":
            # 普通平台 - 棕色木板
            pygame.draw.rect(screen, LIGHT_BROWN, (self.x, draw_y, self.width, self.height), border_radius=4)
            pygame.draw.rect(screen, BROWN, (self.x, draw_y, self.width, self.height), 2, border_radius=4)
            # 木板纹理线
            for i in range(1, 3):
                line_y = draw_y + i * self.height // 3
                pygame.draw.line(screen, BROWN, (self.x + 4, line_y), (self.x + self.width - 4, line_y), 1)

        elif self.type == "breakable":
            # 脆弱平台 - 灰色，有裂缝
            if not self.broken:
                pygame.draw.rect(screen, GRAY, (self.x, draw_y, self.width, self.height), border_radius=4)
                pygame.draw.rect(screen, DARK_GRAY, (self.x, draw_y, self.width, self.height), 2, border_radius=4)
                # 裂缝 X
                pygame.draw.line(screen, DARK_GRAY, (self.x + self.width * 0.3, draw_y + 2),
                                (self.x + self.width * 0.7, draw_y + self.height - 2), 2)
                pygame.draw.line(screen, DARK_GRAY, (self.x + self.width * 0.7, draw_y + 2),
                                (self.x + self.width * 0.3, draw_y + self.height - 2), 2)
            else:
                # 破碎动画
                alpha = max(0, 255 - self.break_timer * 25)
                surf = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
                pygame.draw.rect(surf, (200, 200, 200, alpha), (0, 0, self.width, self.height), border_radius=4)
                screen.blit(surf, (self.x, draw_y))

        elif self.type == "moving":
            # 移动平台 - 蓝色
            pygame.draw.rect(screen, (80, 180, 220), (self.x, draw_y, self.width, self.height), border_radius=4)
            pygame.draw.rect(screen, (40, 120, 180), (self.x, draw_y, self.width, self.height), 2, border_radius=4)
            # 箭头指示
            cx = self.x + self.width // 2
            pygame.draw.polygon(screen, WHITE, [(cx - 6, draw_y + 4), (cx + 6, draw_y + 4), (cx, draw_y + 10)])


# ==================== 钉刺类 ====================
class Spike:
    def __init__(self, x, y, width=30):
        self.x = x
        self.y = y
        self.width = width
        self.height = 18

    def draw(self, screen, camera_y):
        draw_y = self.y - camera_y
        if draw_y < -self.height or draw_y > SCREEN_HEIGHT:
            return

        # 绘制三角形钉刺
        spike_count = self.width // 14
        for i in range(spike_count):
            sx = self.x + i * 14
            points = [(sx + 7, draw_y), (sx, draw_y + self.height), (sx + 14, draw_y + self.height)]
            pygame.draw.polygon(screen, SILVER if hasattr(self, 'collected') else (200, 50, 50), points)
            pygame.draw.polygon(screen, (150, 30, 30), points, 1)


SILVER = (192, 192, 192)


# ==================== 弹簧类 ====================
class Spring:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 36
        self.height = 18
        self.bounce = 0

    def draw(self, screen, camera_y):
        draw_y = self.y - camera_y
        if draw_y < -self.height or draw_y > SCREEN_HEIGHT:
            return

        h = max(6, self.height - self.bounce)
        # 弹簧主体
        pygame.draw.rect(screen, (255, 200, 50), (self.x, draw_y + self.height - h, self.width, h), border_radius=3)
        pygame.draw.rect(screen, ORANGE, (self.x, draw_y + self.height - h, self.width, h), 2, border_radius=3)
        # 弹簧线圈
        coil_count = self.width // 8
        for i in range(coil_count):
            cx = self.x + 4 + i * 8
            pygame.draw.arc(screen, ORANGE, (cx - 3, draw_y + self.height - h, 6, 6), 0, math.pi, 1)

        if self.bounce > 0:
            self.bounce -= 1


# ==================== 金币类 ====================
class Coin:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 20
        self.height = 20
        self.collected = False
        self.anim_frame = random.uniform(0, math.pi * 2)

    def draw(self, screen, camera_y):
        if self.collected:
            return
        draw_y = self.y - camera_y
        if draw_y < -self.height or draw_y > SCREEN_HEIGHT:
            return

        self.anim_frame += 0.1
        # 金币旋转效果
        stretch = abs(math.sin(self.anim_frame)) * 0.4 + 0.6
        w = int(self.width * stretch)
        h = self.height
        cx = self.x + self.width // 2
        cy = draw_y + self.height // 2

        pygame.draw.ellipse(screen, GOLD, (cx - w // 2, cy - h // 2, w, h))
        pygame.draw.ellipse(screen, (200, 170, 0), (cx - w // 2, cy - h // 2, w, h), 2)
        # $ 符号
        if w > 8:
            font_tiny = pygame.font.Font(None, 14)
            text = font_tiny.render("$", True, (200, 170, 0))
            screen.blit(text, (cx - text.get_width() // 2, cy - text.get_height() // 2))


# ==================== 移动平台类 ====================
class MovingPlatform:
    def __init__(self, x, y, width, move_range=80, speed=1.5):
        self.x = x
        self.y = y
        self.width = width
        self.height = 16
        self.start_x = x
        self.move_range = move_range
        self.speed = speed
        self.vel_x = speed
        self.direction = 1

    def update(self):
        self.x += self.vel_x
        if self.x > self.start_x + self.move_range:
            self.vel_x = -self.speed
            self.direction = -1
        elif self.x < self.start_x - self.move_range:
            self.vel_x = self.speed
            self.direction = 1

    def draw(self, screen, camera_y):
        draw_y = self.y - camera_y
        if draw_y < -self.height or draw_y > SCREEN_HEIGHT:
            return

        pygame.draw.rect(screen, (80, 180, 220), (self.x, draw_y, self.width, self.height), border_radius=4)
        pygame.draw.rect(screen, (40, 120, 180), (self.x, draw_y, self.width, self.height), 2, border_radius=4)
        # 方向箭头
        cx = self.x + self.width // 2
        if self.direction > 0:
            pygame.draw.polygon(screen, WHITE, [(cx - 6, draw_y + 4), (cx + 6, draw_y + 4), (cx, draw_y + 10)])
        else:
            pygame.draw.polygon(screen, WHITE, [(cx - 6, draw_y + 10), (cx + 6, draw_y + 10), (cx, draw_y + 4)])


# ==================== 粒子效果 ====================
class Particle:
    def __init__(self, x, y, color, vel_x, vel_y, life=30):
        self.x = x
        self.y = y
        self.color = color
        self.vel_x = vel_x
        self.vel_y = vel_y
        self.life = life
        self.max_life = life
        self.size = random.randint(2, 5)

    def update(self):
        self.x += self.vel_x
        self.y += self.vel_y
        self.vel_y += 0.15
        self.life -= 1

    def draw(self, screen, camera_y):
        alpha = int(255 * self.life / self.max_life)
        draw_y = self.y - camera_y
        if 0 <= draw_y <= SCREEN_HEIGHT:
            surf = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
            pygame.draw.circle(surf, (*self.color, alpha), (self.size, self.size), self.size)
            screen.blit(surf, (self.x - self.size, draw_y - self.size))


# ==================== 背景云朵 ====================
class Cloud:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = random.randint(-200, -50)
        self.speed = random.uniform(0.3, 1.0)
        self.size = random.uniform(0.8, 1.5)

    def update(self):
        self.y += self.speed
        if self.y > SCREEN_HEIGHT + 100:
            self.y = random.randint(-300, -100)
            self.x = random.randint(0, SCREEN_WIDTH)

    def draw(self, screen, camera_y):
        draw_y = self.y - camera_y * 0.3  # 视差滚动
        if draw_y < -100 or draw_y > SCREEN_HEIGHT + 100:
            return

        alpha = 100
        surf = pygame.Surface((int(80 * self.size), int(40 * self.size)), pygame.SRCALPHA)
        for ox, oy, r in [(-10, 5, 18), (0, 0, 22), (10, 5, 18), (20, 8, 15)]:
            pygame.draw.circle(surf, (255, 255, 255, alpha),
                               (int(ox * self.size + 20), int(oy * self.size + 15)),
                               int(r * self.size))
        screen.blit(surf, (self.x - 20, int(draw_y) - 15))


# ==================== 主游戏类 ====================
class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("是男人就上一百层")
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 32)
        self.font_big = pygame.font.Font(None, 56)
        self.font_small = pygame.font.Font(None, 22)
        self.font_title = pygame.font.Font(None, 72)
        self.state = MENU
        self.camera_y = 0
        self.highest_y = 0  # 玩家达到的最高位置
        self.target_floor = 100
        self.current_floor = 0
        self.screen_shake = 0
        self.particles = []
        self.clouds = [Cloud() for _ in range(6)]
        self.bg_color_top = (25, 25, 60)
        self.bg_color_bot = (80, 120, 180)
        self.reset_game()

    def reset_game(self):
        self.player = Player(SCREEN_WIDTH // 2 - 18, SCREEN_HEIGHT - 100)
        self.platforms = []
        self.spikes = []
        self.springs = []
        self.coins = []
        self.moving_platforms = []
        self.particles = []
        self.camera_y = 0
        self.highest_y = 0
        self.current_floor = 0
        self.screen_shake = 0
        self.generate_level()

    def generate_level(self):
        """生成关卡 - 从下往上排布平台"""
        # 第一层 - 起始平台（宽大安全）
        self.platforms.append(Platform(60, SCREEN_HEIGHT - 60, 360, "normal"))

        # 生成 1-100 层的平台
        last_y = SCREEN_HEIGHT - 60
        for floor in range(1, 101):
            py = last_y - random.randint(70, 110)
            px = random.randint(20, SCREEN_WIDTH - 120)
            pw = random.randint(70, 140)

            # 根据层数决定平台类型
            if floor % 15 == 0:
                # 每15层一个弹簧
                self.springs.append(Spring(px + pw // 2 - 18, py - 18))
                self.platforms.append(Platform(px, py, pw, "normal"))
            elif floor % 10 == 0:
                # 每10层一个移动平台
                mp = MovingPlatform(px, py, pw, move_range=random.randint(50, 100))
                self.moving_platforms.append(mp)
            elif floor % 7 == 0:
                # 每7层一个脆弱平台
                self.platforms.append(Platform(px, py, pw, "breakable"))
            else:
                self.platforms.append(Platform(px, py, pw, "normal"))

            # 随机放置钉刺（越高越多）
            if floor > 3 and random.random() < 0.2 + floor * 0.003:
                spike_x = random.randint(20, SCREEN_WIDTH - 50)
                # 确保不和平台重叠
                too_close = False
                for p in self.platforms:
                    if abs(p.y - py) < 30 and abs(p.x - spike_x) < 100:
                        too_close = True
                        break
                if not too_close:
                    self.spikes.append(Spike(spike_x, py - 18))

            # 随机放置金币
            if random.random() < 0.3:
                coin_x = px + random.randint(10, max(10, pw - 20))
                self.coins.append(Coin(coin_x, py - 30))

            last_y = py

        self.highest_platform_y = last_y

    def add_particles(self, x, y, color, count=8):
        for _ in range(count):
            vx = random.uniform(-3, 3)
            vy = random.uniform(-4, 0)
            self.particles.append(Particle(x, y, color, vx, vy, random.randint(20, 40)))

    def update(self):
        if self.state != PLAYING:
            return

        # 更新云朵
        for cloud in self.clouds:
            cloud.update()

        # 更新移动平台
        for mp in self.moving_platforms:
            mp.update()

        # 更新玩家
        self.player.update(self.platforms, self.spikes, self.springs, self.coins, self.moving_platforms)

        # 更新粒子
        for p in self.particles:
            p.update()
        self.particles = [p for p in self.particles if p.life > 0]

        # 处理脆弱平台
        for plat in self.platforms:
            if plat.type == "breakable" and plat.broken:
                plat.break_timer += 1
                if plat.break_timer > 10:
                    plat.y = 99999  # 移出屏幕

        # 检查玩家是否在脆弱平台上
        if self.player.on_ground:
            player_rect = self.player.get_rect()
            for plat in self.platforms:
                if plat.type == "breakable" and not plat.broken:
                    plat_rect = pygame.Rect(plat.x, plat.y, plat.width, plat.height)
                    if player_rect.colliderect(plat_rect):
                        plat.broken = True
                        self.add_particles(plat.x + plat.width // 2, plat.y, GRAY, 12)
                        break

        # 相机跟随
        target_camera = self.player.y - SCREEN_HEIGHT * 0.6
        self.camera_y += (target_camera - self.camera_y) * 0.08

        # 屏幕震动
        if self.screen_shake > 0:
            self.screen_shake -= 1

        # 计算当前层数
        start_y = SCREEN_HEIGHT - 60
        self.current_floor = max(0, int((start_y - self.player.y) / 90))
        self.current_floor = min(self.current_floor, 100)

        # 更新最高位置
        if self.player.y < self.highest_y:
            self.highest_y = self.player.y

        # 检查失败 - 掉出屏幕底部
        if self.player.y - self.camera_y > SCREEN_HEIGHT + 100:
            self.player.alive = False

        # 检查游戏结束
        if not self.player.alive:
            self.state = GAME_OVER
            self.add_particles(self.player.x + 18, self.player.y + 20, RED, 20)

        # 检查胜利 - 到达顶层
        if self.player.y <= self.highest_platform_y - 50:
            self.state = WIN
            for _ in range(5):
                self.add_particles(random.randint(50, SCREEN_WIDTH - 50),
                                   random.randint(100, 300),
                                   random.choice([GOLD, RED, GREEN, ORANGE]), 15)

    def draw_gradient_bg(self):
        """绘制渐变背景"""
        for y in range(SCREEN_HEIGHT):
            ratio = y / SCREEN_HEIGHT
            r = int(self.bg_color_top[0] + (self.bg_color_bot[0] - self.bg_color_top[0]) * ratio)
            g = int(self.bg_color_top[1] + (self.bg_color_bot[1] - self.bg_color_top[1]) * ratio)
            b = int(self.bg_color_top[2] + (self.bg_color_bot[2] - self.bg_color_top[2]) * ratio)
            pygame.draw.line(self.screen, (r, g, b), (0, y), (SCREEN_WIDTH, y))

    def draw(self):
        # 应用屏幕震动
        offset_x = random.randint(-self.screen_shake, self.screen_shake) if self.screen_shake > 0 else 0
        offset_y = random.randint(-self.screen_shake, self.screen_shake) if self.screen_shake > 0 else 0

        self.draw_gradient_bg()

        # 绘制云朵
        for cloud in self.clouds:
            cloud.draw(self.screen, self.camera_y)

        # 绘制所有游戏对象
        for plat in self.platforms:
            plat.draw(self.screen, self.camera_y)

        for mp in self.moving_platforms:
            mp.draw(self.screen, self.camera_y)

        for spike in self.spikes:
            spike.draw(self.screen, self.camera_y)

        for spring in self.springs:
            spring.draw(self.screen, self.camera_y)

        for coin in self.coins:
            coin.draw(self.screen, self.camera_y)

        # 绘制粒子
        for p in self.particles:
            p.draw(self.screen, self.camera_y)

        # 绘制玩家
        self.player.draw(self.screen, self.camera_y)

        # 绘制UI
        self.draw_ui()

        # 绘制楼层标记
        self.draw_floor_markers()

        pygame.display.flip()

    def draw_floor_markers(self):
        """在右侧绘制楼层进度标记"""
        bar_x = SCREEN_WIDTH - 25
        bar_y = 80
        bar_h = SCREEN_HEIGHT - 160

        # 进度条背景
        pygame.draw.rect(self.screen, (255, 255, 255, 80), (bar_x, bar_y, 10, bar_h), border_radius=5)

        # 当前进度
        progress = self.current_floor / 100
        filled_h = int(bar_h * progress)
        if filled_h > 0:
            color = GREEN if progress < 0.5 else ORANGE if progress < 0.8 else RED
            pygame.draw.rect(self.screen, color, (bar_x, bar_y + bar_h - filled_h, 10, filled_h), border_radius=5)

        # 层数文字
        floor_text = self.font_small.render(f"{self.current_floor}/100", True, WHITE)
        self.screen.blit(floor_text, (bar_x - 10, bar_y + bar_h + 5))

        # 里程碑标记
        for milestone in [25, 50, 75, 100]:
            my = bar_y + bar_h - int(bar_h * milestone / 100)
            if 80 <= my <= bar_y + bar_h:
                pygame.draw.line(self.screen, GOLD, (bar_x - 5, my), (bar_x + 15, my), 1)
                mt = self.font_small.render(str(milestone), True, GOLD)
                self.screen.blit(mt, (bar_x - 35, my - 8))

    def draw_ui(self):
        # 左上角 - 生命值
        for i in range(self.player.lives):
            pygame.draw.rect(self.screen, RED, (15 + i * 28, 15, 22, 22), border_radius=4)
            heart = self.font_small.render("♥", True, WHITE)
            self.screen.blit(heart, (18 + i * 28, 14))

        # 右上角 - 分数
        score_text = self.font.render(f"分数: {self.player.score}", True, WHITE)
        self.screen.blit(score_text, (SCREEN_WIDTH - score_text.get_width() - 40, 15))

        # 中间上方 - 当前层数（大字）
        floor_text = self.font_big.render(f"{self.current_floor}", True, WHITE)
        self.screen.blit(floor_text, (SCREEN_WIDTH // 2 - floor_text.get_width() // 2, 12))

        floor_label = self.font_small.render("FLOOR", True, (200, 200, 200))
        self.screen.blit(floor_label, (SCREEN_WIDTH // 2 - floor_label.get_width() // 2, 62))

        # 提示文字（前几层显示）
        if self.current_floor < 3:
            tip = self.font_small.render("方向键移动 | 空格跳跃", True, (200, 200, 200))
            self.screen.blit(tip, (SCREEN_WIDTH // 2 - tip.get_width() // 2, SCREEN_HEIGHT - 40))

    def draw_menu(self):
        self.draw_gradient_bg()

        # 标题
        title = self.font_title.render("是男人就上", True, GOLD)
        title2 = self.font_title.render("一百层", True, RED)
        shadow1 = self.font_title.render("是男人就上", True, (100, 80, 0))
        shadow2 = self.font_title.render("一百层", True, (100, 0, 0))

        self.screen.blit(shadow1, (SCREEN_WIDTH // 2 - title.get_width() // 2 + 3, 153))
        self.screen.blit(title, (SCREEN_WIDTH // 2 - title.get_width() // 2, 150))
        self.screen.blit(shadow2, (SCREEN_WIDTH // 2 - title2.get_width() // 2 + 3, 223))
        self.screen.blit(title2, (SCREEN_WIDTH // 2 - title2.get_width() // 2, 220))

        # 小角色动画
        t = pygame.time.get_ticks() / 1000
        bob_y = int(math.sin(t * 3) * 10)
        # 简单的小人
        cx = SCREEN_WIDTH // 2
        pygame.draw.circle(self.screen, (255, 220, 180), (cx, 340 + bob_y), 12)
        pygame.draw.line(self.screen, BLACK, (cx - 8, 345 + bob_y), (cx + 8, 345 + bob_y), 2)  # 眼睛
        pygame.draw.line(self.screen, (60, 60, 60), (cx, 352 + bob_y), (cx, 385 + bob_y), 4)  # 身体
        pygame.draw.line(self.screen, (60, 60, 60), (cx, 385 + bob_y), (cx - 10, 400 + bob_y), 3)  # 左腿
        pygame.draw.line(self.screen, (60, 60, 60), (cx, 385 + bob_y), (cx + 10, 400 + bob_y), 3)  # 右腿
        pygame.draw.line(self.screen, (60, 60, 60), (cx, 365 + bob_y), (cx - 12, 355 + bob_y), 3)  # 左手
        pygame.draw.line(self.screen, (60, 60, 60), (cx, 365 + bob_y), (cx + 12, 355 + bob_y), 3)  # 右手
        # 红鞋
        pygame.draw.rect(self.screen, RED, (cx - 13, 398 + bob_y, 8, 4))
        pygame.draw.rect(self.screen, RED, (cx + 5, 398 + bob_y, 8, 4))

        # 开始按钮
        btn_rect = pygame.Rect(SCREEN_WIDTH // 2 - 100, 440, 200, 50)
        btn_color = (220, 50, 50) if not hasattr(self, '_btn_hover') else (255, 80, 80)
        pygame.draw.rect(self.screen, btn_color, btn_rect, border_radius=12)
        pygame.draw.rect(self.screen, WHITE, btn_rect, 3, border_radius=12)
        start_text = self.font.render("开始挑战", True, WHITE)
        self.screen.blit(start_text, (SCREEN_WIDTH // 2 - start_text.get_width() // 2, 448))

        # 操作说明
        controls = [
            "← → 或 A D : 移动",
            "空格 或 ↑ : 跳跃",
            "躲避钉刺 | 踩弹簧弹更高",
            "踩碎脆弱平台 | 收集金币得分"
        ]
        for i, txt in enumerate(controls):
            c = self.font_small.render(txt, True, (220, 220, 220))
            self.screen.blit(c, (SCREEN_WIDTH // 2 - c.get_width() // 2, 520 + i * 28))

        # 版本信息
        ver = self.font_small.render("v1.0 | Python + Pygame", True, (150, 150, 150))
        self.screen.blit(ver, (SCREEN_WIDTH // 2 - ver.get_width() // 2, SCREEN_HEIGHT - 35))

        pygame.display.flip()
        return btn_rect

    def draw_game_over(self):
        # 半透明覆盖
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 0, 0, 180))
        self.screen.blit(overlay, (0, 0))

        game_over_text = self.font_big.render("GAME OVER", True, RED)
        self.screen.blit(game_over_text, (SCREEN_WIDTH // 2 - game_over_text.get_width() // 2, 220))

        score_text = self.font.render(f"最终分数: {self.player.score}", True, WHITE)
        self.screen.blit(score_text, (SCREEN_WIDTH // 2 - score_text.get_width() // 2, 290))

        floor_text = self.font.render(f"到达层数: {self.current_floor}/100", True, ORANGE)
        self.screen.blit(floor_text, (SCREEN_WIDTH // 2 - floor_text.get_width() // 2, 330))

        # 重新开始按钮
        btn_rect = pygame.Rect(SCREEN_WIDTH // 2 - 100, 400, 200, 45)
        pygame.draw.rect(self.screen, (220, 50, 50), btn_rect, border_radius=12)
        pygame.draw.rect(self.screen, WHITE, btn_rect, 2, border_radius=12)
        restart_text = self.font.render("再来一次", True, WHITE)
        self.screen.blit(restart_text, (SCREEN_WIDTH // 2 - restart_text.get_width() // 2, 407))

        # 返回菜单
        menu_rect = pygame.Rect(SCREEN_WIDTH // 2 - 100, 460, 200, 40)
        pygame.draw.rect(self.screen, (80, 80, 80), menu_rect, border_radius=10)
        pygame.draw.rect(self.screen, WHITE, menu_rect, 2, border_radius=10)
        menu_text = self.font_small.render("返回主菜单", True, WHITE)
        self.screen.blit(menu_text, (SCREEN_WIDTH // 2 - menu_text.get_width() // 2, 468))

        pygame.display.flip()
        return btn_rect, menu_rect

    def draw_win(self):
        # 半透明覆盖
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 0, 0, 160))
        self.screen.blit(overlay, (0, 0))

        # 闪烁的 YOU WIN
        t = pygame.time.get_ticks() / 500
        win_color = (int(255 * (math.sin(t) * 0.3 + 0.7)),
                     int(215 * (math.sin(t + 1) * 0.3 + 0.7)),
                     int(0 * (math.sin(t + 2) * 0.3 + 0.7) + 100))
        win_text = self.font_title.render("YOU WIN!", True, win_color)
        self.screen.blit(win_text, (SCREEN_WIDTH // 2 - win_text.get_width() // 2, 180))

        # 奖杯
        trophy_cx = SCREEN_WIDTH // 2
        pygame.draw.rect(self.screen, GOLD, (trophy_cx - 20, 270, 40, 50), border_radius=5)
        pygame.draw.rect(self.screen, (200, 170, 0), (trophy_cx - 30, 320, 60, 10), border_radius=3)
        pygame.draw.rect(self.screen, GOLD, (trophy_cx - 8, 330, 16, 20))
        pygame.draw.circle(self.screen, GOLD, (trophy_cx, 260), 15)
        star = self.font_big.render("*", True, (255, 255, 200))
        self.screen.blit(star, (trophy_cx - 10, 242))

        score_text = self.font.render(f"最终分数: {self.player.score}", True, WHITE)
        self.screen.blit(score_text, (SCREEN_WIDTH // 2 - score_text.get_width() // 2, 370))

        # 重新开始按钮
        btn_rect = pygame.Rect(SCREEN_WIDTH // 2 - 100, 430, 200, 45)
        pygame.draw.rect(self.screen, (220, 50, 50), btn_rect, border_radius=12)
        pygame.draw.rect(self.screen, WHITE, btn_rect, 2, border_radius=12)
        restart_text = self.font.render("再玩一次", True, WHITE)
        self.screen.blit(restart_text, (SCREEN_WIDTH // 2 - restart_text.get_width() // 2, 437))

        # 返回菜单
        menu_rect = pygame.Rect(SCREEN_WIDTH // 2 - 100, 490, 200, 40)
        pygame.draw.rect(self.screen, (80, 80, 80), menu_rect, border_radius=10)
        pygame.draw.rect(self.screen, WHITE, menu_rect, 2, border_radius=10)
        menu_text = self.font_small.render("返回主菜单", True, WHITE)
        self.screen.blit(menu_text, (SCREEN_WIDTH // 2 - menu_text.get_width() // 2, 498))

        pygame.display.flip()
        return btn_rect, menu_rect

    def run(self):
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                if event.type == pygame.MOUSEBUTTONDOWN:
                    mx, my = event.pos

                    if self.state == MENU:
                        btn_rect = self.draw_menu.__code__  # placeholder
                        # 简单的按钮检测 - 在 MENU 状态下
                        if SCREEN_WIDTH // 2 - 100 <= mx <= SCREEN_WIDTH // 2 + 100 and 440 <= my <= 490:
                            self.state = PLAYING
                            self.reset_game()

                    elif self.state == GAME_OVER:
                        if SCREEN_WIDTH // 2 - 100 <= mx <= SCREEN_WIDTH // 2 + 100:
                            if 400 <= my <= 445:
                                self.state = PLAYING
                                self.reset_game()
                            elif 460 <= my <= 500:
                                self.state = MENU

                    elif self.state == WIN:
                        if SCREEN_WIDTH // 2 - 100 <= mx <= SCREEN_WIDTH // 2 + 100:
                            if 430 <= my <= 475:
                                self.state = PLAYING
                                self.reset_game()
                            elif 490 <= my <= 530:
                                self.state = MENU

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        if self.state == PLAYING:
                            self.state = MENU
                        else:
                            running = False

                    if self.state == MENU and event.key == pygame.K_RETURN:
                        self.state = PLAYING
                        self.reset_game()

                    if (self.state == GAME_OVER or self.state == WIN) and event.key == pygame.K_RETURN:
                        self.state = PLAYING
                        self.reset_game()

            # 更新 & 绘制
            if self.state == MENU:
                self.draw_menu()
            elif self.state == PLAYING:
                self.update()
                self.draw()
            elif self.state == GAME_OVER:
                self.draw()
                self.draw_game_over()
            elif self.state == WIN:
                self.draw()
                self.draw_win()

            self.clock.tick(FPS)

        pygame.quit()
        sys.exit()


# ==================== 启动游戏 ====================
if __name__ == "__main__":
    game = Game()
    game.run()