Skip to content

Instantly share code, notes, and snippets.

@Achie72
Created July 15, 2024 17:02
Show Gist options
  • Save Achie72/ab7d86dc57e7a6d51fd56d0dcb5f2041 to your computer and use it in GitHub Desktop.
Save Achie72/ab7d86dc57e7a6d51fd56d0dcb5f2041 to your computer and use it in GitHub Desktop.
PICO-8 Particles
-- inside init have a collection called particles
particles = {}
-- adder function, you call this anytime you want to add a new particle
-- to the system
function add_particle(_x, _y, _sx, _sy, _lifetime, _color, _type)
-- create particle
local part = {
x = _x,
y = _y,
sx = _sx, --speed for the particle if they move around
sy = _sy, -- same but y direction
lifetime = _lifetime, -- how long it should live
color = _color, -- color to draw
type = _type -- type of it
}
add(particles, part)
end
-- in update
-- update all particles
for particle in all(particles) do
particle.x += particle.sx -- move the x direction by the speed
particle.y += particle.sy -- move the y direction by the speed
particle.lifetime -= 1 -- decrement by 1 each frame
-- if lifetime is over delete particle
if particle.lifetime <= 0 then
del(particles, particle)
end
--particle.color = flr(rnd(15))+1 -- this gonna generate a color between 1 and 15
-- if particle.type == 1 then
-- particle type 1 do something else
-- end
-- here for example you can have a type 1 that is just explosion clouds
-- filled in circles
-- and type 2, which is a shockwave, which is growing faster
end
-- in draw:
-- draw the particles
for part in all(particles) do
--pset(part.x, part.y, part.color)
-- part.type = 1 would be a circfill like this
circfill(part.x, part.y, part.type, part.color)
-- type 2 would be a a white circfill that expands
-- how can you draw different things over time?
-- calculating with lifetime is the easiest,
-- for expanding stuff you want to increase the size
-- so for example let's have a 10/lifetime for example, where
-- tweaking 10 is where you can dial in the maximum size.
-- here you divide 10 with a smaller and smaller number due to how
-- lifetime is decreasing over time
-- circfill(part.x, part.y, 10/part.lifetime, part.colo)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment