SoFunction
Updated on 2024-11-20

The most dazzling Python fireworks code full analysis

Introduction:

New Year's Eve is to get rid of the annoying brain and welcome the new hope! Here I first wish you all a happy New Year's Eve, years and years often laugh, everything is as you wish!

Main article:

Creating a Canvas

setupcap (a poem)drawbeThe two main functions in thecreateCanvasfor creating the size of the canvas.backgroundto set the background color of the canvas

function setup() {
    createCanvas(1303 / 2, 734 / 2)
}
function draw() {
    background(50);
}

Drawing firework particles

Consider that there will be a lot of them, through a functionParticleto generate, the code is as follows

var firework;
function Particle(x, y) {
     = createVector(x, y)
     = createVector(0, 0)
     = createVector(0, 0)
     = function () {
        ()
        ()
        (0)
    }
     = function () {
        point(, )
    }
}
# Call () and () to display the fireworks particles out of the way
function setup() {
    createCanvas(1303 / 2, 734 / 2)
    stroke(255)
    strokeWeight(4)
    firework = new Particle(200, 150)
}
function draw() {
    background(50);
    ()
    ()
}

The results are as follows:

Make firework particles appear randomly at the bottom

modificationssetuphit the nail on the headfireworkThe first thing you need to do is to make it appear anywhere at the bottom of the page.

firework = new Particle(random(width), height)

Here.widthcap (a poem)heightThe width and height of the canvas

The results are as follows

Make the firework particles move upward

Simply modify theParticlehit the nail on the headcan immediately (do sth)

 = createVector(0, -4)

createVectorin which the first parameter represents the rate of the x-axis, with positive being the rate to the right and negative being the rate to the left; the second parameter represents the rate of the y-axis, with negative being the rate up and positive being the rate down

The effect is as follows

Let the particles use the gravity effect, which allows downward motion

First declare a variable globallygravity, setup gravity in the setup function

gravity = createVector(0, 0.2)
(gravity)
 = function (force) {
    (force)
}

The effect is as follows

Need a lot of firework particles

It is necessary to create aFireworkfunction (math.)

function Firework() {
     = new Particle(random(width), height)
     = function () {
        (gravity)
        ()
    }
     = function () {
        ();
    }
}
# And then in the draw, a for loop to show a lot of firework particles
function draw() {
    background(50)
    (new Firework())
    for (var i = 0; i < ; i++) {
        fireworks[i].update()
        fireworks[i].show()
    }
}

The results are as follows

Make the firework particles disappear when they reach their own apex.

function Firework() {
     = new Particle(random(width), height)
     = function () {
        if () {
            (gravity)
            ()
            if ( >= 0) {
                 = null
            }
        }
    }
     = function () {
        if () {
            ();
        }
    }
}

The effect is as follows

The moment it disappeared, it made the surrounding blasting

There will be more modifications here, the main ones areFirework:

function Firework() {
     = new Particle(random(width), height, true)
     = false
     = []
     = function () {
        if (!) {
            (gravity)
            ()
            if ( >= 0) {
                 = true
                ()
            }
        }
        for (let i = 0; i < ; i++) {
            [i].applyForce(gravity)
            [i].update()
        }
 
    }
     = function () {
        for (let i = 0; i < 100; i++) {
            var p = new Particle(, )
            (p)
        }
    }
     = function () {
        if (!) {
            ();
        }
        for (let i = 0; i < ; i++) {
            [i].show()
        }
    }
}

The results are as follows

Stochastic multiplier bursts

Can be modifiedParticleto refine the following effect above, the modified code is

function Particle(x, y, firework) {
     = createVector(x, y)
     = firework
    if () {
         = createVector(0, random(-12, -8))
    } else {
         = .random2D()
        (random(1, 6))
    }
     = createVector(0, 0)
     = function (force) {
        (force)
    }
     = function () {
        ()
        ()
        (0)
    }
     = function () {
        point(, )
    }
}

The effect is as follows:

Show less fireworks

This is accomplished by adjusting the odds to allow for fewer display fireworks

we willdrawfunction in the

if(random(1)<0.1){
    (new Firework())
}    

Modified to:

if(random(1)<0.02){
    (new Firework())
}    

That's less.

And then we realized that the fireworks were too scattered, modify the fireworks to be too scattered

until (a time)ParticleIn theupdatemethod, add the

if(!){
    (0.85)
}

It can be understood thatmultThe greater the value of the force, the greater the dispersion of the explosion.

Fade-out effect implementation

After dispersing, it needs to slowly fade out and disappear.

In fact, the main introduction of a variablelifespanLet's get it out of the255Starts decreasing through thestroke(255,)to realize the fade-out

following code

function Particle(x, y, firework) {
     = createVector(x, y)
     = firework
     = 255
    if () {
         = createVector(0, random(-12, -8))
    } else {
         = .random2D()
        (random(1, 6))
    }
     = createVector(0, 0)
     = function (force) {
        (force)
    }
     = function () {
        if(!){
            (0.85)
             -= 4
        }
        ()
        ()
        (0)
    }
     = function () {
        if (!) {
            strokeWeight(2)
            stroke(255,)
        } else {
            strokeWeight(4)
            stroke(255)
        }
        point(, )
    }
}

The effect is as follows

Modify the background color

existsetuphit the nail on the headbackgroundfunction to change the background color to black

background(0)

at the same timedrawincrease

colorMode(RGB)
background(0, 0, 0, 25)

colorModeUsed to set the color model, in addition to theRGBAnd above that.HSBοΌ›background(used form a nominal expression)4parameter is the parameter that corresponds to thergba

The effect is as follows

Add firework colors

Mainly add color to fireworks, you can add random color by random number, mainly in theFireworkAdd it.

 = random(255)

Ending:

Finally, good luck.

Happy New Year's Eve~πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰!

To this article on the most dazzling Python fireworks code full analysis of the article is introduced to this, more related Python brilliant fireworks content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!