pyhon 烟花特效 蛇年大吉

import tkinter as tk
import random
import math


class Firework:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.colors = ["#ff0000", "#ffff00", "#00ff00", "#00ffff", "#ff00ff"]
        self.particles = []
        self.create_firework(color)

    def create_firework(self, color):
        x = random.randint(100, 700)
        y = random.randint(100, 500)
        for _ in range(50):
            angle = random.uniform(0, 2 * math.pi)
            speed = random.uniform(1, 5)
            dx = math.cos(angle) * speed
            dy = math.sin(angle) * speed
            particle = self.canvas.create_oval(
                x, y, x + 5, y + 5,
                fill=random.choice(self.colors),
                outline=random.choice(self.colors)
            )
            self.particles.append((particle, dx, dy))

    def update(self):
        to_remove = []
        for i, (particle, dx, dy) in enumerate(self.particles):
            self.canvas.move(particle, dx, dy)
            x0, y0, x1, y1 = self.canvas.coords(particle)
            if x0 < 0 or x1 > 800 or y0 < 0 or y1 > 600:
                to_remove.append(i)

        for index in reversed(to_remove):
            particle, _, _ = self.particles.pop(index)
            self.canvas.delete(particle)


class App:
    def __init__(self, root):
        self.root = root
        self.root.title("蛇年大吉 - 烟花特效")
        self.canvas = tk.Canvas(root, width=800, height=600, bg='black')
        self.canvas.pack()

        # 添加祝福文字
        self.canvas.create_text(400, 300,
                                text="蛇年大吉",
                                font=("华文行楷", 72, "bold"),
                                fill="#FFD700",
                                tags="text")

        self.fireworks = []
        self.animate()

    def animate(self):
        if random.random() < 0.1:
            color = "#{:02x}{:02x}{:02x}".format(
                random.randint(0, 255),
                random.randint(0, 255),
                random.randint(0, 255)
            )
            self.fireworks.append(Firework(self.canvas, color))

        for firework in self.fireworks[:]:
            firework.update()
            if not firework.particles:
                self.fireworks.remove(firework)

        self.root.after(30, self.animate)


if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

效果展示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISDF-CodeInkVotex

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值