最炫Python烟花代码全解析
作者:迢迢x 发布时间:2022-02-16 13:07:53
导语:
除夕除夕,就是除去烦脑,迎接新的希望!在这里小编先祝大家除夕快乐,岁岁常欢笑,事事皆如意!
正文:
创建画布
setup
和draw
是p5.js
的两个主函数,里头的createCanvas
用于创建画布的大小,background
来设置画布的背景颜色
function setup() {
createCanvas(1303 / 2, 734 / 2)
}
function draw() {
background(50);
}
画烟花粒子
考虑到会有很多,通过一个函数Particle
来生成,代码如下
var firework;
function Particle(x, y) {
this.pos = createVector(x, y)
this.vel = createVector(0, 0)
this.acc = createVector(0, 0)
this.update = function () {
this.vel.add(this.acc)
this.pos.add(this.vel)
this.acc.mult(0)
}
this.show = function () {
point(this.pos.x, this.pos.y)
}
}
#调用firework.update()和firework.show()将烟花粒子展示出来
function setup() {
createCanvas(1303 / 2, 734 / 2)
stroke(255)
strokeWeight(4)
firework = new Particle(200, 150)
}
function draw() {
background(50);
firework.update()
firework.show()
}
结果如下:
让烟花粒子随机出现在底部
修改setup
中的firework
,让它出现在底部的任意位置
firework = new Particle(random(width), height)
这里的width
和height
表示的就是画布的宽和高
结果如下
让烟花粒子向上运动
只需要修改Particle
中的this.vel
即可
this.vel = createVector(0, -4)
createVector
中第一个参数表示x轴的速率,正数为向右的速率,负为向左的速率;第二个参数表示y轴的速率,负为向上,正为向下
效果如下
让粒子用重力效果,可以下向运动
首先在全局声明一个变量gravity
,在setup函数中设置重力
gravity = createVector(0, 0.2)
firework.applyForce(gravity)
this.applyForce = function (force) {
this.acc.add(force)
}
效果如下
需要很多的烟花粒子
需要创建一个Firework
函数
function Firework() {
this.firework = new Particle(random(width), height)
this.update = function () {
this.firework.applyForce(gravity)
this.firework.update()
}
this.show = function () {
this.firework.show();
}
}
#然后再draw中,通过for循环来显示很多的烟花粒子
function draw() {
background(50)
fireworks.push(new Firework())
for (var i = 0; i < fireworks.length; i++) {
fireworks[i].update()
fireworks[i].show()
}
}
结果如下
让烟花粒子上到自身顶点时消失
function Firework() {
this.firework = new Particle(random(width), height)
this.update = function () {
if (this.firework) {
this.firework.applyForce(gravity)
this.firework.update()
if (this.firework.vel.y >= 0) {
this.firework = null
}
}
}
this.show = function () {
if (this.firework) {
this.firework.show();
}
}
}
效果如下
消失的那一刻,让周围爆破
这里修改的地方会比较多,主要修改的地方是Firework
:
function Firework() {
this.firework = new Particle(random(width), height, true)
this.exploded = false
this.particles = []
this.update = function () {
if (!this.exploded) {
this.firework.applyForce(gravity)
this.firework.update()
if (this.firework.vel.y >= 0) {
this.exploded = true
this.explode()
}
}
for (let i = 0; i < this.particles.length; i++) {
this.particles[i].applyForce(gravity)
this.particles[i].update()
}
}
this.explode = function () {
for (let i = 0; i < 100; i++) {
var p = new Particle(this.firework.pos.x, this.firework.pos.y)
this.particles.push(p)
}
}
this.show = function () {
if (!this.exploded) {
this.firework.show();
}
for (let i = 0; i < this.particles.length; i++) {
this.particles[i].show()
}
}
}
结果如下
随机倍数爆发
可以修改Particle
来完善以下上面的效果,修改后的代码为
function Particle(x, y, firework) {
this.pos = createVector(x, y)
this.firework = firework
if (this.firework) {
this.vel = createVector(0, random(-12, -8))
} else {
this.vel = p5.Vector.random2D()
this.vel.mult(random(1, 6))
}
this.acc = createVector(0, 0)
this.applyForce = function (force) {
this.acc.add(force)
}
this.update = function () {
this.vel.add(this.acc)
this.pos.add(this.vel)
this.acc.mult(0)
}
this.show = function () {
point(this.pos.x, this.pos.y)
}
}
效果如下:
展示烟花少一些
通过调整几率来实现,让展示烟花少一些
我们将draw
函数中的
if(random(1)<0.1){
fireworks.push(new Firework())
}
修改成:
if(random(1)<0.02){
fireworks.push(new Firework())
}
这样就少一些了
然后我们又发现,烟花太散落了,修改烟花太散落的问题
到Particle
中,找到update
方法,里头添加
if(!this.firework){
this.vel.mult(0.85)
}
可以理解为,mult
的值越大作用力就越大 * 就越散
淡出效果实现
散开之后,需要慢慢淡出消失,
其实主要引入一个变量lifespan
,让它从255
开始递减,通过stroke(255,this.lifespan)
来实现淡出
如下代码
function Particle(x, y, firework) {
this.pos = createVector(x, y)
this.firework = firework
this.lifespan = 255
if (this.firework) {
this.vel = createVector(0, random(-12, -8))
} else {
this.vel = p5.Vector.random2D()
this.vel.mult(random(1, 6))
}
this.acc = createVector(0, 0)
this.applyForce = function (force) {
this.acc.add(force)
}
this.update = function () {
if(!this.firework){
this.vel.mult(0.85)
this.lifespan -= 4
}
this.vel.add(this.acc)
this.pos.add(this.vel)
this.acc.mult(0)
}
this.show = function () {
if (!this.firework) {
strokeWeight(2)
stroke(255,this.lifespan)
} else {
strokeWeight(4)
stroke(255)
}
point(this.pos.x, this.pos.y)
}
}
效果如下
修改背景颜色
在setup
中通过background
函数将背景色修改成黑色
background(0)
同时在draw
添加
colorMode(RGB)
background(0, 0, 0, 25)
colorMode
用于设置颜色模型,除了RGB
,还有上面的HSB
;background
的4
个参数就是对应rgba
效果如下
添加烟花颜色
主要给烟花添加色彩,可以随机数来添加随机颜色,主要在Firework
添加一下
this.hu = random(255)
结尾:
最后祝你
岁岁常欢愉,年年皆胜意,除夕快乐~🎉🎉🎉
来源:https://blog.csdn.net/a55656aq/article/details/122455251


猜你喜欢
- 1 回顾上一节我们详细讲解了如何对数据库进行分区操作,包括了 垂直拆分(Scale Up 纵向扩展)和水平拆分(Scale Out 横向扩展
- 写在前面周日下午在家学习,看到一个关于切片的问题,在网上找了一些资料,做个总结。上代码func main() {sl := make([]i
- 大家好,我是丁小杰。上次和大家分享了Python定时爬取微博热搜示例介绍,堪称摸鱼神器,一个热榜不够看?今天我们再来爬取一下抖音热搜榜,感兴
- # 从X和Y中取出相应步长对应的数组并保存至x_data和y_data中x_data = []y_data = []for i in ran
- 本文讨论 MySQL 的备份和恢复机制,以及如何维护数据表,包括最主要的两种表类型:MyISAM 和 Innodb,文中设计的 MySQL
- 在Python中,生成器和函数很像,都是在运行的过程中才会去确定各种变量的值,所以在很多情况下,会导致各种各样的问题。def generat
- Internet Explorer 8 Beta 测试了一年多之后,今天,IE8 终于发布了。它绝对好过 IE7,还有一些不错的新功能,如
- 源起我本想删写一小段代码用于删除一串字符串中的连续重复的指定字符,可能也是长时间不写代码,而且有的时候写代码只途快,很多基础知识都忘光了。我
- 1、File > Setting > Project:xxx > Project Interpreter 选择或添加环境2
- 如下所示:import ( "golang.org/x/net/html")在使用此包时,会导入失败,因为这
- 一、eval()函数是什么?Python的一个内置函数;返回传入字符串的表达式结果(官方)二、eval()函数语法解析三、eval()函数应
- ff默认不让改 statusopera9 测试通过ie6 测试通过这东西是给统计部门用的,分析用户习惯以改良网站布局
- 前言innodb_data_file_path用来指定innodb tablespace文件,如果我们不在My.cnf文件中指定innodb
- 在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接。在这种情况下, 使用EXISTS(或NOT EX
- 本文通过图文并茂的方式给大家展示SQL SERVER数据库表记录只保留N天图文教程,具体方法步骤请看下文:第一步:首先设置SQL Serve
- 多线程多线程类似于同时执行多个不同程序,多线程运行有如下优点:使用线程可以把占据长时间的程序中的任务放到后台去处理。用户界面可以更加吸引人,
- 首先备份数据库,以防不必要的损失。而后对所有被挂马的小于8000字符的varchar字段执行 update 表名 set 字段名=repla
- 前言最近在爬行 nosec.org 的数据,看了下需要模拟登录拿到cookie后才能访问想抓的数据,重要的是 nosec.org 的登录页面
- String str = "n1e你v00a?Az$Z000?#99?9900眯2_悄s3你y4@好?!6求救你d75a8t&qu
- 一、 简单查询简单的Transact-SQL查询只包括选择列表、FROM子句和Where子句。它们分别说明所查询列、查询的表或视图、以及搜索