Python新春烟花绽放之旅

fireworks

系列文章

序号 直达链接
Tkinter
1 Python李峋同款可写字版跳动的爱心
2 Python跳动的双爱心
3 Python蓝色跳动的爱心
4 Python动漫烟花
5 Python粒子烟花
Turtle
1 Python满屏飘字
2 Python蓝色流星雨
3 Python金色流星雨
4 Python漂浮爱心
5 Python爱心光波①
6 Python爱心光波②
7 Python满天繁星
8 Python五彩气球
9 Python白色飘雪
10 Python七彩花朵
11 Python 3D星空
12 Python大雪纷飞
13 Python一闪一闪亮星星
14 Python爱心泡泡
15 Python爱心射线
16 Python圣诞礼物
17 Python礼物圣诞树
18 Python浪漫星空
19 Python飞舞蝙蝠
20 Python万圣礼物
21 Python蓝色飘雪
Pygame
1 Python跨年烟花
2 Python炫酷烟花
3 Python黑客帝国字母雨
敬请期待……

写在前面

Python实现炫酷新春烟花动画的完整代码。

技术需求

  1. Pygame模块

  2. Pygame是一个专为Python设计的游戏开发工具包,具备图形渲染、输入响应、声音管理等功能。本程序利用Pygame构建显示界面、响应用户操作、呈现视觉元素(包括粒子效果、烟花动画及文本内容)。
  3. 粒子效果模拟

  4. 粒子模拟技术常用于再现自然现象,如爆破、烟尘等视觉特效。当前实现中,该技术被用于展现烟花绽放时的动态场景。每个粒子单元包含坐标信息、移动矢量、存在时长等参数,并遵循重力规则,真实还原烟花爆裂后的扩散过程。
  5. 重力模型实现

  6. update()函数中通过调整垂直速度参数vy来体现重力作用。粒子持续获得向下的加速度,精确复现真实世界中爆炸残骸的运动规律。
  7. 随机化处理

  8. 随机参数生成机制决定了烟花的色彩表现、发射方向、运动速率及持续时间等特性。程序为每个新建烟花实例分配随机的视觉参数,确保每次展示效果都具有独特性。
  9. 可视化呈现

  10. 采用pygame.draw.circle()方法渲染粒子及文字内容,动态展示烟花效果和新年祝福语。系统字体通过pygame.font.SysFont()调取并用于文本绘制。
  11. 交互响应

  12. 使用pygame.event.get()监听并处理用户输入事件,如窗口关闭指令,确保程序具备基本的交互能力。
  13. 时序管理

  14. 通过帧率调控clock.tick(FPS))保障动画播放流畅度,配合time.sleep(0.05)适当延缓帧切换速度,使烟花特效获得更佳的观赏效果。
  15. 对象化设计

  16. 采用面向对象架构,将粒子和烟花的相关属性和方法分别封装在ParticleFirework类中,使代码组织更具逻辑性,便于后续优化和功能拓展。

完整代码

import pygame
import random
import math
import time

# Initialize Pygame
pygame.init()

# Constants
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 700
BACKGROUND_COLOR = (0, 0, 0)  # Black
FPS = 60

# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('新春烟花')

# Font setup using system default font
font = pygame.font.SysFont("Comic Sans MS", 100)  # You can replace "Arial" with any system font
text_color = (255, 255, 255)  # White color for the text

# Particle class
class Particle:
    def __init__(self, x, y, color, angle, speed, life):
        self.x = x
        self.y = y
        self.color = color
        self.angle = angle
        self.speed = speed
        self.life = life
        self.radius = 2
        self.vx = math.cos(angle) * speed
        self.vy = math.sin(angle) * speed

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.life -= 1
        self.vy += 0.05  # Gravity effect

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


# Firework class
class Firework:
    def __init__(self, x, y, color, num_particles=100, spread=2 * math.pi):
        self.x = x
        self.y = y
        self.color = color
        self.num_particles = num_particles
        self.spread = spread
        self.particles = []
        self.exploded = False

    def explode(self):
        if not self.exploded:
            for _ in range(self.num_particles):
                angle = random.uniform(0, self.spread)
                speed = random.uniform(1, 3)
                life = random.randint(30, 50)
                particle = Particle(self.x, self.y, self.color, angle, speed, life)
                self.particles.append(particle)
            self.exploded = True
……

代码分析

这段Python代码利用Pygame库实现了动态的新春烟花效果,包含烟花发射、爆炸粒子动画和"Happy New Year!"祝福文字的展示。以下是代码的主要组成部分:

程序架构

  1. 初始化设置

  2. Pygame环境初始化
  3. 显示参数配置(分辨率、背景色、帧率)
  4. 窗口创建与标题设置
  5. 核心类定义

  6. Particle类:管理粒子属性(坐标、颜色、速度、生命周期)及运动逻辑
  7. Firework类:控制烟花爆炸效果与粒子生成
  8. 主程序循环

  9. 事件处理
  10. 随机烟花生成
  11. 动态效果更新
  12. 文字渲染显示

关键技术实现

# 显示配置
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 700
BACKGROUND_COLOR = (0, 0, 0)
FPS = 60

# 窗口设置
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('新春烟花')

# 文字渲染
font = pygame.font.SysFont("Comic Sans MS", 100)
text_color = (255, 255, 255)
class Particle:
    def __init__(self, x, y, color, angle, speed, life):
        # 初始化粒子属性
        self.vx = math.cos(angle) * speed
        self.vy = math.sin(angle) * speed
        
    def update(self):
        # 更新粒子位置和生命周期
        self.vy += 0.05  # 重力模拟
        
    def draw(self):
        # 粒子绘制方法
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
class Firework:
    def __init__(self, x, y, color, num_particles=100):
        # 初始化烟花参数
        
    def explode(self):
        # 生成随机粒子
        angle = random.uniform(0, 2*math.pi)
        speed = random.uniform(1, 3)
        
    def update(self):
        # 更新所有粒子状态
        
    def draw(self):
        # 绘制有效粒子

主程序流程

  1. 处理退出事件
  2. 随机触发新烟花
  3. 更新所有烟花状态
  4. 渲染背景和祝福文字
  5. 控制帧率刷新

该实现通过粒子系统模拟烟花效果,结合随机参数生成多样化的爆炸动画,配合节日文字营造新春氛围。

写在后面

我是一只有趣的兔子,感谢你的喜欢!

作者:Want595

物联沃分享整理
物联沃-IOTWORD物联网 » Python新春烟花绽放之旅

发表回复