import tkinter as tk
import random
import math
import time

# 烟花粒子类
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        angle = random.uniform(0, 2 * math.pi)
        speed = random.uniform(2, 6)
        self.vx = math.cos(angle) * speed
        self.vy = math.sin(angle) * speed
        self.color = color
        self.life = random.randint(40, 70)

# 烟花主程序
class FireworksApp:
    def __init__(self, root):
        self.root = root
        self.root.title("🎆 动态烟花")
        self.root.geometry("800x600")
        self.root.configure(bg="#00001a")

        self.canvas = tk.Canvas(root, width=800, height=600, bg="#00001a", highlightthickness=0)
        self.canvas.pack()

        self.particles = []
        # 鼠标点击触发烟花
        self.canvas.bind("<Button-1>", self.launch_firework)

        self.animate()

    def launch_firework(self, event):
        # 随机颜色
        colors = ["#ff3333", "#ffcc00", "#33ff33", "#33ccff", "#ff33ff", "#ffffff", "#ff9900"]
        c = random.choice(colors)
        for _ in range(80):
            p = Particle(event.x, event.y, c)
            self.particles.append(p)

    def animate(self):
        self.canvas.delete("all")
        new_particles = []
        for p in self.particles:
            p.x += p.vx
            p.y += p.vy
            p.vy += 0.12  # 重力下落
            p.life -= 1
            if p.life > 0:
                size = 2 + p.life / 25
                self.canvas.create_oval(
                    p.x - size, p.y - size,
                    p.x + size, p.y + size,
                    fill=p.color, outline=p.color
                )
                new_particles.append(p)
        self.particles = new_particles
        self.root.after(20, self.animate)


if __name__ == "__main__":
    win = tk.Tk()
    app = FireworksApp(win)
    win.mainloop()
