import turtle
import random
import math

# 初始化画布
screen = turtle.Screen()
screen.setup(800, 600)  # 窗口大小
screen.bgcolor("black")  # 黑色背景
screen.title("动态烟花")
screen.tracer(0)  # 关闭自动刷新，提升动画流畅度

# 烟花粒子类
class Firework:
    def __init__(self):
        # 烟花发射器（底部随机位置）
        self.launcher = turtle.Turtle()
        self.launcher.hideturtle()
        self.launcher.speed(0)
        self.launcher.penup()
        self.x = random.randint(-350, 350)
        self.y = -280
        self.launcher.goto(self.x, self.y)
        
        # 发射状态与速度
        self.launch_speed = random.randint(15, 25)
        self.explode_y = random.randint(100, 250)  # 爆炸高度
        self.state = "launch"  # launch=发射中 explode=爆炸中
        
        # 爆炸粒子
        self.particles = []
        self.color = (random.random(), random.random(), random.random())  # 随机颜色
    
    def launch(self):
        # 向上发射
        self.y += self.launch_speed
        self.launcher.goto(self.x, self.y)
        self.launcher.clear()
        self.launcher.dot(4, self.color)
        
        # 到达爆炸高度，生成粒子
        if self.y >= self.explode_y:
            self.state = "explode"
            self.create_particles()
    
    def create_particles(self):
        # 生成30-50个爆炸粒子
        for _ in range(random.randint(30, 50)):
            angle = random.uniform(0, math.pi * 2)
            speed = random.uniform(3, 8)
            vx = math.cos(angle) * speed
            vy = math.sin(angle) * speed
            particle = {
                "x": self.x, "y": self.y,
                "vx": vx, "vy": vy,
                "life": 60,  # 粒子存活时间
                "color": self.color
            }
            self.particles.append(particle)
    
    def explode(self):
        # 爆炸动画：粒子扩散+下落+渐隐
        for p in self.particles[:]:
            p["vy"] -= 0.2  # 重力下落
            p["x"] += p["vx"]
            p["y"] += p["vy"]
            p["life"] -= 1
            
            # 绘制粒子
            t = turtle.Turtle()
            t.hideturtle()
            t.speed(0)
            t.penup()
            t.goto(p["x"], p["y"])
            t.dot(3, p["color"])
            screen.turtles().remove(t)
            
            # 粒子消失
            if p["life"] <= 0:
                self.particles.remove(p)
    
    def update(self):
        if self.state == "launch":
            self.launch()
        elif self.state == "explode" and not self.particles:
            return False  # 烟花结束
        elif self.state == "explode":
            self.explode()
        return True

# 烟花列表
fireworks = []

def animate():
    global fireworks
    screen.clearscreen()  # 清空画布
    screen.bgcolor("black")
    screen.tracer(0)
    
    # 随机生成新烟花
    if random.randint(1, 6) == 1:
        fireworks.append(Firework())
    
    # 更新所有烟花
    alive_fireworks = []
    for fw in fireworks:
        if fw.update():
            alive_fireworks.append(fw)
    fireworks = alive_fireworks
    
    screen.update()
    screen.ontimer(animate, 30)  # 30ms刷新一次

# 启动动画
animate()
turtle.done()