Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- local tray = {
- w = 12;
- h = 18;
- }
- local xoff,yoff = w/2 - tray.w/2, 0
- local avcols = {
- colours.orange, colours.green, colours.blue,
- colours.red, colours.purple, colours.white
- }
- local cnum = 3
- local score = 0
- local gtimer = nil
- local tlength = 1
- local piece = { x = tray.w/2, y = -2}
- function init()
- for y = 1,tray.h do
- tray[y] = {}
- end
- end
- function rotate(dir)
- local temp = nil
- if dir == -1 then
- temp = piece[3]
- for i=3,2,dir do piece[i-1] = piece[i] end
- piece[1] = temp
- else
- temp = piece[1]
- for i=1,2,dir do piece[i+1] = piece[i] end
- piece[3] = temp
- end
- end
- function makePiece()
- for i=1,3 do
- piece[i] = avcols[math.random(1,#avcols)]
- end
- piece.x,piece.y = tray.w/2,-2
- end
- function checkMatches()
- matchlist = {}
- ccol,crun = nil, 0
- --Horizontal
- for y=tray.y,1,-1 do
- top = true
- ccol, crun = nil, 0
- for x=1,tray.w do
- if tray[y][x] then top = false end
- if tray[y][x] == ccol and tray[y][x] ~= nil then
- crun = crun + 1
- if crun == 3 then
- for i=x-2,x do table.insert(matchlist, {i,y}) end
- elseif crun > 3 then
- table.insert(matchlist, {x,y})
- end
- else ccol,crun = tray[y][x],0 end
- end
- end
- --Vertical
- for x=1,tray.x do
- ccol, crun = nil, 0
- for y=tray.y,1 do
- if tray[y][x] == nil then break
- elseif tray[y][x] == ccol then
- crun = crun + 1
- if crun == 3 then
- for i=y-2,y do table.insert(matchlist,{i,y}) end
- elseif crun > 3 then
- table.insert(matchlist, {x,y})
- end
- else ccol,crun = tray[y][x],0 end
- end
- end
- for _,v in ipairs(matchlist) do
- tray[v[2]][v[1]] = nil
- score = score + 1
- end
- return #matchlist > 0
- end
- function runGravity()
- local runAgain = false
- repeat
- runAgain = false
- for y=tray.y-1,1,-1 do
- local top = true
- for x=1,tray.x do
- if tray[y][x] then
- top = false
- if not tray[y-1][x] then
- tray[y-1][x] = tray[y][x]
- tray[y][x] = nil
- runAgain = true
- end
- end
- end
- if top then break end
- end
- sleep(tlength)
- until not runAgain
- end
- function input()
- gtimer = os.startTimer(tlength)
- while true do
- local id,p = os.pullEvent()
- term.setCursorPos(1,1)
- term.write(id..":"..p.."("..gtimer..")")
- if id == "timer" then
- if p == gtimer then
- if piece.y + 3 > tray.h or tray[piece.y + 3][piece.x] ~= nil then
- for i=1,3 do tray[y+i-1][piece.x] = piece[i] end
- makePiece()
- elseif tray[piece.y + 3][piece.x] == nil then
- piece.y = piece.y + 1
- gtimer = os.startTimer(tlength)
- else
- for i=1,3 do tray[y+i-1][piece.x] = piece[i] end
- while true do if not checkMatches() then break end
- runGravity()
- end
- makePiece()
- end
- end
- elseif id == "key" then
- if p == keys.left and piece.x > 0 then piece.x = piece.x - 1
- elseif p == keys.right and piece.x < tray.w then piece.x = piece.x + 1
- elseif p == keys.up then rotate(-1)
- elseif p == keys.down then rotate(1)
- elseif p == keys.q then break end
- end
- draw()
- end
- end
- function draw()
- for y=1, tray.h do
- term.setBackgroundColour(colours.grey)
- term.setCursorPos(xoff,y+yoff)
- term.write(" ")
- term.setCursorPos(xoff + tray.w + 1, y+yoff)
- term.write(" ")
- for x=1, tray.w do
- term.setCursorPos(x+xoff,y+yoff)
- term.setBackgroundColour(tray[y][x] or colours.black)
- term.write(" ")
- end
- end
- for i=1,#piece do
- term.setCursorPos(xoff + piece.x, yoff + piece.y + i)
- term.setBackgroundColour(piece[i])
- term.write(" ")
- end
- end
- init()
- draw()
- makePiece()
- input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement