Advertisement
nitrogenfingers

columns

Mar 8th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. local w,h = term.getSize()
  2. local tray = {
  3. w = 12;
  4. h = 18;
  5. }
  6. local xoff,yoff = w/2 - tray.w/2, 0
  7. local avcols = {
  8. colours.orange, colours.green, colours.blue,
  9. colours.red, colours.purple, colours.white
  10. }
  11. local cnum = 3
  12. local score = 0
  13.  
  14. local gtimer = nil
  15. local tlength = 1
  16.  
  17. local piece = { x = tray.w/2, y = -2}
  18.  
  19. function init()
  20. for y = 1,tray.h do
  21. tray[y] = {}
  22. end
  23. end
  24.  
  25. function rotate(dir)
  26. local temp = nil
  27. if dir == -1 then
  28. temp = piece[3]
  29. for i=3,2,dir do piece[i-1] = piece[i] end
  30. piece[1] = temp
  31. else
  32. temp = piece[1]
  33. for i=1,2,dir do piece[i+1] = piece[i] end
  34. piece[3] = temp
  35. end
  36. end
  37.  
  38. function makePiece()
  39. for i=1,3 do
  40. piece[i] = avcols[math.random(1,#avcols)]
  41. end
  42. piece.x,piece.y = tray.w/2,-2
  43. end
  44.  
  45. function checkMatches()
  46. matchlist = {}
  47. ccol,crun = nil, 0
  48. --Horizontal
  49. for y=tray.y,1,-1 do
  50. top = true
  51. ccol, crun = nil, 0
  52. for x=1,tray.w do
  53. if tray[y][x] then top = false end
  54. if tray[y][x] == ccol and tray[y][x] ~= nil then
  55. crun = crun + 1
  56. if crun == 3 then
  57. for i=x-2,x do table.insert(matchlist, {i,y}) end
  58. elseif crun > 3 then
  59. table.insert(matchlist, {x,y})
  60. end
  61. else ccol,crun = tray[y][x],0 end
  62. end
  63. end
  64. --Vertical
  65. for x=1,tray.x do
  66. ccol, crun = nil, 0
  67. for y=tray.y,1 do
  68. if tray[y][x] == nil then break
  69. elseif tray[y][x] == ccol then
  70. crun = crun + 1
  71. if crun == 3 then
  72. for i=y-2,y do table.insert(matchlist,{i,y}) end
  73. elseif crun > 3 then
  74. table.insert(matchlist, {x,y})
  75. end
  76. else ccol,crun = tray[y][x],0 end
  77. end
  78. end
  79. for _,v in ipairs(matchlist) do
  80. tray[v[2]][v[1]] = nil
  81. score = score + 1
  82. end
  83. return #matchlist > 0
  84. end
  85.  
  86. function runGravity()
  87. local runAgain = false
  88. repeat
  89. runAgain = false
  90. for y=tray.y-1,1,-1 do
  91. local top = true
  92. for x=1,tray.x do
  93. if tray[y][x] then
  94. top = false
  95. if not tray[y-1][x] then
  96. tray[y-1][x] = tray[y][x]
  97. tray[y][x] = nil
  98. runAgain = true
  99. end
  100. end
  101. end
  102. if top then break end
  103. end
  104. sleep(tlength)
  105. until not runAgain
  106. end
  107.  
  108. function input()
  109. gtimer = os.startTimer(tlength)
  110. while true do
  111. local id,p = os.pullEvent()
  112. term.setCursorPos(1,1)
  113. term.write(id..":"..p.."("..gtimer..")")
  114. if id == "timer" then
  115. if p == gtimer then
  116. if piece.y + 3 > tray.h or tray[piece.y + 3][piece.x] ~= nil then
  117. for i=1,3 do tray[y+i-1][piece.x] = piece[i] end
  118. makePiece()
  119. elseif tray[piece.y + 3][piece.x] == nil then
  120. piece.y = piece.y + 1
  121. gtimer = os.startTimer(tlength)
  122. else
  123. for i=1,3 do tray[y+i-1][piece.x] = piece[i] end
  124. while true do if not checkMatches() then break end
  125. runGravity()
  126. end
  127. makePiece()
  128. end
  129. end
  130. elseif id == "key" then
  131. if p == keys.left and piece.x > 0 then piece.x = piece.x - 1
  132. elseif p == keys.right and piece.x < tray.w then piece.x = piece.x + 1
  133. elseif p == keys.up then rotate(-1)
  134. elseif p == keys.down then rotate(1)
  135. elseif p == keys.q then break end
  136. end
  137. draw()
  138. end
  139. end
  140.  
  141. function draw()
  142. for y=1, tray.h do
  143. term.setBackgroundColour(colours.grey)
  144. term.setCursorPos(xoff,y+yoff)
  145. term.write(" ")
  146. term.setCursorPos(xoff + tray.w + 1, y+yoff)
  147. term.write(" ")
  148. for x=1, tray.w do
  149. term.setCursorPos(x+xoff,y+yoff)
  150. term.setBackgroundColour(tray[y][x] or colours.black)
  151. term.write(" ")
  152. end
  153. end
  154. for i=1,#piece do
  155. term.setCursorPos(xoff + piece.x, yoff + piece.y + i)
  156. term.setBackgroundColour(piece[i])
  157. term.write(" ")
  158. end
  159. end
  160.  
  161. init()
  162. draw()
  163. makePiece()
  164. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement