最炫Python烟花代码全解析

 更新时间:2022年02月14日 15:53:40   作者:迢迢x  
2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试

导语:

除夕除夕,就是除去烦脑,迎接新的希望!在这里小编先祝大家除夕快乐,岁岁常欢笑,事事皆如意!

正文:

创建画布

setupdrawp5.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)

这里的widthheight表示的就是画布的宽和高

结果如下

让烟花粒子向上运动

只需要修改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,还有上面的HSBbackground4个参数就是对应rgba

效果如下

添加烟花颜色

主要给烟花添加色彩,可以随机数来添加随机颜色,主要在Firework添加一下

this.hu = random(255)

结尾:

最后祝你

岁岁常欢愉,年年皆胜意,除夕快乐~🎉🎉🎉

到此这篇关于 最炫Python烟花代码全解析的文章就介绍到这了,更多相关Python绚烂烟花内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python初学定义函数

    python初学定义函数

    这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助
    2021-11-11
  • python实现Zabbix-API监控

    python实现Zabbix-API监控

    这篇文章主要为大家详细介绍了python实现Zabbix-API监控,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • Python3中的指针你了解吗

    Python3中的指针你了解吗

    Python这个编程语言虽然没有指针类型,但是Python中的可变参量也可以像指针一样,改变一个数值之后,所有指向该数值的可变参量都会随之而改变,这篇文章主要介绍了Python3中的“指针”,需要的朋友可以参考下
    2024-02-02
  • sklearn中的交叉验证的实现(Cross-Validation)

    sklearn中的交叉验证的实现(Cross-Validation)

    这篇文章主要介绍了sklearn中的交叉验证的实现(Cross-Validation),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python调用C语言动态库的方法小结

    Python调用C语言动态库的方法小结

    这篇文章主要为大家详细介绍了Python调用C语言动态库的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2024-12-12
  • Python+Pygame实战之炫舞小游戏的实现

    Python+Pygame实战之炫舞小游戏的实现

    提到QQ炫舞,可能很多人想到的第一个词是“青春”。恍然间,这个承载了无数人回忆与时光的游戏品牌,已经走到了第十几个年头。今天小编就来给大家尝试做一款简单的简陋版的小游戏——《舞动青春*炫舞》,感兴趣的可以了解一下
    2022-12-12
  • Pygame 精准检测图像碰撞的问题

    Pygame 精准检测图像碰撞的问题

    这篇文章主要介绍了Pygame 精准检测图像碰撞,在用Pygame写游戏的时候,有人可能会遇到两个Rect对象碰撞但是对象之间还有空间间隔的问题,这里,将教大家用一种方法精准地检测图像碰撞,需要的朋友可以参考下
    2022-06-06
  • python config文件的读写操作示例

    python config文件的读写操作示例

    这篇文章主要介绍了python config文件的读写操作,结合简单示例形式分析了Python针对config文件的设置、读取、写入相关操作技巧,需要的朋友可以参考下
    2019-09-09
  • 有关Python的22个编程技巧

    有关Python的22个编程技巧

    本文给大家分享python的22个编程技巧,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-08-08
  • 使用Python打包程序并制作Windows安装程序的超完整指南

    使用Python打包程序并制作Windows安装程序的超完整指南

    这篇文章主要介绍了Python脚本打包为Windows可执行文件(.exe),并使用InnoSetup制作带有安装向导的安装程序,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-02-02

最新评论