Python 实现 2025 专属烟花效果粒子

引言

“爆竹声中一岁除”,听到这句话,想必大家都不陌生吧。在城市中看到那拖着彩星的烟花飞向天空,在空中绽放出来,那一刻是多么美好。那么,话说回来,你是否想过用代码的力量,在虚拟世界中重现这绚烂的烟花场景呢?借助 Python 强大的绘图和动画库,我们完全可以实现这一有趣的创意,为 2025 年增添一份独特的科技感与浪漫氛围。

准备工作

在开始编码之前,我们需要安装一些必要的 Python 库。这里我们会用到pygame库,它是一个专门用于开发游戏和多媒体应用的 Python 库,能帮助我们轻松实现图形绘制和动画效果。可以通过以下命令使用pip进行安装:

打开cmd:win+R打开运行框,输入cmd,然后回车。

在cmd输入pip install pygame,然后回车。(已下载pygame无需进行以上操作)

安装完成后,就可以开始我们的烟花效果创作之旅。

代码实现

初始化与设置

首先,我们需要导入pygame库,并对其进行初始化设置,包括窗口的大小、背景颜色等。

import pygame
import random

# 初始化pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('2025 Python烟花秀')

# 设置背景颜色
background_color = (0, 0, 0)

# 定义粒子类
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.size = 3
        self.life = 100

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.2
        self.life -= 1
        if self.life <= 0:
            return False
        return True

    def draw(self):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)

烟花绽放逻辑

接下来,我们要实现烟花绽放的核心逻辑,即创建多个粒子并模拟它们的运动轨迹(关键)。

# 烟花类
class Firework:
    def __init__(self):
        self.x = random.randint(0, width)
        self.y = height
        self.particles = []
        self.colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]
        self.exploded = False

    def explode(self):
        for _ in range(100):
            vx = random.uniform(-3, 3)
            vy = random.uniform(-7, -3)
            color = random.choice(self.colors)
            particle = Particle(self.x, self.y, vx, vy, color)
            self.particles.append(particle)
        self.exploded = True

    def update(self):
        if not self.exploded:
            self.y -= 1
            if self.y < 100:
                self.explode()
        else:
            new_particles = []
            for particle in self.particles:
                if particle.update():
                    new_particles.append(particle)
            self.particles = new_particles

    def draw(self):
        if not self.exploded:
            pygame.draw.circle(screen, (255, 255, 255), (int(self.x), int(self.y)), 5)
        else:
            for particle in self.particles:
                particle.draw()

主循环

最后,我们需要编写主循环,用于更新和渲染屏幕上的烟花效果。

fireworks = []

running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(background_color)

    if random.random() < 0.02:
        fireworks.append(Firework())

    for firework in fireworks:
        firework.update()
        firework.draw()

    fireworks = [firework for firework in fireworks if firework.particles or not firework.exploded]

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

 

代码解析

粒子类(Particle)

Particle类用于表示烟花中的单个粒子。每个粒子都有自己的位置(xy)、速度(vxvy)、颜色(color)、大小(size)和生命周期(life)。update方法负责更新粒子的位置和速度,同时模拟重力效果,并减少粒子的生命周期。draw方法用于在屏幕上绘制粒子。

烟花类(Firework)

Firework类用于管理整个烟花的行为。它包含多个粒子,并且在达到一定高度时会爆炸。explode方法会在烟花爆炸时创建多个粒子,并为每个粒子赋予随机的初始速度和颜色。update方法会根据烟花是否已经爆炸来更新烟花的状态和粒子的运动。draw方法则负责在屏幕上绘制烟花和粒子。

主循环

在主循环中,我们首先处理用户的退出事件。然后填充屏幕背景颜色,随机生成新的烟花,并更新和绘制所有的烟花。最后,通过pygame.display.flip()方法更新屏幕显示,并使用clock.tick(60)来控制帧率,使动画效果更加流畅。

效果展示

完整代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('2025 Python烟花秀')

# 设置背景颜色
background_color = (0, 0, 0)

# 定义粒子类
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.size = 3
        self.life = 100

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.2
        self.life -= 1
        if self.life <= 0:
            return False
        return True

    def draw(self):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)


# 烟花类
class Firework:
    def __init__(self):
        self.x = random.randint(0, width)
        self.y = height
        self.particles = []
        self.colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]
        self.exploded = False

    def explode(self):
        for _ in range(100):
            vx = random.uniform(-3, 3)
            vy = random.uniform(-7, -3)
            color = random.choice(self.colors)
            particle = Particle(self.x, self.y, vx, vy, color)
            self.particles.append(particle)
        self.exploded = True

    def update(self):
        if not self.exploded:
            self.y -= 1
            if self.y < 100:
                self.explode()
        else:
            new_particles = []
            for particle in self.particles:
                if particle.update():
                    new_particles.append(particle)
            self.particles = new_particles

    def draw(self):
        if not self.exploded:
            pygame.draw.circle(screen, (255, 255, 255), (int(self.x), int(self.y)), 5)
        else:
            for particle in self.particles:
                particle.draw()

fireworks = []

running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(background_color)

    if random.random() < 0.02:
        fireworks.append(Firework())

    for firework in fireworks:
        firework.update()
        firework.draw()

    fireworks = [firework for firework in fireworks if firework.particles or not firework.exploded]

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

运行上述代码,你将在屏幕上看到绚丽多彩的烟花不断绽放,每个烟花都由多个粒子组成,它们在重力的作用下自由下落,模拟出真实烟花绽放的效果。在 2025 年,用这段代码为自己和他人带来一份独特的视觉盛宴,感受科技与艺术融合的魅力。

因为版本限制,只能由图片展示,视频效果更好,大家可以去尝试一下。

 记得点赞关注,下期再见!

作者:Clevermea

物联沃分享整理
物联沃-IOTWORD物联网 » Python 实现 2025 专属烟花效果粒子

发表回复