I wrote "Game of Life" (not the board game) in Lua and it was running very slow. I used to get the pixel color to see if it was alive or not but then changed the alive state into a 2d array.
My current code is:
Code: Select all
while not Controls.read():start() do
-- Create the colors
color_green = Color.new(0, 255, 0)
color_black = Color.new(0, 0, 0)
-- Create the pixelData array
o_pixelData = {}
-- Create the 2d array
for i_counter_0 = 1, 480 do
o_pixelData[i_counter_0] = {}
for i_counter_1 = 1, 272 do
o_pixelData[i_counter_0][i_counter_1] = color_black
end
end
-- Background
g_backGround = Image.createEmpty(480, 272)
-- Fill it black
g_backGround:fillRect(0, 0, 480, 272, color_black)
o_liveCells = {}
g_backGround:pixel(200, 200, color_green)
o_liveCells["o_cell_" .. 200 .. "_" .. 200] = {["i_x"] = 200, ["i_y"] = 200}
o_pixelData[200][200] = color_green
g_backGround:pixel(201, 200, color_green)
o_liveCells["o_cell_" .. 201 .. "_" .. 200] = {["i_x"] = 201, ["i_y"] = 200}
o_pixelData[201][200] = color_green
g_backGround:pixel(202, 200, color_green)
o_liveCells["o_cell_" .. 202 .. "_" .. 200] = {["i_x"] = 202, ["i_y"] = 200}
o_pixelData[202][200] = color_green
g_backGround:pixel(201, 199, color_green)
o_liveCells["o_cell_" .. 201 .. "_" .. 199] = {["i_x"] = 201, ["i_y"] = 199}
o_pixelData[201][199] = color_green
g_backGround:pixel(201, 201, color_green)
o_liveCells["o_cell_" .. 201 .. "_" .. 201] = {["i_x"] = 201, ["i_y"] = 201}
o_pixelData[201][201] = color_green
g_backGround:pixel(203, 201, color_green)
o_liveCells["o_cell_" .. 203 .. "_" .. 201] = {["i_x"] = 203, ["i_y"] = 201}
o_pixelData[203][201] = color_green
-- Function to process life
function f_process()
-- Clear storage
o_toChange = {}
o_deadCells = {}
-- Process each live cell
for i_counter_0 in o_liveCells do
-- Init the neighbours we have
i_neighbours = 0
-- Find out how many neighbours
-- Up left
i_xToUse = o_liveCells[i_counter_0]["i_x"] - 1
i_yToUse = o_liveCells[i_counter_0]["i_y"] - 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Up
i_xToUse = o_liveCells[i_counter_0]["i_x"]
i_yToUse = o_liveCells[i_counter_0]["i_y"] - 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Up right
i_xToUse = o_liveCells[i_counter_0]["i_x"] + 1
i_yToUse = o_liveCells[i_counter_0]["i_y"] - 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Left
i_xToUse = o_liveCells[i_counter_0]["i_x"] - 1
i_yToUse = o_liveCells[i_counter_0]["i_y"]
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Right
i_xToUse = o_liveCells[i_counter_0]["i_x"] + 1
i_yToUse = o_liveCells[i_counter_0]["i_y"]
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Down left
i_xToUse = o_liveCells[i_counter_0]["i_x"] - 1
i_yToUse = o_liveCells[i_counter_0]["i_y"] + 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Down
i_xToUse = o_liveCells[i_counter_0]["i_x"]
i_yToUse = o_liveCells[i_counter_0]["i_y"] + 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Down right
i_xToUse = o_liveCells[i_counter_0]["i_x"] + 1
i_yToUse = o_liveCells[i_counter_0]["i_y"] + 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
else
-- No taken, add to dead cells
o_deadCells["o_cell_" .. i_xToUse .. "_" .. i_yToUse] = {["i_x"] = i_xToUse, ["i_y"] = i_yToUse}
end
-- Does the cell die?
if i_neighbours ~= 2 and i_neighbours ~= 3 then
o_toChange["o_cell_" .. o_liveCells[i_counter_0]["i_x"] .. "_" .. o_liveCells[i_counter_0]["i_y"]] = {["i_x"] = o_liveCells[i_counter_0]["i_x"], ["i_y"] = o_liveCells[i_counter_0]["i_y"], ["i_color"] = color_black}
end
end
-- Process each dead cell
for i_counter_0 in o_deadCells do
-- Init the neighbours we have
i_neighbours = 0
-- Find out how many neighbours
-- Up left
i_xToUse = o_deadCells[i_counter_0]["i_x"] - 1
i_yToUse = o_deadCells[i_counter_0]["i_y"] - 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Up
i_xToUse = o_deadCells[i_counter_0]["i_x"]
i_yToUse = o_deadCells[i_counter_0]["i_y"] - 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Up right
i_xToUse = o_deadCells[i_counter_0]["i_x"] + 1
i_yToUse = o_deadCells[i_counter_0]["i_y"] - 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Left
i_xToUse = o_deadCells[i_counter_0]["i_x"] - 1
i_yToUse = o_deadCells[i_counter_0]["i_y"]
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Right
i_xToUse = o_deadCells[i_counter_0]["i_x"] + 1
i_yToUse = o_deadCells[i_counter_0]["i_y"]
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Down left
i_xToUse = o_deadCells[i_counter_0]["i_x"] - 1
i_yToUse = o_deadCells[i_counter_0]["i_y"] + 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Down
i_xToUse = o_deadCells[i_counter_0]["i_x"]
i_yToUse = o_deadCells[i_counter_0]["i_y"] + 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Down right
i_xToUse = o_deadCells[i_counter_0]["i_x"] + 1
i_yToUse = o_deadCells[i_counter_0]["i_y"] + 1
if o_pixelData[i_xToUse][i_yToUse] == color_green then
-- It's taken, add to the neightbours
i_neighbours = i_neighbours + 1
end
-- Does the cell come alive?
if i_neighbours == 3 then
o_toChange["o_cell_" .. o_deadCells[i_counter_0]["i_x"] .. "_" .. o_deadCells[i_counter_0]["i_y"]] = {["i_x"] = o_deadCells[i_counter_0]["i_x"], ["i_y"] = o_deadCells[i_counter_0]["i_y"], ["i_color"] = color_green}
end
end
-- Change all the cells that need it
for i_counter_0 in o_toChange do
-- Change the color
g_backGround:pixel(o_toChange[i_counter_0]["i_x"], o_toChange[i_counter_0]["i_y"], o_toChange[i_counter_0]["i_color"])
o_pixelData[o_toChange[i_counter_0]["i_x"]][o_toChange[i_counter_0]["i_y"]] = o_toChange[i_counter_0]["i_color"]
-- Do we need to add it or remove it from live cells?
if o_toChange[i_counter_0]["i_color"] == color_green then
-- Add it
o_liveCells["o_cell_" .. o_toChange[i_counter_0]["i_x"] .. "_" .. o_toChange[i_counter_0]["i_y"]] = {["i_x"] = o_toChange[i_counter_0]["i_x"], ["i_y"] = o_toChange[i_counter_0]["i_y"]}
else
-- Remove it
o_liveCells["o_cell_" .. o_toChange[i_counter_0]["i_x"] .. "_" .. o_toChange[i_counter_0]["i_y"]] = nil
end
end
end
-- Game loop
while true do
-- Draw the background
screen:blit(0, 0, g_backGround)
-- Process the life function
f_process()
-- Flip the screen
screen.flip()
end
end
Can anyone see any obvious slow errors?
Thanks,
G