Python 如何实现烟花效果的完整代码

文章精选推荐

1 JetBrains Ai assistant 编程工具让你的工作效率翻倍

2 Extra Icons:JetBrains IDE的图标增强神器

3 IDEA插件推荐-SequenceDiagram,自动生成时序图

4 BashSupport Pro 这个ides插件主要是用来干嘛的 ?

5 IDEA必装的插件:Spring Boot Helper的使用与功能特点

6 Ai assistant ,又是一个写代码神器

7 Cursor 设备ID修改器,你的Cursor又可以继续试用了

 

文章正文

在 Python 中实现烟花特效可以使用 pygame 库来创建图形化界面和动画效果。以下是一个完整的烟花特效实现代码:


1. 安装 pygame

首先,确保你已经安装了 pygame 库。如果没有安装,可以使用以下命令安装:

bash 复制代码
pip install pygame

2. 实现烟花特效的完整代码

python 复制代码
import pygame
import random
import math

# 初始化 pygame
pygame.init()

# 设置屏幕大小
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("烟花特效")

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
COLORS = [
    (255, 0, 0),  # 红色
    (0, 255, 0),  # 绿色
    (0, 0, 255),  # 蓝色
    (255, 255, 0),  # 黄色
    (255, 0, 255),  # 紫色
    (0, 255, 255),  # 青色
]

# 烟花粒子类
class Particle:
    def __init__(self, x, y, color, speed, angle):
        self.x = x
        self.y = y
        self.color = color
        self.speed = speed
        self.angle = angle
        self.age = 0

    def move(self):
        # 根据角度和速度更新粒子的位置
        self.x += math.cos(self.angle) * self.speed
        self.y += math.sin(self.angle) * self.speed
        self.age += 1

    def draw(self):
        # 绘制粒子
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 2)

# 烟花类
class Firework:
    def __init__(self):
        self.x = random.randint(0, WIDTH)
        self.y = HEIGHT
        self.color = random.choice(COLORS)
        self.particles = []
        self.exploded = False

    def explode(self):
        # 爆炸生成多个粒子
        for _ in range(100):
            speed = random.uniform(1, 3)
            angle = random.uniform(0, 2 * math.pi)
            self.particles.append(Particle(self.x, self.y, self.color, speed, angle))
        self.exploded = True

    def move(self):
        if not self.exploded:
            # 烟花上升
            self.y -= 5
            if self.y <= random.randint(100, 300):
                self.explode()
        else:
            # 粒子移动
            for particle in self.particles:
                particle.move()

    def draw(self):
        if not self.exploded:
            # 绘制未爆炸的烟花
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 5)
        else:
            # 绘制爆炸后的粒子
            for particle in self.particles:
                particle.draw()

# 主循环
def main():
    clock = pygame.time.Clock()
    fireworks = []

    running = True
    while running:
        screen.fill(BLACK)  # 清屏

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

        # 添加新的烟花
        if random.random() < 0.05:  # 控制烟花生成频率
            fireworks.append(Firework())

        # 更新和绘制烟花
        for firework in fireworks[:]:
            firework.move()
            firework.draw()
            if firework.exploded and all(particle.age > 50 for particle in firework.particles):
                fireworks.remove(firework)

        pygame.display.flip()  # 更新屏幕
        clock.tick(30)  # 控制帧率

    pygame.quit()

if __name__ == "__main__":
    main()

3. 代码说明

  1. Particle 类:

    • 表示烟花爆炸后的单个粒子。
    • 每个粒子有自己的位置、颜色、速度和角度。
    • 通过 move 方法更新粒子的位置,通过 draw 方法绘制粒子。
  2. Firework 类:

    • 表示一个烟花。
    • 烟花在未爆炸时会向上移动,达到一定高度后爆炸,生成多个粒子。
    • 通过 explode 方法生成粒子,通过 move 和 draw 方法更新和绘制烟花。
  3. 主循环:

    • 不断生成新的烟花,并更新和绘制所有烟花。
    • 使用 pygame.event.get() 检测退出事件。
    • 通过 clock.tick(30) 控制帧率为 30 FPS。

4. 运行效果

运行代码后,你会看到一个黑色背景的窗口,烟花不断从底部升起并在空中爆炸,形成绚丽的烟花特效。


5. 进一步优化

  • 增加更多颜色 :可以在 COLORS 列表中添加更多颜色。
  • 调整烟花数量 :通过修改 random.random() < 0.05 中的阈值控制烟花生成频率。
  • 添加音效 :使用 pygame.mixer 播放烟花爆炸的音效。

希望这个烟花特效的实现对你有帮助!

相关推荐
随流寻影31 分钟前
利用 C++ 实现快速排序及三种优化
开发语言·c++
默 语1 小时前
【HarmonyOS7】ArkTS通用属性实战(六):组件级像素取整 pixelRound
开发语言
西西弗Sisyphus1 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - 指针与内存地址(2)
开发语言·c++
宿州派大星1 小时前
Python 音视频处理流水线:视频分离音频 + 讯飞转写 + 星火大模型解析(附完整代码与踩坑指南)
开发语言·python·音视频
I Am a robert girl2 小时前
DiffRAC:当视频“呼吸”有了节拍器——重复动作计数的新范式
python·深度学习·计算机视觉·视频分析·视频扩散模型·重复动作计数
烟烟Abandon2 小时前
FFmpeg开发基础之 C 语言编译器
c语言·开发语言·ffmpeg
郝学胜-神的一滴2 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
syf0611132 小时前
【Java】synchronized 关键字详解:从字节码到对象头与锁升级
java·开发语言
有个傻瓜2 小时前
容器里时间凭空差 8 小时:naive 与 aware 时区混用排错复盘与四种方案实测
python·docker·springboot·时区排错·naive/aware