import pygame
import random
import sys

# 初始化
pygame.init()

# 屏幕设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("🏂 滑雪模拟器")

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (34, 139, 34)
RED = (220, 20, 60)
BLUE = (30, 144, 255)
BROWN = (139, 69, 19)
YELLOW = (255, 215, 0)

# 字体
font = pygame.font.Font(None, 36)
big_font = pygame.font.Font(None, 72)

# 时钟
clock = pygame.time.Clock()
FPS = 60

# 玩家属性
player_width = 40
player_height = 60
player_x = SCREEN_WIDTH // 2 - player_width // 2
player_y = SCREEN_HEIGHT - 100
player_speed = 5

# 障碍物列表
obstacles = []  # 每个障碍物：[x, y, width, height, type, speed]
obstacle_timer = 0
base_obstacle_interval = 60  # 初始间隔（帧）
min_interval = 20
score = 0
game_over = False

# 重新开始函数
def reset_game():
    global player_x, obstacles, obstacle_timer, score, game_over
    player_x = SCREEN_WIDTH // 2 - player_width // 2
    obstacles.clear()
    obstacle_timer = 0
    score = 0
    game_over = False

# 绘制玩家（简单几何表示）
def draw_player(x, y):
    # 身体（滑雪服）
    pygame.draw.rect(screen, BLUE, (x, y, player_width, player_height))
    # 头部
    pygame.draw.circle(screen, (255, 220, 180), (x + player_width//2, y - 10), 12)
    # 帽子
    pygame.draw.ellipse(screen, RED, (x + 5, y - 25, 30, 18))
    # 滑雪杖
    pygame.draw.line(screen, BLACK, (x + 5, y + 10), (x - 10, y + 30), 3)
    pygame.draw.line(screen, BLACK, (x + player_width - 5, y + 10), (x + player_width + 10, y + 30), 3)
    # 滑雪板
    pygame.draw.rect(screen, BROWN, (x - 15, y + player_height, 70, 8))

# 生成障碍物
def spawn_obstacle():
    obstacle_type = random.choice(['tree', 'snowball'])
    if obstacle_type == 'tree':
        width = 30
        height = 50
        speed = random.randint(3, 6)
    else:
        width = 30
        height = 30
        speed = random.randint(4, 7)
    x = random.randint(0, SCREEN_WIDTH - width)
    y = -height
    obstacles.append([x, y, width, height, obstacle_type, speed])

# 绘制障碍物
def draw_obstacles():
    for obs in obstacles:
        x, y, w, h, typ, _ = obs
        if typ == 'tree':
            # 树干
            pygame.draw.rect(screen, BROWN, (x + w//2 - 5, y + 20, 10, 30))
            # 树冠（三角形）
            pygame.draw.polygon(screen, GREEN, [
                (x + w//2, y),
                (x, y + 30),
                (x + w, y + 30)
            ])
        elif typ == 'snowball':
            pygame.draw.circle(screen, WHITE, (x + w//2, y + h//2), w//2)
            pygame.draw.circle(screen, (200, 200, 200), (x + w//2, y + h//2), w//2, 2)

# 碰撞检测
def check_collision():
    player_rect = pygame.Rect(player_x, player_y, player_width, player_height)
    for obs in obstacles:
        obs_rect = pygame.Rect(obs[0], obs[1], obs[2], obs[3])
        if player_rect.colliderect(obs_rect):
            return True
    return False

# 游戏主循环
reset_game()
running = True

while running:
    clock.tick(FPS)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if game_over and event.key == pygame.K_r:
                reset_game()

    # 键盘移动
    if not game_over:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= player_speed
        if keys[pygame.K_RIGHT] and player_x < SCREEN_WIDTH - player_width:
            player_x += player_speed

    # 生成障碍物（间隔随分数递减）
    if not game_over:
        obstacle_timer += 1
        # 计算当前间隔
        interval = max(min_interval, base_obstacle_interval - score // 200)
        if obstacle_timer >= interval:
            spawn_obstacle()
            obstacle_timer = 0

    # 更新障碍物位置
    if not game_over:
        for obs in obstacles[:]:
            obs[1] += obs[5]  # y增加
            if obs[1] > SCREEN_HEIGHT:
                obstacles.remove(obs)
                score += 10  # 成功躲过一个障碍物额外加分

        # 碰撞检测
        if check_collision():
            game_over = True

        # 生存分数
        score += 1

    # 绘制画面
    screen.fill(WHITE)

    # 画雪道边缘（简单的树线）
    for i in range(0, SCREEN_WIDTH, 50):
        pygame.draw.circle(screen, GREEN, (i, 0), 20)
        pygame.draw.circle(screen, GREEN, (i, SCREEN_HEIGHT), 20)

    # 绘制玩家和障碍物
    draw_player(player_x, player_y)
    draw_obstacles()

    # 显示分数
    score_text = font.render(f"分数: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))

    # 游戏结束画面
    if game_over:
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 0, 0, 128))
        screen.blit(overlay, (0, 0))
        game_over_text = big_font.render("游戏结束", True, RED)
        restart_text = font.render("按 R 重新开始", True, WHITE)
        screen.blit(game_over_text, (SCREEN_WIDTH//2 - 120, SCREEN_HEIGHT//2 - 50))
        screen.blit(restart_text, (SCREEN_WIDTH//2 - 80, SCREEN_HEIGHT//2 + 20))

    pygame.display.flip()

pygame.quit()
sys.exit()