基于Python实现一个浪漫烟花秀

为了实现一个类似烟花秀的效果,我们可以通过复杂的粒子系统来模拟烟花的升起、绽放和下落效果。以下是一个示例,旨在创建更为动态和逼真的烟花秀效果。

示例代码

这个代码示例将使用 matplotlibnumpy,并实现更丰富的视觉效果:

python 复制代码
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

class Particle:
    def __init__(self, x, y, vx, vy, color):
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.color = color
        self.life = np.random.randint(50, 100)  # 生命值,控制粒子的存活时间

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy -= 0.05  # 重力影响
        self.life -= 1  # 每次更新生命值减少

    def is_alive(self):
        return self.life > 0

class Firework:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.particles = []
        self.exploded = False

    def explode(self):
        if not self.exploded:
            num_particles = np.random.randint(100, 200)
            angles = np.linspace(0, 2 * np.pi, num_particles)
            speeds = np.random.uniform(1, 4, num_particles)
            colors = plt.cm.hsv(np.random.rand(num_particles))  # 使用HSV颜色

            for angle, speed, color in zip(angles, speeds, colors):
                vx = speed * np.cos(angle)
                vy = speed * np.sin(angle)
                self.particles.append(Particle(self.x, self.y, vx, vy, color))
            self.exploded = True

    def update(self):
        if self.exploded:
            for particle in self.particles:
                particle.update()

    def get_particles(self):
        return [p for p in self.particles if p.is_alive()]

# 初始化画布
fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_facecolor('black')

fireworks = []

# 生成烟花
def generate_fireworks(num):
    for _ in range(num):
        x = np.random.uniform(1, 9)
        y = 0  # 从底部开始
        firework = Firework(x, y)
        fireworks.append(firework)

generate_fireworks(3)

# 动画更新函数
def update(frame):
    ax.clear()
    ax.set_xlim(0, 10)
    ax.set_ylim(0, 10)
    ax.set_facecolor('black')

    # 处理烟花升起
    for firework in fireworks:
        if firework.y < 8:  # 设置升起的高度
            firework.y += 0.1
        else:
            firework.explode()  # 当升至最高点,爆炸
            firework.update()  # 更新粒子

        # 绘制存活的粒子
        particles = firework.get_particles()
        for particle in particles:
            ax.scatter(particle.x, particle.y, color=particle.color, s=10)

# 创建动画
ani = animation.FuncAnimation(fig, update, frames=100, interval=50)
plt.show()

代码说明

  1. 粒子类

    • 每个粒子有随机的生命值、速度和颜色。
    • 更新方法考虑了重力的影响,使粒子逐渐下落。
  2. 烟花类

    • 生成大量粒子,每个粒子都有独特的颜色和速度,使效果更加多样化。
    • 当烟花达到一定高度后,会进行爆炸。
  3. 初始化和动画更新

    • 动画中,每个烟花从底部升起,并在达到最高点时爆炸,粒子下落,呈现出烟花绽放的效果。

运行代码

将以上代码复制到你的 Python 环境中运行,即可看到一个更为动态和逼真的烟花效果。你可以根据需要进一步调整粒子的数量、速度和颜色映射,以实现更符合您预期的效果。

相关推荐
敏编程3 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪3 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook3 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田16 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪20 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽20 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战20 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋1 天前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者2 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者2 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python