nitrogenfingers

Columns

Mar 8th, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.28 KB | None | 0 0
  1. local w,h = term.getSize()
  2.  
  3. tray = {
  4. w = 12;
  5. h = 12;
  6. }
  7. xoff = math.floor(w/2) - tray.w/2
  8. yoff = 5
  9. gcombo = 1
  10.  
  11. local avcols = {
  12.   colours.orange, colours.green, colours.blue,
  13.   colours.red, colours.purple, colours.white
  14. }
  15. local cnum = 3
  16. local score = 0
  17.  
  18. local gtimer = nil
  19. local tlength = 0.25
  20.  
  21. local piece,npiece = nil,nil
  22. function init()
  23.   for y = 1,tray.h do
  24.     tray[y] = {}
  25.   end
  26. end
  27.  
  28. function rotate(dir)
  29.   local temp = nil
  30.   if dir == -1 then
  31.     temp = piece[3]
  32.     piece[3] = piece[2]
  33.     piece[2] = piece[1]
  34.     piece[1] = temp
  35.   else
  36.     temp = piece[1]
  37.     piece[1] = piece[2]
  38.     piece[2] = piece[3]
  39.     piece[3] = temp
  40.   end
  41. end
  42.  
  43. function makePiece()
  44.   piece = npiece
  45.   npiece = {x = tray.w/2, y = -2}
  46.   for i=1,3 do
  47.     npiece[i] = avcols[math.random(1,#avcols)]
  48.   end
  49.   if not piece then makePiece() end
  50. end
  51.  
  52. function checkMatches(clist)
  53.   matchlist = {}
  54.   ccol,crun = nil, 0
  55.   term.setTextColour(colours.white)
  56.  
  57.   for _,v in pairs(clist) do
  58.     local col = tray[v[2]][v[1]]
  59.     local crun = 0
  60.     --horizontal
  61.     for x=math.max(v[1]-2,0),math.min(v[1]+2,tray.w) do
  62.       if tray[v[2]][x] == col then
  63.         crun = crun + 1
  64.         if crun == 3 then for nx=x-2,x do
  65.           table.insert(matchlist, {nx,v[2]}) end
  66.         elseif crun > 3 then table.insert(matchlist, {x,v[2]}) end
  67.       else crun = 0 end
  68.     end
  69.     --vertical
  70.     crun = 0
  71.     for y=math.max(v[2]-2,1),math.min(v[2]+2,tray.h) do
  72.       assert(tray[y],y)
  73.       if tray[y][v[1]] == col then
  74.         crun = crun + 1
  75.         if crun == 3 then for ny=y-2,y do
  76.           table.insert(matchlist, {v[1],ny}) end
  77.         elseif crun > 3 then table.insert(matchlist, {v[1],y}) end
  78.       else crun = 0 end
  79.     end
  80.     --diagonal right
  81.     crun = 0
  82.     for i=-2,2 do
  83.       if tray[v[2]+i] and tray[v[2]+i][v[1]+i] == col then
  84.         crun = crun + 1
  85.         if crun == 3 then for j=i-2,i do
  86.           table.insert(matchlist, {v[1]+j, v[2]+j}) end
  87.         elseif crun > 3 then table.insert(matchlist, {v[1]+i,v[2]+i}) end
  88.       else crun = 0 end
  89.     end
  90.     crun = 0
  91.     --diagonal left
  92.     for i=-2,2 do
  93.       if tray[v[2]-i] and tray[v[2]-i][v[1]+i] == col then
  94.         crun = crun + 1
  95.         if crun == 3 then for j=i-2,i do
  96.           table.insert(matchlist, {v[1]+j,v[2]-j}) end
  97.         elseif crun > 3 then table.insert(matchlist, {v[1]+i,v[2]-i}) end
  98.       else crun = 0 end
  99.     end
  100.   end
  101.  
  102.   --A display then we delete
  103.   if #matchlist == 0 then return false end
  104.   for i=1,2 do
  105.     draw()
  106.     for _,v in pairs(matchlist) do
  107.       assert(tray[v[2]] and tray[v[2]][v[1]], v[1]..","..v[2])
  108.       term.setCursorPos(xoff + v[1], yoff + v[2])
  109.       term.setBackgroundColour(tray[v[2]][v[1]])
  110.       term.setTextColour(colours.black)
  111.       if i==1 then term.write("*")
  112.       else term.write("#") end
  113.     end
  114.     sleep(tlength/2)
  115.   end
  116.   for _,v in ipairs(matchlist) do
  117.     tray[v[2]][v[1]] = nil
  118.     score = score + (10 * gcombo)
  119.   end
  120.   return #matchlist > 0
  121. end
  122.  
  123. function runGravity()
  124.   local mlist = nil
  125.   local runAgain = false
  126.   repeat
  127.     local tlist = {}
  128.     runAgain = false
  129.     for y=tray.h-1,1,-1 do
  130.       for x=1,tray.w do
  131.         if tray[y][x] and not tray[y+1][x] then
  132.           tray[y+1][x] = tray[y][x]
  133.           tray[y][x] = nil
  134.           table.insert(tlist,{x,y+1})
  135.           runAgain = true
  136.         end
  137.       end
  138.     end
  139.     draw()
  140.     if runAgain then
  141.       sleep(tlength/2)
  142.       mlist = tlist
  143.     end
  144.     tlist = {}
  145.   until not runAgain
  146.   return mlist
  147. end
  148.  
  149. function drop()
  150.   gcombo = 1
  151.   local mlist = {}
  152.   for i=1,3 do
  153.     tray[piece.y+i-1][piece.x] = piece[i]
  154.     table.insert(mlist, {piece.x, piece.y +i -1})
  155.   end
  156.   makePiece()
  157.   while true do
  158.     if not checkMatches(mlist) then break end
  159.     mlist = runGravity()
  160.     gcombo = gcombo + 1
  161.     if not mlist then break end
  162.   end
  163. end
  164.  
  165. function canMove(dir)
  166.   return (not tray[piece.y] or tray[piece.y][piece.x+dir] == nil) and
  167.        (not tray[piece.y+1] or tray[piece.y+1][piece.x+dir] == nil) and
  168.        (not tray[piece.y+2] or tray[piece.y+2][piece.x+dir] == nil)
  169. end
  170.  
  171. function input()
  172.   gtimer = os.startTimer(tlength)
  173.   while true do
  174.     local id,p = os.pullEvent()
  175.     if id == "timer" then
  176.       if p == gtimer then
  177.         if piece.y + 2 >= tray.h or tray[piece.y + 3][piece.x] ~= nil then
  178.           if piece.y < 1 then break end
  179.           drop()
  180.         elseif tray[piece.y + 3][piece.x] == nil then
  181.           piece.y = piece.y + 1
  182.         end
  183.         gtimer = os.startTimer(tlength)
  184.       end
  185.     elseif id == "key" then
  186.       if p == keys.left and gtimer and piece.x > 1 and
  187.         canMove(-1) then piece.x = piece.x - 1
  188.       elseif p == keys.right and gtimer and piece.x < tray.w and
  189.         canMove(1) then piece.x = piece.x + 1
  190.       elseif p == keys.up and gtimer then rotate(1)
  191.       elseif p == keys.down and gtimer then rotate(-1)
  192.       elseif p == keys.space then
  193.         while piece.y + 2 < tray.h and tray[piece.y + 3][piece.x] == nil do
  194.           piece.y = piece.y + 1
  195.         end
  196.         if piece.y < 1 then break end
  197.         drop()
  198.         gtimer = os.startTimer(tlength)
  199.       elseif p == keys.q then os.pullEvent() break
  200.       elseif p == keys.p then
  201.         if gtimer then gtimer = nil
  202.         else gtimer = os.startTimer(tlength) end
  203.       end
  204.     end
  205.     draw()
  206.   end
  207. end
  208.  
  209. function drawNext(npiece,tray)
  210.   term.setCursorPos(xoff+tray.w+1,yoff+1)
  211.   term.setBackgroundColour(colours.grey)
  212.   term.write(string.rep(" ", 5))
  213.   term.setCursorPos(xoff+tray.w+1,yoff+5)
  214.   term.write(string.rep(" ", 5))
  215.   for i=1,3 do
  216.     term.setCursorPos(xoff+tray.w+2,yoff+i+1)
  217.     term.setBackgroundColour(colours.black)
  218.     term.write(string.rep(" ", 3))
  219.     term.setBackgroundColour(colours.grey)
  220.     term.write(" ")
  221.   end
  222.   for i=1,#npiece do
  223.     term.setCursorPos(xoff + tray.w + 3, yoff + i+1)
  224.     term.setBackgroundColour(npiece[i])
  225.     term.write(" ")
  226.   end
  227. end
  228.  
  229. function draw()
  230.   term.setCursorBlink(false)
  231.   --There's an offset error I'm... too tired to fix
  232.   term.setCursorPos(xoff,yoff+tray.h+1)
  233.   term.setBackgroundColour(colours.grey)
  234.   term.write(string.rep(" ",tray.w+2))
  235.   term.setBackgroundColour(colours.lightGrey)
  236.   for y=yoff-2,yoff do
  237.     term.setCursorPos(xoff,y)
  238.     term.write(string.rep(" ", tray.w + 2))
  239.   end
  240.  
  241.   for y=1,tray.h do
  242.     term.setBackgroundColour(colours.grey)
  243.     term.setCursorPos(xoff,y+yoff)
  244.     term.write(" ")
  245.     term.setCursorPos(xoff + tray.w + 1, y+yoff)
  246.     term.write(" ")
  247.     for x=1, tray.w do
  248.       term.setCursorPos(x+xoff,y+yoff)
  249.       term.setBackgroundColour(tray[y][x] or colours.black)
  250.       term.write(" ")
  251.     end
  252.   end
  253.   for i=1,#piece do
  254.     term.setCursorPos(xoff + piece.x, yoff + piece.y + i - 1)
  255.     term.setBackgroundColour(piece[i])
  256.     term.write(" ")
  257.   end
  258.   term.setCursorPos(xoff + 1,yoff + tray.h + 1)
  259.   term.setBackgroundColour(colours.grey)
  260.   term.setTextColour(colours.white)
  261.   term.write("Score: "..score)
  262.   drawNext(npiece, tray)
  263.  
  264.   term.setBackgroundColour(colours.lightGrey)
  265.   term.setTextColour(colours.red)
  266.   local txt = "Column Drop"
  267.   term.setCursorPos(w/2-#txt/2,2)
  268.   term.write(txt)
  269. end
  270.  
  271. term.setBackgroundColour(colours.lightGrey)
  272. shell.run("clear")
  273. init()
  274. makePiece()
  275. draw()
  276. input()
  277. term.setBackgroundColour(colours.black)
  278. shell.run("clear")
Add Comment
Please, Sign In to add comment