Code: Select all
function load()
Cell = love.graphics.newImage("Cell.PNG")
WorldX = {}
for a=1,160,1 do
WorldX[a] = {}
WorldX[a].WorldY = {}
for b=1,120,1 do
WorldX[a].WorldY[b] = false
end
end
ChangeX = {}
for a=1,160,1 do
ChangeX[a] = {}
ChangeX[a].ChangeY = {}
for b=1,120,1 do
ChangeX[a].ChangeY[b] = false
end
end
Playing = false
Neighbors = 0
time = 0
end
function draw()
for a=1,160,1 do
for b=1,120,1 do
if WorldX[a].WorldY[b] then
love.graphics.draw(Cell,a*5-2.5,b*5-2.5)
end
end
end
end
function update(dt)
if Playing then
time = dt+time
if time > .1 then
time = time-.1
life()
end
end
end
function keypressed(key)
if key == love.key_right and Playing == false then
life()
end
if key == love.key_space then
if Playing then
Playing = false
else
Playing = true
end
end
end
function mousepressed(x,y,button)
if button == love.mouse_left and Playing == false then
if WorldX[math.floor((x+5)/5)].WorldY[math.floor((y+5)/5)] == false then
WorldX[math.floor((x+5)/5)].WorldY[math.floor((y+5)/5)] = true
else
WorldX[math.floor((x+5)/5)].WorldY[math.floor((y+5)/5)] = false
end
end
end
function life()
for a=1,160,1 do
for b=1,120,1 do
Neighbors = 0
if a>1 then
if WorldX[a-1].WorldY[b] then
Neighbors = Neighbors+1
end
end
if a<160 then
if WorldX[a+1].WorldY[b] then
Neighbors = Neighbors+1
end
end
if b>1 then
if WorldX[a].WorldY[b-1] then
Neighbors = Neighbors+1
end
end
if b<120 then
if WorldX[a].WorldY[b+1] then
Neighbors = Neighbors+1
end
end
if a>1 and b>1 then
if WorldX[a-1].WorldY[b-1] then
Neighbors = Neighbors+1
end
end
if a<160 and b<120 then
if WorldX[a+1].WorldY[b+1] then
Neighbors = Neighbors+1
end
end
if b>1 and a<160 then
if WorldX[a+1].WorldY[b-1] then
Neighbors = Neighbors+1
end
end
if b<120 and a>1 then
if WorldX[a-1].WorldY[b+1] then
Neighbors = Neighbors+1
end
end
if WorldX[a].WorldY[a] then
if Neighbors<2 or Neighbors>3 then
ChangeX[a].ChangeY[b] = false
else
ChangeX[a].ChangeY[b] = true
end
else
if Neighbors == 3 then
ChangeX[a].ChangeY[b] = true
end
end
end
end
for a=1,160,1 do
for b=1,120,1 do
WorldX[a].WorldY[b] = ChangeX[a].ChangeY[b]
end
end
end