import pygame
import sys
import random

pygame.init()

# 窗口设置
WIDTH = 480
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞行躲避")

# 颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (80, 180, 255)
GRAY = (120, 120, 120)
RED = (255, 60, 60)

# 玩家飞机
player_w = 40
player_h = 40
player_x = WIDTH // 2 - player_w // 2
player_y = HEIGHT - 100
player_speed = 7

# 障碍物
block_width = 50
block_height = 30
blocks = []
block_speed = 4
spawn_interval = 60

# 游戏变量
clock = pygame.time.Clock()
frame_count = 0
score = 0
game_over = False

# 字体（兼容所有系统）
font = pygame.font.Font(None, 38)

def reset_game():
    global player_x, blocks, block_speed, frame_count, score, game_over
    player_x = WIDTH // 2 - player_w // 2
    blocks.clear()
    block_speed = 4
    frame_count = 0
    score = 0
    game_over = False

reset_game()

running = True
while running:
    clock.tick(60)
    screen.fill(BLACK)
    frame_count += 1

    # 事件处理
    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_SPACE:
                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 + player_w < WIDTH:
            player_x += player_speed

        # 生成障碍物
        if frame_count % spawn_interval == 0:
            bx = random.randint(0, WIDTH - block_width)
            blocks.append([bx, -block_height])
            # 略微提速，增加难度
            if block_speed < 9:
                block_speed += 0.05

        # 更新障碍物
        new_blocks = []
        for pos in blocks:
            bx, by = pos
            by += block_speed
            if by < HEIGHT:
                new_blocks.append([bx, by])
                # 绘制障碍物
                pygame.draw.rect(screen, GRAY, (bx, by, block_width, block_height))

                # 碰撞检测
                player_rect = pygame.Rect(player_x, player_y, player_w, player_h)
                block_rect = pygame.Rect(bx, by, block_width, block_height)
                if player_rect.colliderect(block_rect):
                    game_over = True
            else:
                # 成功躲过，加分
                score += 1
        blocks = new_blocks

        # 绘制玩家飞机
        pygame.draw.polygon(screen, BLUE, [
            (player_x + player_w//2, player_y),
            (player_x, player_y + player_h),
            (player_x + player_w, player_y + player_h)
        ])

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

    else:
        # 游戏结束界面
        text1 = font.render("撞上障碍物！", True, RED)
        text2 = font.render(f"最终分数：{score}", True, WHITE)
        text3 = font.render("按空格重新开始", True, WHITE)
        screen.blit(text1, (WIDTH//2 - 110, HEIGHT//2 - 80))
        screen.blit(text2, (WIDTH//2 - 100, HEIGHT//2 - 30))
        screen.blit(text3, (WIDTH//2 - 130, HEIGHT//2 + 20))

    pygame.display.flip()

pygame.quit()
sys.exit()