Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- tray = {
- w = 12;
- h = 12;
- }
- xoff = math.floor(w/2) - tray.w/2
- yoff = 5
- gcombo = 1
- 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 = 0.25
- local piece,npiece = nil,nil
- 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]
- piece[3] = piece[2]
- piece[2] = piece[1]
- piece[1] = temp
- else
- temp = piece[1]
- piece[1] = piece[2]
- piece[2] = piece[3]
- piece[3] = temp
- end
- end
- function makePiece()
- piece = npiece
- npiece = {x = tray.w/2, y = -2}
- for i=1,3 do
- npiece[i] = avcols[math.random(1,#avcols)]
- end
- if not piece then makePiece() end
- end
- function checkMatches(clist)
- matchlist = {}
- ccol,crun = nil, 0
- term.setTextColour(colours.white)
- for _,v in pairs(clist) do
- local col = tray[v[2]][v[1]]
- local crun = 0
- --horizontal
- for x=math.max(v[1]-2,0),math.min(v[1]+2,tray.w) do
- if tray[v[2]][x] == col then
- crun = crun + 1
- if crun == 3 then for nx=x-2,x do
- table.insert(matchlist, {nx,v[2]}) end
- elseif crun > 3 then table.insert(matchlist, {x,v[2]}) end
- else crun = 0 end
- end
- --vertical
- crun = 0
- for y=math.max(v[2]-2,1),math.min(v[2]+2,tray.h) do
- assert(tray[y],y)
- if tray[y][v[1]] == col then
- crun = crun + 1
- if crun == 3 then for ny=y-2,y do
- table.insert(matchlist, {v[1],ny}) end
- elseif crun > 3 then table.insert(matchlist, {v[1],y}) end
- else crun = 0 end
- end
- --diagonal right
- crun = 0
- for i=-2,2 do
- if tray[v[2]+i] and tray[v[2]+i][v[1]+i] == col then
- crun = crun + 1
- if crun == 3 then for j=i-2,i do
- table.insert(matchlist, {v[1]+j, v[2]+j}) end
- elseif crun > 3 then table.insert(matchlist, {v[1]+i,v[2]+i}) end
- else crun = 0 end
- end
- crun = 0
- --diagonal left
- for i=-2,2 do
- if tray[v[2]-i] and tray[v[2]-i][v[1]+i] == col then
- crun = crun + 1
- if crun == 3 then for j=i-2,i do
- table.insert(matchlist, {v[1]+j,v[2]-j}) end
- elseif crun > 3 then table.insert(matchlist, {v[1]+i,v[2]-i}) end
- else crun = 0 end
- end
- end
- --A display then we delete
- if #matchlist == 0 then return false end
- for i=1,2 do
- draw()
- for _,v in pairs(matchlist) do
- assert(tray[v[2]] and tray[v[2]][v[1]], v[1]..","..v[2])
- term.setCursorPos(xoff + v[1], yoff + v[2])
- term.setBackgroundColour(tray[v[2]][v[1]])
- term.setTextColour(colours.black)
- if i==1 then term.write("*")
- else term.write("#") end
- end
- sleep(tlength/2)
- end
- for _,v in ipairs(matchlist) do
- tray[v[2]][v[1]] = nil
- score = score + (10 * gcombo)
- end
- return #matchlist > 0
- end
- function runGravity()
- local mlist = nil
- local runAgain = false
- repeat
- local tlist = {}
- runAgain = false
- for y=tray.h-1,1,-1 do
- for x=1,tray.w do
- if tray[y][x] and not tray[y+1][x] then
- tray[y+1][x] = tray[y][x]
- tray[y][x] = nil
- table.insert(tlist,{x,y+1})
- runAgain = true
- end
- end
- end
- draw()
- if runAgain then
- sleep(tlength/2)
- mlist = tlist
- end
- tlist = {}
- until not runAgain
- return mlist
- end
- function drop()
- gcombo = 1
- local mlist = {}
- for i=1,3 do
- tray[piece.y+i-1][piece.x] = piece[i]
- table.insert(mlist, {piece.x, piece.y +i -1})
- end
- makePiece()
- while true do
- if not checkMatches(mlist) then break end
- mlist = runGravity()
- gcombo = gcombo + 1
- if not mlist then break end
- end
- end
- function canMove(dir)
- return (not tray[piece.y] or tray[piece.y][piece.x+dir] == nil) and
- (not tray[piece.y+1] or tray[piece.y+1][piece.x+dir] == nil) and
- (not tray[piece.y+2] or tray[piece.y+2][piece.x+dir] == nil)
- end
- function input()
- gtimer = os.startTimer(tlength)
- while true do
- local id,p = os.pullEvent()
- if id == "timer" then
- if p == gtimer then
- if piece.y + 2 >= tray.h or tray[piece.y + 3][piece.x] ~= nil then
- if piece.y < 1 then break end
- drop()
- elseif tray[piece.y + 3][piece.x] == nil then
- piece.y = piece.y + 1
- end
- gtimer = os.startTimer(tlength)
- end
- elseif id == "key" then
- if p == keys.left and gtimer and piece.x > 1 and
- canMove(-1) then piece.x = piece.x - 1
- elseif p == keys.right and gtimer and piece.x < tray.w and
- canMove(1) then piece.x = piece.x + 1
- elseif p == keys.up and gtimer then rotate(1)
- elseif p == keys.down and gtimer then rotate(-1)
- elseif p == keys.space then
- while piece.y + 2 < tray.h and tray[piece.y + 3][piece.x] == nil do
- piece.y = piece.y + 1
- end
- if piece.y < 1 then break end
- drop()
- gtimer = os.startTimer(tlength)
- elseif p == keys.q then os.pullEvent() break
- elseif p == keys.p then
- if gtimer then gtimer = nil
- else gtimer = os.startTimer(tlength) end
- end
- end
- draw()
- end
- end
- function drawNext(npiece,tray)
- term.setCursorPos(xoff+tray.w+1,yoff+1)
- term.setBackgroundColour(colours.grey)
- term.write(string.rep(" ", 5))
- term.setCursorPos(xoff+tray.w+1,yoff+5)
- term.write(string.rep(" ", 5))
- for i=1,3 do
- term.setCursorPos(xoff+tray.w+2,yoff+i+1)
- term.setBackgroundColour(colours.black)
- term.write(string.rep(" ", 3))
- term.setBackgroundColour(colours.grey)
- term.write(" ")
- end
- for i=1,#npiece do
- term.setCursorPos(xoff + tray.w + 3, yoff + i+1)
- term.setBackgroundColour(npiece[i])
- term.write(" ")
- end
- end
- function draw()
- term.setCursorBlink(false)
- --There's an offset error I'm... too tired to fix
- term.setCursorPos(xoff,yoff+tray.h+1)
- term.setBackgroundColour(colours.grey)
- term.write(string.rep(" ",tray.w+2))
- term.setBackgroundColour(colours.lightGrey)
- for y=yoff-2,yoff do
- term.setCursorPos(xoff,y)
- term.write(string.rep(" ", tray.w + 2))
- end
- 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 - 1)
- term.setBackgroundColour(piece[i])
- term.write(" ")
- end
- term.setCursorPos(xoff + 1,yoff + tray.h + 1)
- term.setBackgroundColour(colours.grey)
- term.setTextColour(colours.white)
- term.write("Score: "..score)
- drawNext(npiece, tray)
- term.setBackgroundColour(colours.lightGrey)
- term.setTextColour(colours.red)
- local txt = "Column Drop"
- term.setCursorPos(w/2-#txt/2,2)
- term.write(txt)
- end
- term.setBackgroundColour(colours.lightGrey)
- shell.run("clear")
- init()
- makePiece()
- draw()
- input()
- term.setBackgroundColour(colours.black)
- shell.run("clear")
Add Comment
Please, Sign In to add comment