We use the code from last tutorial.
Let’s extend our particle system with a new function:
explode=function(pcount)
local cols={rnd(15),rnd(15)}
local rndx=rnd(127)
local rndy=rnd(127)
local i=1
while pcount>0 and i<maxparticles do
local rndxs=rnd(10)-5
local rndys=rnd(20)-10
if _pool[i].launch(rndx,rndy,rndxs,rndys,cols) then
pcount-=1
end
if (pcount==0) break
i+=1
end
end
In the above code we start by setting a color array with two values between 0-15 (max 16 colors in PICO-8 (standard colors)).
Then we set a random x-coordinate within the screen bounds and a random y-coordinate inside the screen bounds.
We then loop through the particles to get particles that can be launched (remember, if a particle has been launched it can’t be launched until it has left the screen on the y-axis).
If we find a particle we subtract one from pcount (particle count).
If there’s no more particles to launch we stop the function otherwise we add 1 to i and the loop continues.
If i has reached maxparticles then there are no particles available.
Now we can go to our _update function and add the following line:
if (frames%15==0) particlesys.explode(50)
Okay, we haven’t introduced frames yet. We can count time in PICO-8 in multiple ways. We have (if we use the standard _update() ) 30 frames per second, there’s also an _update60 that does 60 frames per second. For now we use _update but you should be able to transfer the code pretty easy to _update60. The above code basically let’s 15 frames pass (½ a second) and then it calls particlesys.explode().
Let’s add frames as a global variable inside _init():
frames=0
And in _draw we add frames+=1 on the last line of the function.
Now we need to go to our launch function of the particle.
Declare a variable called _col in the lines before the return statement.
The launch function needs to take in a new argument called cols which is the possible colors we want for the particle.
Inside the launch function we set the _col variable to the following:
_col=cols[flr(rnd(2))+1]
Inside the draw function of the particle we add the following to the line call:
line(_x,_y,_x+_xspeed,_y+_yspeed,_col)
Type run in the console and see the sparks in action!