setWindowTitle("Conway's Game of Life")
setScreenResolution(1024, 768)
setVirtualResolution(1024, 768)
setSyncRate(0,0)
gosub init
do
updateCells()
drawGrid()
drawCells()
print(screenFPS())
sync()
loop
////////////////////////////////
init:
#constant numCellsX = 68
#constant numCellsY = 68
global tickDelay : tickDelay = 50 //time between ticks in ms
/////
#constant true = 1
#constant false = 0
global cellSize
global gridOffsetX
global gridOffsetY
global lastTick : lastTick = getMilliseconds()
updateCellSize()
global cells as integer[numCellsX,numCellsY]
randomlyFillCells()
return
function updateCellSize()
if (getVirtualHeight()+0.0)/getVirtualWidth()*numCellsX > numCellsY //X is the constraint
cellSize = getVirtualWidth()/numCellsX
else //Y is the constraint
cellSize = getVirtualHeight()/numCellsY
endif
gridOffsetX = (getVirtualWidth()-numCellsX*cellSize)/2
gridOffsetY = (getVirtualHeight()-numCellsY*cellSize)/2
endfunction
function randomlyFillCells()
for x = 0 to numCellsX-1
for y = 0 to numCellsY-1
cells[x,y] = random(0,1)
next y
next x
endfunction
////////////////////////////////
function updateCells()
time = getMilliseconds()
if time - lastTick >= tickDelay
newCells as integer[numCellsX,numCellsY]
for x = 0 to numCellsX-1
for y = 0 to numCellsY-1
count = countNeighborsWorldWrap(x,y)
if cells[x,y] = 1
if count < 2 //Any live cell with fewer than two live neighbours dies, as if caused by under-population.
newCells[x,y] = 0
elseif count = 2 or count = 3 //Any live cell with two or three live neighbours lives on to the next generation.
newCells[x,y] = 1
elseif count > 3 //Any live cell with more than three live neighbours dies, as if by overcrowding.
newCells[x,y] = 0
endif
endif
if cells[x,y] = 0
if count = 3 //Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
newCells[x,y] = 1
else
newCells[x,y] = 0
endif
endif
next y
next x
lastTick = time
cells = newCells
endif
endfunction
function countNeighborsWorldBorder(x,y)
count = 0
if x <> 0
if y <> 0
if cells[x-1, y-1] = 1 then count = count+1
endif
if cells[x-1, y] = 1 then count = count+1
if y <> numCellsY-1
if cells[x-1, y+1] = 1 then count = count+1
endif
endif
if y <> 0
if cells[x, y-1] = 1 then count = count+1
endif
if y <> numCellsY-1
if cells[x, y+1] = 1 then count = count+1
endif
if x <> numCellsX-1
if y <> 0
if cells[x+1, y-1] = 1 then count = count+1
endif
if cells[x+1, y] = 1 then count = count+1
if y <> numCellsY-1
if cells[x+1, y+1] = 1 then count = count+1
endif
endif
endfunction count
function countNeighborsWorldWrap(x,y)
count = 0
if x <> 0
if y <> 0
if cells[x-1, y-1] = 1 then count = count+1
else
if cells[x-1, numCellsY-1] = 1 then count = count+1
endif
if cells[x-1, y] = 1 then count = count+1
if y <> numCellsY-1
if cells[x-1, y+1] = 1 then count = count+1
else
if cells[x-1, 0] = 1 then count = count+1
endif
else
if y <> 0
if cells[numCellsX-1, y-1] = 1 then count = count+1
else
if cells[numCellsX-1, numCellsY-1] = 1 then count = count+1
endif
if cells[numCellsX-1, y] = 1 then count = count+1
if y <> numCellsY-1
if cells[numCellsX-1, y+1] = 1 then count = count+1
else
if cells[numCellsX-1, 0] = 1 then count = count+1
endif
endif
if y <> 0
if cells[x, y-1] = 1 then count = count+1
else
if cells[x, numCellsY-1] = 1 then count = count+1
endif
if y <> numCellsY-1
if cells[x, y+1] = 1 then count = count+1
else
if cells[x, 0] = 1 then count = count+1
endif
if x <> numCellsX-1
if y <> 0
if cells[x+1, y-1] = 1 then count = count+1
else
if cells[x+1, numCellsY-1] = 1 then count = count+1
endif
if cells[x+1, y] = 1 then count = count+1
if y <> numCellsY-1
if cells[x+1, y+1] = 1 then count = count+1
else
if cells[x+1, 0] = 1 then count = count+1
endif
else
if y <> 0
if cells[0, y-1] = 1 then count = count+1
else
if cells[0, numCellsY-1] = 1 then count = count+1
endif
if cells[0, y] = 1 then count = count+1
if y <> numCellsY-1
if cells[0, y+1] = 1 then count = count+1
else
if cells[0, 0] = 1 then count = count+1
endif
endif
endfunction count
function drawGrid()
for x = 0 to numCellsX
drawLine(gridOffsetX+x*cellSize, gridOffsetY, gridOffsetX+x*cellSize, gridOffsetY+numCellsY*cellSize, 50, 50, 50) //vertical
next x
for y = 0 to numCellsY
drawLine(gridOffsetX, gridOffsetY+y*cellSize, gridOffsetX+numCellsX*cellSize, gridOffsetY+y*cellSize, 50, 50, 50) //horizontal
next y
endfunction
function drawCells()
for x = 0 to numCellsX-1
for y = 0 to numCellsY-1
if cells[x,y] = 1 then drawCell(x, y)
next y
next x
endfunction
function drawCell(x, y)
drawBox(gridOffsetX+x*cellSize, gridOffsetY+y*cellSize+1, gridOffsetX+(x+1)*cellSize-1, gridOffsetY+(y+1)*cellSize-1, 16711680, 16711680, 16711680, 16711680, 1)
endfunction
I had a slice of time so I wrote this in an hour and a half. I was using an old version of AppGameKit that initially led to some issues (wasn't on my normal computer) but I'm happy with the way it turned out
It's not truly very complex. The bulk of the code is the choice between a wrapping world or a bounded one. Feel free to play with the board size and the tick delay (A cap on the speed. A tick delay of 0 will run at full speed). If you prefer you can remove the drawGrid() line
As a side note, apparently drawLine and drawBox work slightly differently with regard to their left most edge. I didn't have to add a 1 to the drawBox x1 coordinate where I expected to. Perhaps the left most side is unintentionally exclusive?