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()

效果展示:

### Python代码实现烟花效果动画 对于创建一个庆祝烟花显示动画,可以利用`pygame`库来完成视觉上的呈现。下面是一段能够展示烟花爆炸并带有简单形图案的Python代码[^1]。 ```python import pygame import random import math from pygame.locals import * # 初始化Pygame pygame.init() # 设置屏幕尺寸 screen_width, screen_height = 800, 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Fireworks Display Celebrating Snake Year') class Particle(pygame.sprite.Sprite): def __init__(self, x, y, color=(255, 255, 255)): super().__init__() self.image = pygame.Surface([4, 4]) self.rect = self.image.get_rect() self.color = color self.rect.x = x self.rect.y = y self.life_time = random.randint(30, 70) angle = random.uniform(0, 2 * math.pi) speed = random.uniform(-5, 5) self.change_x = math.cos(angle) * speed self.change_y = math.sin(angle) * speed - 0.5 def update(self): self.life_time -= 1 if self.life_time <= 0 or \ not (0 < self.rect.x < screen_width and 0 < self.rect.y < screen_height): self.kill() self.rect.x += self.change_x self.rect.y += self.change_y self.change_y += 0.1 # Gravity effect on particles def draw_snake_pattern(): points = [] amplitude = 50 frequency = .05 phase_shift = 0 for i in range(int(screen_width / 2), int(screen_width*1.5), 5): y_offset = amplitude * math.sin(frequency*i + phase_shift) points.append([i, screen_height//2 + int(y_offset)]) if len(points)>1: pygame.draw.lines(screen,(255,255,0),False,points,5) particles_group = pygame.sprite.Group() running = True while running: for event in pygame.event.get(): if event.type == QUIT: running = False screen.fill((0, 0, 0)) if random.random() < 0.05: # 控制烟花发射频率 new_particle = Particle(random.randrange(screen_width), screen_height, (random.choice([(255, 0, 0),(0, 255, 0)]))) particles_group.add(new_particle) particles_group.update() particles_group.draw(screen) draw_snake_pattern() pygame.display.flip() pygame.time.Clock().tick(60) pygame.quit() ``` 这段程序通过粒子系统模拟了烟花的效果,并且在屏幕上绘制了一个简单的波浪线代表的形象以呼应主题[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ISDF-CodeInkVotex

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

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

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

打赏作者

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

抵扣说明:

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

余额充值