import tkinter as tk
import random
import math
import time

# 窗口配置
WIDTH = 800
HEIGHT = 600

class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.vx = random.uniform(-4, 4)
        self.vy = random.uniform(-4, 4)
        self.color = color
        self.life = random.randint(30, 60)
        self.size = random.randint(2, 4)

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.08  # 重力
        self.life -= 1

class Firework:
    def __init__(self, canvas, target_x, target_y):
        self.canvas = canvas
        self.x = random.randint(50, WIDTH - 50)
        self.y = HEIGHT
        self.target_x = target_x
        self.target_y = target_y
        self.speed = 8
        self.exploded = False
        self.particles = []
        self.color = random.choice(["#ff3333", "#ffdd33", "#33ff33", "#33ddff", "#dd33ff", "#ff9933"])

    def update(self):
        if not self.exploded:
            # 上升阶段
            dx = self.target_x - self.x
            dy = self.target_y - self.y
            dist = math.hypot(dx, dy)
            if dist < self.speed:
                self.explode()
            else:
                self.x += dx / dist * self.speed
                self.y += dy / dist * self.speed
        else:
            # 粒子更新
            for p in self.particles:
                p.update()
            self.particles = [p for p in self.particles if p.life > 0]

    def explode(self):
        self.exploded = True
        for _ in range(80):
            self.particles.append(Particle(self.x, self.y, self.color))

    def draw(self):
        if not self.exploded:
            self.canvas.create_oval(
                self.x - 3, self.y - 3,
                self.x + 3, self.y + 3,
                fill="#ffffff", outline=self.color
            )
        else:
            for p in self.particles:
                self.canvas.create_oval(
                    p.x - p.size, p.y - p.size,
                    p.x + p.size, p.y + p.size,
                    fill=p.color
                )

class FireworkApp:
    def __init__(self, root):
        self.root = root
        self.root.title("动态烟花")
        self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="#000011")
        self.canvas.pack()
        self.canvas.bind("<Button-1>", self.on_click)
        self.fireworks = []
        self.loop()

    def on_click(self, event):
        # 点击位置生成烟花
        self.fireworks.append(Firework(self.canvas, event.x, event.y))

    def loop(self):
        self.canvas.delete("all")
        # 随机自动发射烟花
        if random.random() < 0.03:
            rx = random.randint(100, WIDTH - 100)
            ry = random.randint(80, HEIGHT // 2)
            self.fireworks.append(Firework(self.canvas, rx, ry))

        for fw in self.fireworks:
            fw.update()
            fw.draw()
        # 清理结束的烟花
        self.fireworks = [fw for fw in self.fireworks if not (fw.exploded and len(fw.particles) == 0)]
        self.root.after(20, self.loop)


if __name__ == "__main__":
    window = tk.Tk()
    app = FireworkApp(window)
    window.mainloop()