I’m here to share a bit of my code, spent the last hour or so writing it and here it is, it’s Conway's Game of Life
, I’ve decided to give this away since it’s quite simple and great for learning about Codea
. It’s by no means perfect but it looks quite nice and it’s very simple
Here’s the code
###Main.lua
-- golly
-- Use this function to perform your initial setup
function setup()
parameter.color("DeadCellColour", color(123,123,223,255))
parameter.color("AliveCellColour", color(10,10,10,255))
parameter.color("Background", color(255,255,255,255))
parameter.boolean("Play")
gol = Gol(15, 30)
gol.drawMethod = function (cell)
wid = WIDTH / 15
hei = HEIGHT / 30
if cell.s == gol._alive then
fill(AliveCellColour)
else
fill(DeadCellColour)
end
rect(wid * (cell.x - 1), hei * cell.y, wid, hei)
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(Background)
-- If we've paused, stop calculating
if Play then
gol:calculate()
end
-- Always draw
gol:draw()
end
function touched(t)
gol:touched(t)
end
###Gol.lua
Gol = class()
function Gol:init(W, H)
-- Set the size
self.columns = W
self.rows = H
-- The varying states of cells
self._alive = 1
self._dead = 0
-- The game states
self._paused = 0
self._stepping = 1
-- Some public things
self.matrix = {}
-- This is a method to set for a draw method
-- Called for every cell every frame
self.drawMethod = function(cell) end
-- Create a blank board
self:create()
end
function Gol:create()
for r = 1, self.rows do
table.insert(self.matrix, {})
for c = 1, self.columns do
table.insert(self.matrix[r], {})
self.matrix[r][c] = self._dead
end
end
end
function Gol:clear()
for r = 1, self.rows do
for c = 1, self.columns do
self.matrix[r][c] = self._dead
end
end
end
function Gol:alive(c, r)
if self.matrix[r][c] == self._alive then
return true
end
return false
end
function Gol:dead(c, r)
if self.matrix[r][c] == self._dead then
return true
end
return false
end
function Gol:calculate()
local n = 0
local tab = self.matrix
-- Neighbour positions from the current index
local nbrs = {
{-1,-1},
{-1,0},
{-1,1},
{0,-1},
{0,1},
{1,-1},
{1,0},
{1,1}
}
for r = 2, self.rows - 1 do
for c = 2, self.columns - 1 do
n = 0
-- How many neighbours does this cell have?
for _,a in ipairs(nbrs) do
if self:alive(c + a[2], r + a[1]) then
n = n + 1
end
end
-- Kill or live
if self:alive(c, r) and n < 2 then
tab[r][c] = self._dead
elseif self:alive(c, r) and (n == 2 or n == 3) then
tab[r][c] = self._alive
elseif self:alive(c, r) and n >= 3 then
tab[r][c] = self._dead
elseif self:dead(c, r) and n == 3 then
tab[r][c] = self._alive
end
end
end
end
function Gol:draw()
for r = 1, self.rows do
for c = 1, self.columns do
self.drawMethod({
x = c,
y = r,
s = self.matrix[r][c]
})
end
end
end
function Gol:touched(touch)
local x = math.floor(touch.x / (WIDTH / self.columns))
local y = math.floor(touch.y / (HEIGHT / self.rows))
self.matrix[y][x] = self._alive
end
Any criticism is welcome as well as improvements and ideas
###Enjoy!