import pygame
import random
import sys

# 初始化pygame
pygame.init()

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

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

# 帧率控制器
clock = pygame.time.Clock()
FPS = 60

# 玩家飞机类
class Plane:
    def __init__(self):
        self.width = 40
        self.height = 50
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - 100
        self.speed = 7

    def move(self):
        # 按键控制
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.x > 0:
            self.x -= self.speed
        if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width:
            self.x += self.speed
        if keys[pygame.K_UP] and self.y > 0:
            self.y -= self.speed
        if keys[pygame.K_DOWN] and self.y < HEIGHT - self.height:
            self.y += self.speed

    def draw(self):
        # 绘制简易飞机
        pygame.draw.polygon(screen, BLUE, [
            (self.x + self.width//2, self.y),
            (self.x, self.y + self.height),
            (self.x + self.width, self.y + self.height)
        ])
        pygame.draw.rect(screen, WHITE, (self.x+12, self.y+15, 16, 20))

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

# 陨石障碍物类
class Rock:
    def __init__(self):
        self.size = random.randint(20, 50)
        self.x = random.randint(0, WIDTH - self.size)
        self.y = -self.size
        self.speed = random.randint(4, 9)

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

    def draw(self):
        pygame.draw.circle(screen, GRAY, (self.x + self.size//2, self.y + self.size//2), self.size//2)

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

# 游戏主逻辑
def game_loop():
    plane = Plane()
    rocks = []
    score = 0
    game_over = False
    font = pygame.font.SysFont(None, 40)

    while True:
        screen.fill(BLACK)
        # 事件监听
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            # 游戏结束按空格重新开始
            if event.type == pygame.KEYDOWN:
                if game_over and event.key == pygame.K_SPACE:
                    game_loop()

        if not game_over:
            # 飞机移动
            plane.move()
            plane.draw()

            # 随机生成陨石
            if random.randint(1, 30) == 1:
                rocks.append(Rock())

            # 更新并绘制陨石
            for rock in rocks[:]:
                rock.update()
                rock.draw()
                # 移除出屏幕的陨石并加分
                if rock.y > HEIGHT:
                    rocks.remove(rock)
                    score += 1
                # 碰撞检测
                if plane.get_rect().colliderect(rock.get_rect()):
                    game_over = True

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

        else:
            # 游戏结束界面
            over_text = font.render("游戏结束！", True, RED)
            tip_text = font.render("按空格重新开始", True, WHITE)
            screen.blit(over_text, (WIDTH//2 - 100, HEIGHT//2 - 50))
            screen.blit(tip_text, (WIDTH//2 - 130, HEIGHT//2 + 20))

        pygame.display.flip()
        clock.tick(FPS)

# 启动游戏
if __name__ == "__main__":
    game_loop()
