import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 480, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞行躲避游戏")
clock = pygame.time.Clock()
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 60, 60)
BLUE = (40, 140, 255)
GRAY = (80, 80, 80)
GREEN = (60, 220, 80)

# ==========修复字体BUG关键改动==========
# 不用SysFont(None,xx)，使用系统默认字体名称兜底
try:
    font_big = pygame.font.SysFont("simhei", 60)
    font_mid = pygame.font.SysFont("simhei", 36)
    font_small = pygame.font.SysFont("simhei", 28)
except:
    # 找不到黑体时使用默认构造方式
    font_big = pygame.font.Font(pygame.font.get_default_font(), 60)
    font_mid = pygame.font.Font(pygame.font.get_default_font(), 36)
    font_small = pygame.font.Font(pygame.font.get_default_font(), 28)

# 玩家飞机类
class Player:
    def __init__(self):
        self.w = 40
        self.h = 50
        self.x = WIDTH // 2 - self.w // 2
        self.y = HEIGHT - 120
        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)

    def update(self):
        # 跟随鼠标
        mx, my = pygame.mouse.get_pos()
        self.rect.centerx = mx
        self.rect.centery = my
        # 边界限制
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT

    def draw(self):
        pygame.draw.polygon(screen, BLUE, [
            (self.rect.centerx, self.rect.top),
            (self.rect.left, self.rect.bottom),
            (self.rect.right, self.rect.bottom)
        ])

# 障碍物陨石类
class Asteroid:
    def __init__(self):
        self.size = random.randint(25, 55)
        self.x = random.randint(0, WIDTH - self.size)
        self.y = random.randint(-120, -40)
        self.speed = random.randint(4, 9)
        self.rect = pygame.Rect(self.x, self.y, self.size, self.size)

    def update(self):
        self.rect.y += self.speed

    def draw(self):
        pygame.draw.ellipse(screen, GRAY, self.rect)

# 游戏状态
STATE_START = 0
STATE_GAME = 1
STATE_GAMEOVER = 2

def main():
    game_state = STATE_START
    player = Player()
    asteroids = []
    score = 0
    spawn_timer = 0

    while True:
        clock.tick(FPS)
        screen.fill(BLACK)

        # 事件循环
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if game_state == STATE_START:
                    # 点击开始游戏
                    game_state = STATE_GAME
                    asteroids.clear()
                    score = 0
                elif game_state == STATE_GAMEOVER:
                    # 重新开始
                    game_state = STATE_GAME
                    asteroids.clear()
                    score = 0

        if game_state == STATE_START:
            # 开始界面
            title = font_big.render("飞行躲避", True, WHITE)
            tip = font_mid.render("点击画面开始游戏", True, GREEN)
            info = font_small.render("鼠标控制飞机移动，躲避陨石", True, WHITE)
            screen.blit(title, (WIDTH//2 - title.get_width()//2, 220))
            screen.blit(tip, (WIDTH//2 - tip.get_width()//2, 320))
            screen.blit(info, (WIDTH//2 - info.get_width()//2, 380))

        elif game_state == STATE_GAME:
            player.update()
            # 生成陨石
            spawn_timer += 1
            if spawn_timer > 25:
                asteroids.append(Asteroid())
                spawn_timer = 0

            # 更新陨石
            for ast in asteroids[:]:
                ast.update()
                # 移出屏幕清除，加分
                if ast.rect.top > HEIGHT:
                    asteroids.remove(ast)
                    score += 1
                # 碰撞检测
                if player.rect.colliderect(ast.rect):
                    game_state = STATE_GAMEOVER

            # 绘制所有物体
            player.draw()
            for ast in asteroids:
                ast.draw()
            # 绘制分数
            score_text = font_mid.render(f"分数: {score}", True, WHITE)
            screen.blit(score_text, (10, 10))

        elif game_state == STATE_GAMEOVER:
            # 结束界面
            over_text = font_big.render("游戏结束", True, RED)
            score_text = font_mid.render(f"最终分数: {score}", True, WHITE)
            restart_tip = font_mid.render("点击重新开始", True, GREEN)
            screen.blit(over_text, (WIDTH//2 - over_text.get_width()//2, 200))
            screen.blit(score_text, (WIDTH//2 - score_text.get_width()//2, 300))
            screen.blit(restart_tip, (WIDTH//2 - restart_tip.get_width()//2, 380))

        pygame.display.flip()

if __name__ == "__main__":
    main()