Conway's Game of Life with colors
Lua Carousel » Devlog
When I first started messing with LÖVE, one of the first examples I gravitated towards was this Game of Life with colors. I just went back to it and ported it to Lua Carousel. Very fun. In the process I spent some time understanding its rules for assigning colors:
- Only live cells have a color.
- Initially live cells get random colors.
- Cells with 2 live neighbors continue to live and preserve their colors.
- Cells with 3 live neighbors are 'reborn' with the color of one of their 'parents'.
- Cells that die (fewer than 2 or greater than 3 live neighbors) stop spreading their color.
The link above also adds some randomness when cells are born, but I took it out to better see the interactions between "lineages" of cells.
math.randomseed(os.time()) N = 6 W, H = Safe_width/N, Safe_height/N curr, new = {}, {} for x=1,W do table.insert(curr, {}) table.insert(new, {}) for y=1,H do table.insert(curr[x], {v=0, c={0,0,0}}) table.insert(new[x], {v=0, c={0,0,0}}) end end -- R pentomino function set(x,y) curr[x][y].v = 1 curr[x][y].c = {rand()/2+0.25, rand()/2+0.25, rand()/2+0.25} end cx = math.floor(W/2) cy = math.floor(H/2) set(cx, cy) set(cx, cy+1) set(cx, cy+2) set(cx-1, cy) set(cx+1, cy+1) function car.draw() for x=1,W do for y=1,H do if curr[x][y].v > 0 then color(unpack(curr[x][y].c)) rect('fill', x*N, y*N, N-1,N-1) end end end end function car.update(dt) step() end function step() for x=2,W-1 do for y=2,H-1 do local n = nghs(x,y) if n < 2 or n > 3 then new[x][y].v = 0 elseif n == 2 then new[x][y].v = curr[x][y].v local c, oldc = new[x][y].c, curr[x][y].c c[1], c[2], c[3] = oldc[1], oldc[2], oldc[3] elseif n == 3 then new[x][y].v = 1 -- copy some random live neighbor's color over local oldc = random_color(x,y) local c = new[x][y].c c[1], c[2], c[3] = oldc[1], oldc[2], oldc[3] end end end curr, new = new, curr end function nghs(x,y) return curr[x-1][y-1].v + curr[x-1][y].v + curr[x-1][y+1].v + curr[x][y-1].v + curr[x][y+1].v + curr[x+1][y-1].v + curr[x+1][y].v + curr[x+1][y+1].v end function random_color(x,y) local rgbs = {} maybe_append_color(x-1, y-1, rgbs) maybe_append_color(x-1, y, rgbs) maybe_append_color(x-1, y+1, rgbs) maybe_append_color(x, y-1, rgbs) maybe_append_color(x, y+1, rgbs) maybe_append_color(x+1, y-1, rgbs) maybe_append_color(x+1, y, rgbs) maybe_append_color(x+1, y+1, rgbs) return rgbs[rand(#rgbs)] end function maybe_append_color(x,y, out) if curr[x][y].v > 0 then table.insert(out, curr[x][y].c) end end
If you try pasting this program into Lua Carousel, remember to first run the abbreviations on one of the example screens. Or if you've deleted that screen, here are the abbreviations this program uses:
g = love.graphics rect = g.rectangle color = g.setColor rand = math.random
Get Lua Carousel
Lua Carousel
Write programs on desktop and mobile
Status | In development |
Category | Tool |
Author | Kartik Agaram |
Tags | LÖVE |
More posts
- New version after 3 days3 days ago
- New version after 40 days6 days ago
- Turn your phone or tablet into a chess clock20 days ago
- Guest program: bouncing balls36 days ago
- New version after 3 months, and turtle graphics47 days ago
- Interactively zooming in to the Mandelbrot set on a touchscreen66 days ago
- A little timer app67 days ago
- New version after 4 monthsJun 28, 2024
- Visualizing the digits of πMay 04, 2024
- A little recursion problemApr 15, 2024
Leave a comment
Log in with itch.io to leave a comment.