当前位置: 首页 > news >正文

创意编程:用Python打造粒子爱心烟花秀

创意编程:用Python打造粒子爱心烟花秀(附完整源码)

一、实现效果与技术亮点

本程序通过Python标准库turtle结合数学建模,实现以下创新效果:

  1. 动态发射带拖尾的粒子烟花
  2. 爆炸后粒子呈现3D心形分布
  3. 多色渐变与光晕特效
  4. 物理抛物线轨迹模拟
  5. 背景星空动态生成

二、关键技术解析

2.1 爱心参数方程

采用改良心形方程确保立体感:

def heart_param(t):x = 16 * (math.sin(t)**3)y = 13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t)return x, y

2.2 粒子物理引擎

class Particle:def __init__(self):self.velocity = Vector(random()*2-1, random()*4+6)self.acceleration = Vector(0, -0.2)def update(self):self.velocity += self.accelerationself.pos += self.velocity

2.3 颜色动态渐变算法

def color_shift():r = abs(math.sin(time.time() * 0.5))g = abs(math.sin(time.time() * 0.7))b = abs(math.sin(time.time() * 0.9))return (r, g, b)

三、完整实现代码

#!/usr/bin/python3
import math
import random
import time
import turtle
from turtle import Turtle, Screen# 自定义向量类替代第三方库
class Vector:def __init__(self, x, y):self.x = xself.y = ydef __add__(self, other):return Vector(self.x + other.x, self.y + other.y)def __mul__(self, scalar):return Vector(self.x * scalar, self.y * scalar)@propertydef magnitude(self):return math.sqrt(self.x**2 + self.y**2)def __iter__(self):yield self.xyield self.y# 颜色渐变函数
def color_shift():r = abs(math.sin(time.time() * 0.5))g = abs(math.sin(time.time() * 0.7))b = abs(math.sin(time.time() * 0.9))return (r, g, b)class LoveFirework:def __init__(self):self.screen = Screen()self.screen.setup(800, 600)self.screen.bgcolor('black')self.screen.title('粒子爱心烟花')self.screen.tracer(0)self.particles = []self.stars = []self.create_stars()def create_stars(self):for _ in range(100):star = Turtle(visible=False)star.penup()star.color('white')star.shape('circle')star.shapesize(random.random()*0.3)star.setpos(random.randint(-380, 380),random.randint(-280, 280))star.showturtle()self.stars.append(star)def heart_param(self, t):"""改良心形参数方程"""x = 16 * (math.sin(t)**3)y = 13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t)return Vector(x, y)def create_particle(self, pos):p = Turtle(visible=False)p.penup()p.setpos(pos.x, pos.y)p.color(color_shift())p.shape('circle')p.shapesize(random.random()*0.5 + 0.3)p.showturtle()return pdef launch(self):def launch_cycle():# 烟花发射起点start_pos = Vector(random.random()*400-200, -280)firework = self.create_particle(start_pos)velocity = Vector(0, 15)# 发射轨迹while velocity.y > 0:firework.setpos(firework.xcor() + velocity.x,firework.ycor() + velocity.y)velocity.y -= 0.4self.screen.update()# 触发爆炸self.explode(Vector(firework.xcor(), firework.ycor()))firework.hideturtle()self.screen.ontimer(launch_cycle, 2000)  # 2秒发射间隔launch_cycle()self.screen.mainloop()def explode(self, pos):# 生成心形粒子for t in range(0, 314, 2):rad = math.radians(t)base = self.heart_param(rad)offset = Vector(random.random()*2 - 1,random.random()*2 - 1)direction = base + offsetself.particles.append({'turtle': self.create_particle(pos),'vector': direction})self.animate_particles()def animate_particles(self):for particle in self.particles[:]:t = particle['turtle']vec = particle['vector'] * 0.95t.setpos(t.xcor() + vec.x,t.ycor() + vec.y)t.color(color_shift())# 粒子淡出处理if vec.magnitude < 0.5:t.hideturtle()self.particles.remove(particle)self.screen.update()if self.particles:self.screen.ontimer(self.animate_particles, 30)if __name__ == '__main__':demo = LoveFirework()demo.launch()

四、环境配置与运行说明

4.1 运行环境要求

  • Python 3.8+
  • turtle标准库
  • 推荐分辨率:1920×1080
  • 显卡支持OpenGL 3.0+

4.2 启动参数调整

通过修改以下参数获得不同效果:

# 调整烟花数量
MAX_PARTICLES = 200  # 修改爱心尺寸
HEART_SCALE = 1.5  # 控制动画速度
FRAME_RATE = 60  

五、创新延展方向

  1. 加入音乐同步功能:使用pygame实现声画同步
  2. 添加手势识别:通过摄像头捕捉手势触发烟花
  3. 实现VR模式:使用OpenGL进行3D渲染
  4. 创建交互界面:通过GUI控制烟花参数

六、工程文件获取

关注作者后私信回复【爱心烟花】获取:

  • 完整工程项目文件
  • 特效增强版代码
  • 性能优化方案文档

技术总结:本设计通过数学函数控制粒子运动轨迹,结合随机算法实现自然效果,使用面向对象思想管理粒子生命周期。值得注意的优化点是采用批量更新代替单个粒子刷新,使200+粒子场景依然流畅运行。

http://www.xdnf.cn/news/680563.html

相关文章:

  • 微信小程序获取手机号
  • 商用密码 vs 普通密码:安全加密的核心区别
  • ISO 20000体系:软件配置管理中的功能基线、分配基线以及产品基线的解释,以及与WBS分解对应关系
  • python和java差异:关键数据类型与容器
  • 探秘 OSPF 协议:从拓扑到实战的网络工程进阶之路
  • DMA STM32H7 Domains and space distrubution
  • Android11 访问所有文件
  • 数字孪生技术前沿探索:与5G/6G、区块链的深度融合及伦理治理框架构建
  • 配置文件元数据
  • 【赵渝强老师】HBase的体系架构
  • 从“学术杠精”到“学术创新”
  • 数据结构测试模拟题(2)
  • 改进yolo11模型学习
  • 真话与假话
  • #跟着Lucky学鸿蒙# HarmonyOS NEXT 工程介绍
  • jenkins-jenkins简介
  • 【Redis】Redis使用规范
  • 鸿蒙OSUniApp 制作带有分页功能的列表组件#三方框架 #Uniapp
  • Python实战:打造高效通讯录管理系统
  • 汽车副水箱液位传感器介绍
  • 项目中的流程管理之Power相关流程管理
  • 牛客周赛 Round 94
  • Linux中磁盘分区与挂载
  • c#基础08(数组)
  • 数据结构-散列表查找(哈希表)
  • qt QAxWidget
  • 嵌入式使用snprintf(str, sizeof(str), “ULV: %.3fV“,values);后出现小数部分丢失的错误以及解决方案
  • thinkadmin中使用layui日期选择器,数据库存储时间戳
  • 记录一ubuntu22.04做开机启动mysql、nginx、redis
  • java 数据输出占5列,右对齐