import pygame
import random
import sys
import math

# 初始化pygame
pygame.init()

# 设置窗口尺寸
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("是男人就上100层")

# 颜色定义
BACKGROUND = (5, 10, 30)
FLOOR_COLOR = (100, 80, 60)
FLOOR_HIGHLIGHT = (120, 100, 80)
PLAYER_COLOR = (70, 180, 255)
PLAYER_OUTLINE = (40, 150, 230)
DANGER_COLOR = (255, 80, 80)
DANGER_OUTLINE = (200, 50, 50)
TEXT_COLOR = (240, 240, 240)
TEXT_SHADOW = (100, 100, 100)
BUTTON_COLOR = (60, 150, 220)
BUTTON_HOVER = (80, 170, 240)
GAME_OVER_BG = (20, 20, 40, 200)

# 游戏状态
GAME_STATE_MENU = 0
GAME_STATE_PLAYING = 1
GAME_STATE_GAME_OVER = 2
GAME_STATE_WIN = 3

# 字体
try:
    title_font = pygame.font.SysFont("microsoftyahei", 60, bold=True)
    score_font = pygame.font.SysFont("microsoftyahei", 36, bold=True)
    button_font = pygame.font.SysFont("microsoftyahei", 30)
    info_font = pygame.font.SysFont("microsoftyahei", 24)
    small_font = pygame.font.SysFont("microsoftyahei", 20)
except:
    title_font = pygame.font.Font(None, 60)
    score_font = pygame.font.Font(None, 36)
    button_font = pygame.font.Font(None, 30)
    info_font = pygame.font.Font(None, 24)
    small_font = pygame.font.Font(None, 20)

# 玩家类
class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 40
        self.height = 60
        self.velocity_x = 0
        self.velocity_y = 0
        self.jump_power = 15
        self.gravity = 0.6
        self.move_speed = 5
        self.is_on_ground = False
        self.facing_right = True
        self.jump_count = 0
        self.max_jumps = 2
        self.is_jumping = False
        self.animation_timer = 0
        
    def update(self, floors, dangers):
        # 应用重力
        self.velocity_y += self.gravity
        
        # 水平移动
        self.x += self.velocity_x
        
        # 检查水平边界
        if self.x < 0:
            self.x = 0
        elif self.x + self.width > WIDTH:
            self.x = WIDTH - self.width
        
        # 垂直移动
        self.y += self.velocity_y
        
        # 检查是否在地板上
        self.is_on_ground = False
        
        # 与地板碰撞检测
        for floor in floors:
            if self.check_collision(floor):
                # 如果从上方碰撞地板
                if self.velocity_y > 0 and self.y + self.height <= floor.y + 10:
                    self.y = floor.y - self.height
                    self.velocity_y = 0
                    self.is_on_ground = True
                    self.jump_count = 0
                    self.is_jumping = False
        
        # 与危险物碰撞检测
        for danger in dangers:
            if self.check_collision(danger):
                return False  # 游戏结束
        
        # 检查是否掉出屏幕底部
        if self.y > HEIGHT:
            return False  # 游戏结束
            
        return True  # 玩家仍然存活
    
    def check_collision(self, obj):
        # 简单的AABB碰撞检测
        return (self.x < obj.x + obj.width and
                self.x + self.width > obj.x and
                self.y < obj.y + obj.height and
                self.y + self.height > obj.y)
    
    def jump(self):
        if self.jump_count < self.max_jumps:
            self.velocity_y = -self.jump_power
            self.jump_count += 1
            self.is_jumping = True
            self.animation_timer = 0
    
    def move_left(self):
        self.velocity_x = -self.move_speed
        self.facing_right = False
    
    def move_right(self):
        self.velocity_x = self.move_speed
        self.facing_right = True
    
    def stop_horizontal(self):
        self.velocity_x = 0
    
    def draw(self, surface):
        # 更新动画计时器
        self.animation_timer += 1
        
        # 绘制玩家角色
        player_rect = pygame.Rect(int(self.x), int(self.y), self.width, self.height)
        
        # 绘制身体
        pygame.draw.rect(surface, PLAYER_COLOR, player_rect, border_radius=10)
        pygame.draw.rect(surface, PLAYER_OUTLINE, player_rect, 3, border_radius=10)
        
        # 绘制头部
        head_radius = 15
        head_y = self.y - head_radius + 5
        head_x = self.x + self.width // 2
        
        # 根据跳跃状态调整头部位置
        if self.is_jumping:
            head_y += math.sin(self.animation_timer * 0.3) * 3
        
        pygame.draw.circle(surface, PLAYER_COLOR, (int(head_x), int(head_y)), head_radius)
        pygame.draw.circle(surface, PLAYER_OUTLINE, (int(head_x), int(head_y)), head_radius, 2)
        
        # 绘制眼睛
        eye_offset = 5 if self.facing_right else -5
        eye_size = 4
        pygame.draw.circle(surface, (255, 255, 255), (int(head_x + eye_offset), int(head_y - 2)), eye_size)
        pygame.draw.circle(surface, (0, 0, 0), (int(head_x + eye_offset), int(head_y - 2)), eye_size // 2)
        
        # 绘制嘴巴
        mouth_y = head_y + 5
        mouth_start = (head_x - 6, mouth_y)
        mouth_end = (head_x + 6, mouth_y)
        pygame.draw.line(surface, (0, 0, 0), mouth_start, mouth_end, 2)
        
        # 绘制跳跃特效
        if self.is_jumping and self.animation_timer < 10:
            for i in range(3):
                effect_y = self.y + self.height + i * 5
                effect_width = self.width - i * 8
                effect_rect = pygame.Rect(
                    int(self.x + (self.width - effect_width) // 2),
                    int(effect_y),
                    effect_width,
                    3
                )
                alpha = 150 - i * 40
                effect_surface = pygame.Surface((effect_width, 3), pygame.SRCALPHA)
                pygame.draw.rect(effect_surface, (*PLAYER_COLOR, alpha), effect_surface.get_rect())
                surface.blit(effect_surface, effect_rect.topleft)

# 地板类
class Floor:
    def __init__(self, x, y, width, is_moving=False, move_range=0, move_speed=0):
        self.x = x
        self.y = y
        self.width = width
        self.height = 20
        self.is_moving = is_moving
        self.move_range = move_range
        self.move_speed = move_speed
        self.move_direction = 1
        self.original_x = x
        
    def update(self):
        if self.is_moving:
            self.x += self.move_speed * self.move_direction
            if self.x <= self.original_x - self.move_range or self.x >= self.original_x + self.move_range:
                self.move_direction *= -1
    
    def draw(self, surface):
        # 绘制地板
        floor_rect = pygame.Rect(int(self.x), int(self.y), self.width, self.height)
        pygame.draw.rect(surface, FLOOR_COLOR, floor_rect, border_radius=5)
        pygame.draw.rect(surface, FLOOR_HIGHLIGHT, floor_rect, 2, border_radius=5)
        
        # 绘制地板纹理
        for i in range(0, self.width, 10):
            line_x = self.x + i
            pygame.draw.line(surface, FLOOR_HIGHLIGHT, 
                           (line_x, self.y), 
                           (line_x, self.y + self.height), 1)
        
        # 如果是移动地板，绘制特殊标记
        if self.is_moving:
            move_indicator = pygame.Rect(int(self.x + 5), int(self.y - 5), 10, 5)
            pygame.draw.rect(surface, (255, 200, 50), move_indicator, border_radius=2)

# 危险物类
class Danger:
    def __init__(self, x, y, width, height, is_moving=False, move_range=0, move_speed=0):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.is_moving = is_moving
        self.move_range = move_range
        self.move_speed = move_speed
        self.move_direction = 1
        self.original_x = x
        self.animation_timer = 0
        
    def update(self):
        self.animation_timer += 1
        if self.is_moving:
            self.x += self.move_speed * self.move_direction
            if self.x <= self.original_x - self.move_range or self.x >= self.original_x + self.move_range:
                self.move_direction *= -1
    
    def draw(self, surface):
        # 绘制危险物
        danger_rect = pygame.Rect(int(self.x), int(self.y), self.width, self.height)
        
        # 绘制主体
        pygame.draw.rect(surface, DANGER_COLOR, danger_rect, border_radius=3)
        pygame.draw.rect(surface, DANGER_OUTLINE, danger_rect, 2, border_radius=3)
        
        # 绘制警告图案
        spike_height = 8
        for i in range(0, self.width, 15):
            spike_x = self.x + i
            spike_points = [
                (spike_x, self.y + self.height),
                (spike_x + 7, self.y + self.height - spike_height),
                (spike_x + 14, self.y + self.height)
            ]
            pygame.draw.polygon(surface, DANGER_OUTLINE, spike_points)
        
        # 闪烁效果
        if int(self.animation_timer / 10) % 2 == 0:
            flash_rect = pygame.Rect(int(self.x + 2), int(self.y + 2), 
                                   self.width - 4, self.height - 4)
            pygame.draw.rect(surface, (255, 255, 255, 100), flash_rect, border_radius=2)

# 游戏主类
class Game:
    def __init__(self):
        self.game_state = GAME_STATE_MENU
        self.camera_y = 0
        self.target_camera_y = 0
        self.scroll_speed = 3
        
        # 游戏参数（这些需要在reset_game之前定义）
        self.max_floors = 15
        self.floor_gap = 120
        self.difficulty = 1
        self.floor_width_range = (80, 150)
        self.danger_chance = 0.3
        self.moving_floor_chance = 0.2
        self.moving_danger_chance = 0.4
        
        self.reset_game()
    
    def reset_game(self):
        self.player = Player(WIDTH // 2 - 20, HEIGHT - 200)
        self.floors = []
        self.dangers = []
        self.score = 0
        self.highest_score = 0
        self.camera_y = 0
        self.target_camera_y = 0
        self.game_time = 0
        
        # 重置游戏参数到初始值
        self.max_floors = 15
        self.floor_gap = 120
        self.difficulty = 1
        self.floor_width_range = (80, 150)
        self.danger_chance = 0.3
        self.moving_floor_chance = 0.2
        self.moving_danger_chance = 0.4
        
        # 创建初始地板
        self.floors.append(Floor(WIDTH // 2 - 100, HEIGHT - 50, 200))
        
        # 生成初始楼层
        for i in range(self.max_floors):
            self.generate_floor(HEIGHT - 100 - i * self.floor_gap)
    
    def generate_floor(self, y):
        # 随机决定地板宽度
        width = random.randint(self.floor_width_range[0], self.floor_width_range[1])
        
        # 随机位置，确保地板在屏幕内
        max_x = WIDTH - width
        if max_x > 0:
            x = random.randint(0, max_x)
        else:
            x = 0
            
        # 随机决定是否是移动地板
        is_moving = random.random() < self.moving_floor_chance
        move_range = 0
        move_speed = 0
        
        if is_moving:
            move_range = random.randint(30, 80)
            move_speed = random.uniform(0.5, 2.0)
        
        # 创建地板
        floor = Floor(x, y, width, is_moving, move_range, move_speed)
        self.floors.append(floor)
        
        # 随机生成危险物
        if random.random() < self.danger_chance and y < HEIGHT - 200:  # 不在最底层生成危险物
            danger_width = random.randint(20, 60)
            danger_height = random.randint(20, 40)
            
            # 确保危险物在地板上
            danger_x = random.randint(int(x), int(x + width - danger_width))
            danger_y = y - danger_height
            
            # 随机决定是否是移动危险物
            is_moving_danger = random.random() < self.moving_danger_chance
            danger_move_range = 0
            danger_move_speed = 0
            
            if is_moving_danger:
                danger_move_range = random.randint(20, 60)
                danger_move_speed = random.uniform(0.8, 2.0)
            
            danger = Danger(danger_x, danger_y, danger_width, danger_height, 
                           is_moving_danger, danger_move_range, danger_move_speed)
            self.dangers.append(danger)
    
    def update(self):
        if self.game_state != GAME_STATE_PLAYING:
            return
            
        self.game_time += 1
        
        # 更新玩家
        player_alive = self.player.update(self.floors, self.dangers)
        
        if not player_alive:
            self.game_state = GAME_STATE_GAME_OVER
            if self.score > self.highest_score:
                self.highest_score = self.score
            return
        
        # 更新所有地板
        for floor in self.floors:
            floor.update()
        
        # 更新所有危险物
        for danger in self.dangers:
            danger.update()
        
        # 相机跟随玩家（向上滚动）
        if self.player.y < HEIGHT * 0.4:
            self.target_camera_y = HEIGHT * 0.4 - self.player.y
        
        # 平滑移动相机
        self.camera_y += (self.target_camera_y - self.camera_y) * 0.1
        
        # 更新分数（基于相机位置）
        new_score = int(self.camera_y / 10)
        if new_score > self.score:
            self.score = new_score
        
        # 检查是否胜利
        if self.score >= 100:
            self.game_state = GAME_STATE_WIN
            if self.score > self.highest_score:
                self.highest_score = self.score
        
        # 移除超出屏幕的地板和危险物
        self.floors = [floor for floor in self.floors if floor.y - self.camera_y < HEIGHT + 100]
        self.dangers = [danger for danger in self.dangers if danger.y - self.camera_y < HEIGHT + 100]
        
        # 生成新地板
        if len(self.floors) < self.max_floors:
            lowest_floor_y = max([floor.y for floor in self.floors]) if self.floors else HEIGHT
            new_floor_y = lowest_floor_y - self.floor_gap
            self.generate_floor(new_floor_y)
        
        # 随着分数增加提高难度
        self.difficulty = 1 + self.score / 100
        self.floor_gap = 120 + self.score / 5
        self.danger_chance = 0.3 + min(self.score / 500, 0.4)
        self.moving_floor_chance = 0.2 + min(self.score / 300, 0.3)
        self.moving_danger_chance = 0.4 + min(self.score / 400, 0.3)
    
    def draw(self, surface):
        # 绘制背景
        surface.fill(BACKGROUND)
        
        # 绘制渐变背景
        for i in range(HEIGHT // 2):
            color_value = 5 + i * 20 // (HEIGHT // 2)
            pygame.draw.line(surface, (color_value, color_value + 5, color_value + 10), 
                           (0, i), (WIDTH, i))
        
        # 绘制星星
        for _ in range(50):
            x = random.randint(0, WIDTH)
            y = random.randint(0, HEIGHT)
            size = random.randint(1, 3)
            brightness = random.randint(150, 255)
            pygame.draw.circle(surface, (brightness, brightness, brightness), (x, y), size)
        
        # 绘制地板和危险物（考虑相机偏移）
        for floor in self.floors:
            floor.y_with_camera = floor.y - self.camera_y
            if floor.y_with_camera > -50 and floor.y_with_camera < HEIGHT + 50:
                floor.draw(surface)
        
        for danger in self.dangers:
            danger.y_with_camera = danger.y - self.camera_y
            if danger.y_with_camera > -50 and danger.y_with_camera < HEIGHT + 50:
                danger.draw(surface)
        
        # 绘制玩家（考虑相机偏移）
        player_y_with_camera = self.player.y - self.camera_y
        if player_y_with_camera > -100 and player_y_with_camera < HEIGHT + 100:
            self.player.draw(surface)
        
        # 绘制UI
        self.draw_ui(surface)
        
        # 根据游戏状态绘制不同界面
        if self.game_state == GAME_STATE_MENU:
            self.draw_menu(surface)
        elif self.game_state == GAME_STATE_GAME_OVER:
            self.draw_game_over(surface)
        elif self.game_state == GAME_STATE_WIN:
            self.draw_win(surface)
    
    def draw_ui(self, surface):
        # 绘制分数
        score_text = score_font.render(f"层数: {self.score}", True, TEXT_COLOR)
        score_shadow = score_font.render(f"层数: {self.score}", True, TEXT_SHADOW)
        surface.blit(score_shadow, (12, 12))
        surface.blit(score_text, (10, 10))
        
        # 绘制最高分
        high_score_text = info_font.render(f"最高记录: {self.highest_score}", True, TEXT_COLOR)
        high_score_shadow = info_font.render(f"最高记录: {self.highest_score}", True, TEXT_SHADOW)
        surface.blit(high_score_shadow, (12, 52))
        surface.blit(high_score_text, (10, 50))
        
        # 绘制目标
        target_text = info_font.render("目标: 100层", True, TEXT_COLOR)
        target_shadow = info_font.render("目标: 100层", True, TEXT_SHADOW)
        surface.blit(target_shadow, (WIDTH - target_text.get_width() - 8, 12))
        surface.blit(target_text, (WIDTH - target_text.get_width() - 10, 10))
        
        # 绘制进度条
        progress_width = 200
        progress_height = 20
        progress_x = WIDTH // 2 - progress_width // 2
        progress_y = 20
        progress = min(self.score / 100, 1.0)
        
        # 绘制进度条背景
        pygame.draw.rect(surface, (50, 50, 70), 
                        (progress_x, progress_y, progress_width, progress_height), 
                        border_radius=10)
        
        # 绘制进度条填充
        if progress > 0:
            pygame.draw.rect(surface, (100, 200, 100), 
                            (progress_x, progress_y, int(progress_width * progress), progress_height), 
                            border_radius=10)
        
        # 绘制进度条边框
        pygame.draw.rect(surface, (150, 150, 150), 
                        (progress_x, progress_y, progress_width, progress_height), 
                        2, border_radius=10)
        
        # 绘制进度文本
        progress_text = small_font.render(f"{int(progress * 100)}%", True, TEXT_COLOR)
        surface.blit(progress_text, (WIDTH // 2 - progress_text.get_width() // 2, progress_y + 2))
        
        # 绘制控制说明
        controls = [
            "控制: 左右方向键移动, 空格键跳跃",
            "提示: 可以二段跳, 避开红色危险物"
        ]
        
        for i, control in enumerate(controls):
            control_text = small_font.render(control, True, TEXT_COLOR)
            surface.blit(control_text, (WIDTH // 2 - control_text.get_width() // 2, 
                                        HEIGHT - 60 + i * 20))
    
    def draw_menu(self, surface):
        # 半透明覆盖层
        overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 0, 0, 200))
        surface.blit(overlay, (0, 0))
        
        # 绘制标题
        title = title_font.render("是男人就上100层", True, (255, 255, 200))
        title_shadow = title_font.render("是男人就上100层", True, (100, 100, 50))
        surface.blit(title_shadow, (WIDTH//2 - title.get_width()//2 + 3, HEIGHT//2 - 150 + 3))
        surface.blit(title, (WIDTH//2 - title.get_width()//2, HEIGHT//2 - 150))
        
        # 绘制开始按钮
        start_button = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 - 30, 200, 60)
        mouse_pos = pygame.mouse.get_pos()
        button_hover = start_button.collidepoint(mouse_pos)
        
        button_color = BUTTON_HOVER if button_hover else BUTTON_COLOR
        pygame.draw.rect(surface, button_color, start_button, border_radius=15)
        pygame.draw.rect(surface, (255, 255, 255), start_button, 3, border_radius=15)
        
        start_text = button_font.render("开始游戏", True, TEXT_COLOR)
        surface.blit(start_text, (WIDTH//2 - start_text.get_width()//2, 
                                 HEIGHT//2 - start_text.get_height()//2 - 30))
        
        # 绘制游戏说明
        instructions = [
            "游戏说明:",
            "1. 控制角色不断向上跳跃",
            "2. 避开红色危险物",
            "3. 达到100层即可获胜",
            "4. 可以使用二段跳",
            "5. 移动地板可以帮助你到达更远的地方"
        ]
        
        for i, instruction in enumerate(instructions):
            instruction_text = info_font.render(instruction, True, TEXT_COLOR)
            surface.blit(instruction_text, (WIDTH//2 - instruction_text.get_width()//2, 
                                           HEIGHT//2 + 50 + i * 30))
        
        # 绘制最高分
        if self.highest_score > 0:
            high_score_text = score_font.render(f"最高记录: {self.highest_score}层", True, (255, 200, 100))
            surface.blit(high_score_text, (WIDTH//2 - high_score_text.get_width()//2, HEIGHT//2 + 230))
    
    def draw_game_over(self, surface):
        # 半透明覆盖层
        overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        overlay.fill((*GAME_OVER_BG, 200))
        surface.blit(overlay, (0, 0))
        
        # 绘制游戏结束文本
        game_over_text = title_font.render("游戏结束", True, (255, 100, 100))
        surface.blit(game_over_text, (WIDTH//2 - game_over_text.get_width()//2, HEIGHT//2 - 100))
        
        # 绘制分数
        score_text = score_font.render(f"本次得分: {self.score}层", True, TEXT_COLOR)
        surface.blit(score_text, (WIDTH//2 - score_text.get_width()//2, HEIGHT//2 - 30))
        
        if self.highest_score > 0:
            high_score_text = score_font.render(f"最高记录: {self.highest_score}层", True, (255, 200, 100))
            surface.blit(high_score_text, (WIDTH//2 - high_score_text.get_width()//2, HEIGHT//2 + 10))
        
        # 绘制重新开始按钮
        restart_button = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 + 60, 200, 60)
        mouse_pos = pygame.mouse.get_pos()
        button_hover = restart_button.collidepoint(mouse_pos)
        
        button_color = BUTTON_HOVER if button_hover else BUTTON_COLOR
        pygame.draw.rect(surface, button_color, restart_button, border_radius=15)
        pygame.draw.rect(surface, (255, 255, 255), restart_button, 3, border_radius=15)
        
        restart_text = button_font.render("重新开始", True, TEXT_COLOR)
        surface.blit(restart_text, (WIDTH//2 - restart_text.get_width()//2, 
                                   HEIGHT//2 + 60 + restart_text.get_height()//2))
        
        # 绘制返回菜单按钮
        menu_button = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 + 140, 200, 60)
        menu_hover = menu_button.collidepoint(mouse_pos)
        
        menu_button_color = BUTTON_HOVER if menu_hover else BUTTON_COLOR
        pygame.draw.rect(surface, menu_button_color, menu_button, border_radius=15)
        pygame.draw.rect(surface, (255, 255, 255), menu_button, 3, border_radius=15)
        
        menu_text = button_font.render("返回菜单", True, TEXT_COLOR)
        surface.blit(menu_text, (WIDTH//2 - menu_text.get_width()//2, 
                                HEIGHT//2 + 140 + menu_text.get_height()//2))
    
    def draw_win(self, surface):
        # 半透明覆盖层
        overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 30, 0, 200))
        surface.blit(overlay, (0, 0))
        
        # 绘制胜利文本
        win_text = title_font.render("恭喜通关!", True, (100, 255, 100))
        surface.blit(win_text, (WIDTH//2 - win_text.get_width()//2, HEIGHT//2 - 100))
        
        congrats_text = score_font.render("你是真男人! 达到了100层!", True, (200, 255, 200))
        surface.blit(congrats_text, (WIDTH//2 - congrats_text.get_width()//2, HEIGHT//2 - 30))
        
        # 绘制分数
        score_text = score_font.render(f"最终得分: {self.score}层", True, TEXT_COLOR)
        surface.blit(score_text, (WIDTH//2 - score_text.get_width()//2, HEIGHT//2 + 20))
        
        # 绘制重新开始按钮
        restart_button = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 + 80, 200, 60)
        mouse_pos = pygame.mouse.get_pos()
        button_hover = restart_button.collidepoint(mouse_pos)
        
        button_color = BUTTON_HOVER if button_hover else BUTTON_COLOR
        pygame.draw.rect(surface, button_color, restart_button, border_radius=15)
        pygame.draw.rect(surface, (255, 255, 255), restart_button, 3, border_radius=15)
        
        restart_text = button_font.render("再玩一次", True, TEXT_COLOR)
        surface.blit(restart_text, (WIDTH//2 - restart_text.get_width()//2, 
                                   HEIGHT//2 + 80 + restart_text.get_height()//2))
        
        # 绘制返回菜单按钮
        menu_button = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 + 160, 200, 60)
        menu_hover = menu_button.collidepoint(mouse_pos)
        
        menu_button_color = BUTTON_HOVER if menu_hover else BUTTON_COLOR
        pygame.draw.rect(surface, menu_button_color, menu_button, border_radius=15)
        pygame.draw.rect(surface, (255, 255, 255), menu_button, 3, border_radius=15)
        
        menu_text = button_font.render("返回菜单", True, TEXT_COLOR)
        surface.blit(menu_text, (WIDTH//2 - menu_text.get_width()//2, 
                                HEIGHT//2 + 160 + menu_text.get_height()//2))
    
    def handle_event(self, event):
        mouse_pos = pygame.mouse.get_pos()
        
        if self.game_state == GAME_STATE_PLAYING:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    self.player.move_left()
                elif event.key == pygame.K_RIGHT:
                    self.player.move_right()
                elif event.key == pygame.K_SPACE:
                    self.player.jump()
                elif event.key == pygame.K_ESCAPE:
                    self.game_state = GAME_STATE_MENU
            
            elif event.type == pygame.KEYUP:
                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    self.player.stop_horizontal()
        
        elif self.game_state == GAME_STATE_MENU:
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                # 检查开始按钮
                start_button = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 - 30, 200, 60)
                if start_button.collidepoint(mouse_pos):
                    self.reset_game()
                    self.game_state = GAME_STATE_PLAYING
        
        elif self.game_state == GAME_STATE_GAME_OVER or self.game_state == GAME_STATE_WIN:
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                # 检查重新开始按钮
                restart_button = pygame.Rect(WIDTH//2 - 100, 
                                            HEIGHT//2 + 60 if self.game_state == GAME_STATE_GAME_OVER else HEIGHT//2 + 80, 
                                            200, 60)
                if restart_button.collidepoint(mouse_pos):
                    self.reset_game()
                    self.game_state = GAME_STATE_PLAYING
                
                # 检查返回菜单按钮
                menu_button = pygame.Rect(WIDTH//2 - 100, 
                                         HEIGHT//2 + 140 if self.game_state == GAME_STATE_GAME_OVER else HEIGHT//2 + 160, 
                                         200, 60)
                if menu_button.collidepoint(mouse_pos):
                    self.game_state = GAME_STATE_MENU
        
        # 通用事件处理
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

# 主函数
def main():
    clock = pygame.time.Clock()
    game = Game()
    
    # 游戏主循环
    running = True
    while running:
        # 处理事件
        for event in pygame.event.get():
            game.handle_event(event)
        
        # 更新游戏状态
        game.update()
        
        # 绘制游戏
        game.draw(screen)
        
        # 更新显示
        pygame.display.flip()
        
        # 控制帧率
        clock.tick(60)

if __name__ == "__main__":
    main()
