Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- lg = love.graphics
- lg.setBackgroundColor(150,150,150)
- width,height = lg.getWidth(),lg.getHeight()
- pause = true
- size = 12
- numberx = math.floor(width / (size + 1))
- numbery = math.floor(height / (size + 1))
- startx = width % (size + 1) / 2
- starty = height % (size + 1) / 2
- world = {}
- for i = 1, numberx do
- world[i] = {}
- for j = 1, numbery do
- world[i][j] = false
- end
- end
- x = 0
- y = 0
- paused = true
- masterClock = 0
- step = 0
- --Menu (Not implemented)
- menuBar = {}
- menuBar.x = 1
- menuBar.y = -30
- menuBar.height = 30
- menuBar.speed = 4
- buttons = {}
- buttons[1] = {}
- buttons[1].x = 10
- buttons[1].y = 10
- buttons[1].width = 100
- buttons[1].height = 100
- backgroundC = 150
- end
- function love.update( dt )
- masterClock = masterClock + dt
- if love.mouse.isDown("l") then
- mousepressed("l")
- elseif love.mouse.isDown("r") then
- mousepressed("r")
- end
- if not paused then
- if backgroundC > 150 then
- backgroundC = backgroundC - 0.5
- end
- else
- if backgroundC < 160 then
- backgroundC = backgroundC + 1
- end
- end
- end
- function love.draw()
- if masterClock - step >= 0.05 then
- step = masterClock
- if not paused then evolve() end
- end
- sinP = math.abs(math.sin(masterClock * 10) * 255)
- cosC = math.abs(math.cos(masterClock * 2) * 55)
- lg.setColor(100,100,100)
- n = 0
- for i = 1, #world do
- for j = 1, #world[i] do
- if world[i][j] == true then
- if paused then
- lg.setColor(200 + cosC,200 + cosC, 55 + -cosC)
- else
- lg.setColor(0,255,100)
- end
- else
- if paused then
- lg.setColor(backgroundC,backgroundC,backgroundC)
- else
- lg.setColor(backgroundC,backgroundC,backgroundC)
- end
- end
- drawSquare(((i - 1) * size) + i + startx, ((j - 1) * size) + j + starty, size)
- if world[i][j] == true then n = n + 1 end
- end
- end
- lg.setCaption("Conway's Game Of Life - ".."FPS: "..love.timer.getFPS().." Pixels Active: "..n)
- if paused then
- lg.setColor(0,0,0,180)
- lg.rectangle("fill",width / 2 - 147 / 2,10 + 3 ,147,20)
- lg.setColor(255,255,255,sinP)
- lg.print("< Paused >",width / 2 + 8 - 147/2,10 + 4 + 2 )
- end
- end
- function buttonCheck(x,y)
- end
- function drawSquare(x,y,size)
- lg.rectangle("fill",x,y,size,size)
- end
- function mousepressed(button)
- x = love.mouse.getX()
- y = love.mouse.getY()
- for i = 1, numberx do
- for j = 1, numbery do
- if x > i + ((i - 1) * size) + startx and x < i + ((i - 1) * size) + startx + size + 2 then
- if y > j + ((j - 1) * size) + starty and y < j + ((j - 1) * size) + starty + size + 2 then
- if button == "l" then world[i][j] = true elseif button == "r" then world[i][j] = false end
- end
- end
- end
- end
- end
- function love.keypressed( key )
- if key == "p" then paused = not paused end
- end
- function pixel(x,y)
- if math.floor(x) < numberx and math.floor(y) < numbery and math.floor(x) > 0 and math.floor(y) > 0 then
- world[math.floor(x) ][math.floor(y) ] = true
- end
- end
- function line(x1,y1,x2,y2)
- dx = math.abs(x2 - x1)
- dy = math.abs(y2 - y1)
- if x1 < x2 then sx = 1 else sx = -1 end
- if y1 < y2 then sy = 1 else sy = -1 end
- err = dx - dy
- while true do
- pixel(x1,y1)
- if x1 == x2 and y1 == y2 then break end
- e2 = 2 * err
- if e2 > -dy then
- err = err - dy
- x1 = x1 + sx
- end
- if x1 == x2 and y1 == y2 then
- pixel(x1,y1)
- break
- end
- if e2 < dx then
- err = err + dx
- y1 = y1 + sy
- end
- end
- end
- function clear()
- for i = 1, numberx do
- for j = 1, numbery do
- world[i][j] = false
- end
- end
- end
- function circle(x,y,r)
- multiplyPoint = r * (math.pi * 2)
- for i = 1, multiplyPoint + 1 do
- angle = i * (2 * math.pi) / (multiplyPoint)
- cosx = x + (math.cos(angle) * r)
- siny = y + (math.sin(angle) * r)
- pixel(cosx + 1, siny + 1)
- end
- end
- function evolve()
- tempWorld = {}
- for i = 1, numberx do
- tempWorld[i] = {}
- for j = 1, numbery do
- count = getNeighbors(i,j)
- if world[i][j] == true then
- if count < 2 then
- tempWorld[i][j] = false
- elseif count > 3 then
- tempWorld[i][j] = false
- else
- tempWorld[i][j] = true
- end
- elseif world[i][j] == false then
- if count == 3 then
- tempWorld[i][j] = true
- else
- tempWorld[i][j] = false
- end
- end
- end
- end
- world = tempWorld
- tempWorld = {}
- end
- function getNeighbors(x,y)
- count = 0
- if(x > 1 and y > 1) and (x < numberx and y < numberx) then
- if(world[x-1][y-1] == true)then count = count + 1 end
- if(world[x][y-1] == true)then count = count + 1 end
- if(world[x+1][y-1] == true)then count = count + 1 end
- if(world[x-1][y] == true)then count = count + 1 end
- if(world[x+1][y] == true)then count = count + 1 end
- if(world[x-1][y+1] == true)then count = count + 1 end
- if(world[x][y+1] == true)then count = count + 1 end
- if(world[x+1][y+1] == true)then count = count + 1 end
- end
- return count
- end
Advertisement
Add Comment
Please, Sign In to add comment