Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Game of Life programmed by Nick Mead(NickM13)
- --A ComputerCraft program
- --v0.0.1 Pastebin Code: yxfaBGdK
- --v0.0.2 Pastebin Code: KLswNb9f
- local exitGame = false
- local playing = false
- local dimension = {}
- local screen = {}
- local cell = {}
- local newCell = {}
- local livingCells = 0
- local mouse = {}
- local gps = 2 -- Generations per second
- local skipping = false
- local function wait(t)
- os.sleep(t)
- end
- local function resetCells()
- gps = 2
- livingCells = 0
- for i=1, dimension.width, 1 do
- for j=1, dimension.height, 1 do
- cell[i][j] = 0
- end
- end
- end
- local function centeredText(msg, y)
- local width = term.getSize()
- term.setCursorPos(math.ceil(width - string.len(msg))/2+1, y)
- term.write(msg)
- end
- local function writeText(msg, x, y)
- term.setCursorPos(x, y)
- term.write(msg)
- end
- local function drawRect(color, x0, y0, x1, y1, outline)
- term.setBackgroundColor(color)
- for x=0, x1-x0, 1 do
- for y=0, y1-y0, 1 do
- if outline then
- if (x == 0 or x == x1-x0 or y == 0 or y == y1-y0) then
- term.setCursorPos(x+x0, y+y0)
- term.write(" ")
- end
- else
- term.setCursorPos(x+x0, y+y0)
- term.write(" ")
- end
- end
- end
- end
- local function keyInput()
- local event, key = os.pullEvent('char')
- if key == ' ' then
- playing = not playing
- elseif key == 't' then
- exitGame = true
- elseif key == 'c' then
- resetCells()
- elseif key == 'a' then
- gps = gps - 1
- if gps < 1 then gps = 1 end
- elseif key == 'd' then
- gps = gps + 1
- if gps > 20 then gps = 20 end
- elseif key == 's' then
- skipping = true
- end
- end
- local function mouseClickInput()
- local event, b, x, y = os.pullEvent('mouse_click')
- if b == 1 then mouse.left = true mouse.lclick = true end
- if b == 2 then mouse.right = true end
- mouse.x = x
- mouse.y = y
- end
- local function mouseDragInput()
- local event, b, x, y = os.pullEvent('mouse_drag')
- if b == 1 then mouse.left = true end
- if b == 2 then mouse.right = true end
- mouse.x = x
- mouse.y = y
- end
- local function input()
- parallel.waitForAny(keyInput, mouseClickInput, mouseDragInput)
- end
- local function update()
- if mouse.left then
- if (mouse.x < dimension.width) then
- if not playing then if cell[mouse.x][mouse.y] == 0 then cell[mouse.x][mouse.y] = 1 livingCells = livingCells + 1 end end
- else
- if mouse.lclick then
- if (mouse.y >= 5 and mouse.y <= 7) then playing = not playing end
- if (mouse.x == screen.width and mouse.y == screen.height) then
- playing = false
- resetCells()
- end
- if (mouse.x == dimension.width + 1 and mouse.y == screen.height) then exitGame = true end
- if mouse.x == dimension.width + 1 then if mouse.y == 2 or mouse.y == 3 then gps = gps - 1 end end
- if mouse.x == dimension.width + 10 then if mouse.y == 2 or mouse.y == 3 then gps = gps + 1 end end
- if (mouse.x > dimension.width + 1 and mouse.y == 9) then skipping = true end
- if gps < 1 then gps = 1 end
- if gps > 20 then gps = 20 end
- end
- end
- mouse.left = false
- mouse.lclick = false
- end
- if mouse.right then
- if (mouse.x < dimension.width) then
- if not playing then if cell[mouse.x][mouse.y] == 1 then cell[mouse.x][mouse.y] = 0 livingCells = livingCells - 1 end end
- else
- end
- mouse.right = false
- end
- if playing or skipping then
- local alive = 0
- livingCells = 0
- for i=1, dimension.width, 1 do
- for j=1, dimension.height, 1 do
- newCell[i][j] = cell[i][j]
- end
- end
- for i=1, dimension.width, 1 do
- for j=1, dimension.height, 1 do
- if (i == 1 and j > 1 and j < dimension.height) then alive = cell[i][j-1] + cell[i+1][j] + cell[i][j+1] + cell[i+1][j-1] + cell[i+1][j+1]
- elseif (i == dimension.width and j > 1 and j < dimension.height) then alive = cell[i-1][j] + cell[i][j-1] + cell[i][j+1] + cell[i-1][j-1] + cell[i-1][j+1]
- elseif (j == 1 and i > 1 and i < dimension.width) then alive = cell[i-1][j] + cell[i+1][j] + cell[i][j+1] + cell[i-1][j+1] + cell[i+1][j+1]
- elseif (j == dimension.height and i > 1 and i < dimension.width) then alive = cell[i-1][j] + cell[i][j-1] + cell[i+1][j] + cell[i-1][j-1] + cell[i+1][j-1]
- elseif (i == 1 and j == 1) then alive = cell[i+1][j] + cell[i][j+1] + cell[i+1][j+1]
- elseif (i == 1 and j == dimension.height) then alive = cell[i][j-1] + cell[i+1][j] + cell[i+1][j-1]
- elseif (i == dimension.width and j == 1) then alive = cell[i-1][j] + cell[i][j+1] + cell[i-1][j+1]
- elseif (i == dimension.width and j == dimension.height) then alive = cell[i-1][j] + cell[i][j-1] + cell[i-1][j-1]
- else alive = cell[i-1][j] + cell[i][j-1] + cell[i+1][j] + cell[i][j+1] + cell[i-1][j-1] + cell[i-1][j+1] + cell[i+1][j-1] + cell[i+1][j+1]
- end
- if cell[i][j] == 1 then
- if alive < 2 then
- newCell[i][j] = 0
- elseif alive > 3 then
- newCell[i][j] = 0
- end
- else
- if alive == 3 then
- newCell[i][j] = 1
- end
- end
- livingCells = livingCells + newCell[i][j]
- end
- end
- for i=1, dimension.width, 1 do
- for j=1, dimension.height, 1 do
- cell[i][j] = newCell[i][j]
- end
- end
- skipping = false
- end
- end
- local function render()
- for i=1, dimension.width, 1 do
- for j=1, dimension.height, 1 do
- term.setCursorPos(i, j)
- if cell[i][j] == 1 then
- term.setBackgroundColor(colors.white)
- else
- term.setBackgroundColor(colors.black)
- end
- term.write(" ")
- end
- end
- drawRect(colors.black, dimension.width + 1, 2, screen.width, 3, false)
- writeText("Gen/s", dimension.width + 4, 2)
- writeText(gps, dimension.width + 5, 3)
- term.setBackgroundColor(colors.lightGray)
- writeText("/", dimension.width + 1, 2)
- writeText("\\", dimension.width + 1, 3)
- writeText("\\", dimension.width + 10, 2)
- writeText("/", dimension.width + 10, 3)
- if playing then
- drawRect(colors.green, dimension.width + 1, 5, dimension.width + 10, 7, false)
- else
- drawRect(colors.red, dimension.width + 1, 5, dimension.width + 10, 7, false)
- end
- drawRect(colors.lightGray, dimension.width + 1, 9, dimension.width + 10, 9, false)
- writeText("Skip Frame", dimension.width + 1, 9)
- drawRect(colors.black, dimension.width + 1, dimension.height - 3, dimension.width + 10, dimension.height - 2, false)
- writeText("Cells:"..livingCells, dimension.width + 2, dimension.height - 2)
- term.setBackgroundColor(colors.black)
- writeText("C", screen.width, screen.height)
- writeText("T", dimension.width + 1, screen.height)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 19)
- end
- local function loop()
- update()
- render()
- wait(1 / gps)
- end
- local function gameLoop()
- while not exitGame do
- parallel.waitForAny(input, loop)
- end
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Terminated.")
- term.setCursorPos(1, 2)
- end
- local function intro()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clear()
- for i=1, 10, 1 do
- centeredText("Game of Life", 10 - i)
- centeredText("Programmed by NickM13", 10 + i)
- writeText("v 0.0.2", 44, 18)
- drawRect(colors.orange, 1, 1, screen.width, screen.height, true)
- wait(1.0/i)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clear()
- end
- term.setCursorPos(1, 19)
- wait(1)
- end
- local function main()
- screen.width, screen.height = term.getSize()
- dimension.height = screen.height
- dimension.width = screen.width - 10
- for i=1, dimension.width, 1 do
- cell[i] = {}
- newCell[i] = {}
- end
- for i=1, dimension.width, 1 do
- for j=1, dimension.height, 1 do
- cell[i][j] = 0
- newCell[i][j] = 0
- end
- end
- cell[6][5] = 1
- cell[7][6] = 1
- cell[7][7] = 1
- cell[6][7] = 1
- cell[5][7] = 1
- livingCells = 5
- intro()
- term.setBackgroundColor(colors.gray)
- term.clear()
- gameLoop()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement