import pygame
import random
import math

# 初始化
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("开学快乐！Happy School Start")

# 颜色定义
BLACK = (0, 0, 0)
COLORS = [(255, 50, 50), (50, 255, 50), (50, 50, 255), (255, 255, 50), (255, 50, 255), (50, 255, 255)]

# 烟花粒子类
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.velocity = [random.uniform(-5, 5), random.uniform(-5, 5)]
        self.life = 100

    def move(self):
        self.x += self.velocity[0]
        self.y += self.velocity[1]
        self.velocity[1] += 0.1  # 重力模拟
        self.life -= 2

    def draw(self, surface):
        if self.life > 0:
            pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 3)

# 烟花发射类
class Firework:
    def __init__(self):
        self.x = random.randint(100, WIDTH - 100)
        self.y = HEIGHT
        self.target_y = random.randint(100, 300)
        self.color = random.choice(COLORS)
        self.speed = 7
        self.exploded = False
        self.particles = []

    def update(self):
        if not self.exploded:
            self.y -= self.speed
            if self.y <= self.target_y:
                self.exploded = True
                for _ in range(50):
                    self.particles.append(Particle(self.x, self.y, self.color))
        else:
            for p in self.particles:
                p.move()
            self.particles = [p for p in self.particles if p.life > 0]

    def draw(self, surface):
        if not self.exploded:
            pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 5)
        else:
            for p in self.particles:
                p.draw(surface)

# 字体设置
try:
    # 尝试加载中文字体（Windows常用路径，如果是Mac/Linux需更换）
    font_large = pygame.font.SysFont("SimHei", 80)
    font_small = pygame.font.SysFont("Arial", 40)
except:
    font_large = pygame.font.SysFont("Arial", 80)
    font_small = pygame.font.SysFont("Arial", 40)

def main():
    clock = pygame.time.Clock()
    fireworks = []
    running = True
    angle = 0

    while running:
        screen.fill(BLACK)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # 随机产生烟花
        if random.random() < 0.05:
            fireworks.append(Firework())

        # 更新和绘制烟花
        for fw in fireworks[:]:
            fw.update()
            fw.draw(screen)
            if fw.exploded and len(fw.particles) == 0:
                fireworks.remove(fw)

        # 制作跳动的文字效果
        angle += 0.1
        y_offset = math.sin(angle) * 20
        
        # 绘制彩色文字
        text_surf = font_large.render("开 学 快 乐", True, (255, 255, 255))
        text_rect = text_surf.get_rect(center=(WIDTH//2, HEIGHT//2 + y_offset))
        
        # 简单的文字阴影/发光效果
        screen.blit(text_surf, text_rect)
        
        sub_text = font_small.render("Welcome Back to School!", True, random.choice(COLORS))
        screen.blit(sub_text, (WIDTH//2 - 200, HEIGHT//2 + 80))

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

    pygame.quit()

if __name__ == "__main__":
    main()
