Snippet Decrypt #1: Shiny Ripples Cellular Automaton
This is a freebie for the new Patreon-exclusive series Snippet Decrypts where I take snippets from my Pico-8 Doodles and write some text to explain what's going on!


  • for i=0,699 do
  •    local x,y=rnd(128),rnd(128)
  •    local c=pget(x,y)
  •    if c==12 then
  •       circ(x,y,1,12)
  •    
  •       if rnd(200)<1 then
  •          line(x-1,y,x+1,y,7)
  •       end
  •    else
  •       pset(x-1,y,7)
  •       pset(x+1,y,7)
  •    end
  • end


Hi patrons!

Today we're looking at some cellular automata! The snippet above should be placed at the beginning of the _draw() function and will only work if the background is already blue (color 12) or at least partly blue. When these conditions are met, the result is a soothing ripply water scene!


Rather than applying the ruleset to every pixel on the screen each frame, we're only applying it to a number of random pixels each frame. (700 here) That's why the 'for' loop. This way, it's not too heavy on the CPU and the effect is spread out randomly on the screen which is good!

Inside the 'for' loop, we start by picking a random pixel on the 128x128 screen and we inspect its current color. The ruleset spreads out in two branches from here.

If the random pixel is blue (c==12), we'll be coloring its immediate neighbors in blue with 'circ(x,y,1,12)'. But also, there is a small chance for a ripple to be created, by simply drawing a short horizontal white line with 'line(x-1,y,x+1,y,7)'.

And if the pixel is not blue, it's either a ripple or an object from outside this snippet. Either way, we'll treat as a ripple. Only one output there, the pixels immediately to the left and to the right of our random pixel are being colored in white, spreading the ripple horizontally.


As long as there's more blue than white, the blue will clear out the white by spreading everywhere, while the white can only spread horizontally. The ripples randomly appear, spread and disappear, all through the ruleset. This ruleset is particularly favorable to the blue cells because we want the effect to be sparse, to achieve a calm effect.


And that's it for today's snippet! If you have questions, ask them in the comments!

If you want to learn more about cellular automata, you can read the Doodle Insights #8 which are all about it!

Thank you for reading!

TRASEVOL_DOG

Tier Benefits
Recent Posts