In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Python code to achieve the most dazzling fireworks". In daily operation, I believe many people have doubts about how to use Python code to achieve the most dazzling fireworks. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the question of "how to use Python code to achieve the most dazzling fireworks". Next, please follow the editor to study!
Effect picture:
Body: create a canvas
Setup and draw are the two main functions of p5.js. CreateCanvas is used to create the size of the canvas, and background is used to set the background color of the canvas.
Function setup () {createCanvas (1303 / 2734 / 2)} function draw () {background (50);} draw fireworks particles
Considering that there will be many, it is generated by a function Particle, with the following code
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)}} # call firework.update () and firework.show () to display fireworks particles function setup () {createCanvas (1303 / 2734 / 2) stroke (255) strokeWeight (4) firework = new Particle (200150)} function draw () {background (50) Firework.update () firework.show ()}
The results are as follows:
Let the fireworks particles appear randomly at the bottom.
Modify the firework in setup so that it appears anywhere at the bottom
Firework = new Particle (random (width), height)
The width and height here represent the width and height of the canvas.
The results are as follows
Let the fireworks particles move upward
You only need to modify the this.vel in Particle
This.vel = createVector (0,-4)
In createVector, the first parameter represents the rate of the x-axis, the positive number is the rate to the right and the negative is the rate to the left, and the second parameter represents the rate of the y-axis, which is negative upward and positive downward.
The effect is as follows
Let the particles use the gravity effect to move downward.
First declare a variable gravity globally and set gravity in the setup function
Gravity = createVector (0,0.2) firework.applyForce (gravity) this.applyForce = function (force) {this.acc.add (force)}
The effect is as follows
Need a lot of fireworks particles.
You need to create a Firework function
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 () Then in draw, many fireworks particles function draw () {background (50) fireworks.push (new Firework ()) for (var I = 0; I) are displayed through the for loop.
< 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 ();}
The effect is as follows
The moment you disappear, let the surroundings explode
There will be a lot of changes here, and the main modification is 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; partitions +) {this.partials [I] .applyForce (gravity) this.particles.update ()} this.explode = function () {for (let I = 0; I < 100) If +) {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) ITunes +) {this.particles.show ()}}
The results are as follows
Random multiple burst
You can modify Particle to improve the above effect, and the modified code is
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)}}
The effect is as follows:
Display fewer fireworks
By adjusting the probability, there are fewer fireworks on display.
We set the draw function in the
If (random (1)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.