Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local scr_x, scr_y = term.getSize()
- local screen = {}
- local resetScreen = function()
- screen = {}
- for a = 1, scr_y do
- table.insert(screen,string.rep("0",scr_x))
- end
- end
- substrReplace = function(str,x,char)
- if not char and x then return str end
- if not str then return end
- return str:sub(1,x-1)..char..str:sub(x+1)
- end
- local getchar = function(x,y)
- if not screen[y] then return -1 end
- if x > #screen[y] then return -1 end
- return tonumber(screen[y]:sub(x,x)) or screen[y]:sub(x,x)
- end
- local setchar = function(x,y,char,scr)
- if not scr[y] then return end
- if x < 1 or x > #scr[y] then return end
- scr[y] = substrReplace(scr[y],x,char:sub(1,1))
- end
- local scrcols = {
- ["0"] = colors.black,
- ["1"] = colors.gray,
- ["2"] = colors.lightGray,
- ["3"] = colors.white,
- }
- local render = function()
- for y = 1, #screen do
- for x = 1, #screen[y] do
- term.setCursorPos(x,y)
- term.setBackgroundColor(scrcols[getchar(x,y)] or colors.black)
- term.write(" ")
- end
- end
- end
- local tn = tonumber
- local ts = tostring
- local proceed = function()
- local output = {} for a = 1, #screen do output[a] = screen[a] end
- for y = 1, #screen do
- for x = 1, #screen[y] do
- local num = tn(getchar(x,y))
- if num > 0 then
- setchar(x,y,ts(num-2),output)
- setchar(x+1,y,ts(getchar(x+1,y)+1),output)
- setchar(x-1,y,ts(getchar(x-1,y)+1),output)
- setchar(x,y+1,ts(getchar(x,y+1)+1),output)
- setchar(x,y-1,ts(getchar(x,y-1)+1),output)
- end
- end
- end
- for a = 1, #output do screen[a] = output[a] end
- end
- resetScreen()
- local input = function()
- while true do
- local evt = {os.pullEvent()}
- if evt[1] == "mouse_click" then
- setchar(evt[3],evt[4],"2",screen)
- end
- if evt[1] == "key" then
- if evt[2] == keys.q then return end
- if evt[2] == keys.space then resetScreen() end
- end
- end
- end
- local game = function()
- while true do
- proceed()
- render()
- sleep(0.1)
- end
- end
- term.clear()
- parallel.waitForAny(game,input)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement