Guest User

tetris

a guest
Dec 8th, 2013
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.79 KB | None | 0 0
  1. local outline = paintutils.loadImage("tetris_files/board_outline")
  2. local score = 0
  3.  
  4. local board = {
  5. "..........",
  6. "..........",
  7. "..........",
  8. "..........",
  9. "..........",
  10. "..........",
  11. "..........",
  12. "..........",
  13. "..........",
  14. "..........",
  15. "..........",
  16. "..........",
  17. "..........",
  18. "..........",
  19. "..........",
  20. ".........."
  21. }
  22.  
  23. local blocks = {
  24. {
  25. color = colors.cyan,
  26. height = 1,
  27. length = 4,
  28. layer = {"xxxx"}
  29. },
  30. {
  31. color = colors.blue,
  32. height = 2,
  33. length = 3,
  34. layer = {"x..","xxx"}
  35. },
  36. {
  37. color = colors.orange,
  38. height = 2,
  39. length = 3,
  40. layer = {"..x","xxx"}
  41. },
  42. {
  43. color = colors.yellow,
  44. height = 2,
  45. length = 2,
  46. layer = {"xx","xx"}
  47. },
  48. {
  49. color = colors.lime,
  50. height = 2,
  51. length = 3,
  52. layer = {".xx","xx."}
  53. },
  54. {
  55. color = colors.purple,
  56. height = 2,
  57. length = 3,
  58. layer = {".x.","xxx"}
  59. },
  60. {
  61. color = colors.red,
  62. height = 2,
  63. length = 3,
  64. layer = {"xx.",".xx"}
  65. }
  66. }
  67.  
  68. local function drawBlock(block,x,y,rotation,next)
  69.     layer = {}
  70.     height = blocks[block].height
  71.     length = blocks[block].length
  72.     for c = 1,height do
  73.         layer[c] = blocks[block].layer[c]
  74.     end
  75.     --rotates block
  76.     if rotation == 1 then
  77.         l = ""
  78.         for a = 1,length do
  79.             for b = 1,height do
  80.                 l = l..blocks[block].layer[height-b+1]:sub(a,a)
  81.             end
  82.             layer[a] = l
  83.             l = ""
  84.         end
  85.         length = blocks[block].height
  86.         height = blocks[block].length
  87.     elseif rotation == 2 then
  88.         l = ""
  89.         for a = 1,height do
  90.             for b = 1,length do
  91.                 l = l..blocks[block].layer[a]:sub(length-b+1,length-b+1)
  92.             end
  93.             layer[height-a+1] = l
  94.             l = ""
  95.         end
  96.     elseif rotation == 3 then
  97.         l = ""
  98.         for a = 1,length do
  99.             for b = 1,height do
  100.                 l = l..blocks[block].layer[b]:sub(length-a+1,length-a+1)
  101.             end
  102.             layer[a] = l
  103.             l = ""
  104.         end
  105.         length = blocks[block].height
  106.         height = blocks[block].length
  107.     end
  108.     --draws block to screen
  109.     for a = 1,height do
  110.         term.setCursorPos(x+3,y+a+1)
  111.         for b = 1,length do
  112.             if layer[a]:sub(b,b) == "x" then
  113.                 term.setBackgroundColor(blocks[block].color)
  114.                 write(" ")
  115.                 if next ~= true then
  116.                 board[y+a-1] = board[y+a-1]:sub(1,x+b-2).."x"..board[y+a-1]:sub(x+b,10)
  117.                 end
  118.             else
  119.                 term.setCursorPos(x+b+3,y+a+1)
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125. local function drawScore()
  126.     term.setCursorPos(19,5)
  127.     term.setTextColor(colors.black)
  128.     term.setBackgroundColor(colors.white)
  129.     write("Score")
  130.     term.setBackgroundColor(colors.white)
  131.     term.setTextColor(colors.black)
  132.     term.setCursorPos(22-(#tostring(score)/2),6)
  133.     write(score)
  134. end
  135.  
  136. local function removeBlock(block,x,y,rotation)
  137.     layer = {}
  138.     height = blocks[block].height
  139.     length = blocks[block].length
  140.     for c = 1,height do
  141.         layer[c] = blocks[block].layer[c]
  142.     end
  143.     --rotates block
  144.     if rotation == 1 then
  145.         l = ""
  146.         for a = 1,length do
  147.             for b = 1,height do
  148.                 l = l..blocks[block].layer[height-b+1]:sub(a,a)
  149.             end
  150.             layer[a] = l
  151.             l = ""
  152.         end
  153.         length = blocks[block].height
  154.         height = blocks[block].length
  155.     elseif rotation == 2 then
  156.         l = ""
  157.         for a = 1,height do
  158.             for b = 1,length do
  159.                 l = l..blocks[block].layer[a]:sub(length-b+1,length-b+1)
  160.             end
  161.             layer[height-a+1] = l
  162.             l = ""
  163.         end
  164.     elseif rotation == 3 then
  165.         l = ""
  166.         for a = 1,length do
  167.             for b = 1,height do
  168.                 l = l..blocks[block].layer[b]:sub(length-a+1,length-a+1)
  169.             end
  170.             layer[a] = l
  171.             l = ""
  172.         end
  173.         length = blocks[block].height
  174.         height = blocks[block].length
  175.     end
  176.     --draws block to screen
  177.     for a = 1,height do
  178.         term.setCursorPos(x+3,y+a+1)
  179.         for b = 1,length do
  180.             if layer[a]:sub(b,b) == "x" then
  181.                 term.setBackgroundColor(colors.white)
  182.                 write(" ")
  183.                 board[y+a-1] = board[y+a-1]:sub(1,x+b-2).."."..board[y+a-1]:sub(x+b,10)
  184.             else
  185.                 term.setCursorPos(x+b+3,y+a+1)
  186.             end
  187.         end
  188.     end
  189. end
  190.  
  191. function checkRow(row)
  192.     if board[row] == "xxxxxxxxxx" then
  193.         for a = 1,row do
  194.             board[row-a+1] = board[row-a]
  195.         end
  196.         board[1] = ".........."
  197.         score = score+80
  198.         drawScore()
  199.     end
  200. end
  201.  
  202. function moveBlock(block,x,y,rotation,direction)
  203.     moving = true
  204.     layer = {}
  205.     local height = blocks[block].height
  206.     local length = blocks[block].length
  207.     for c = 1,height do
  208.         layer[c] = blocks[block].layer[c]
  209.     end
  210.     --rotates block
  211.     if rotation == 1 then
  212.         l = ""
  213.         for a = 1,length do
  214.             for b = 1,height do
  215.                 l = l..blocks[block].layer[height-b+1]:sub(a,a)
  216.             end
  217.             layer[a] = l
  218.             l = ""
  219.         end
  220.         length = blocks[block].height
  221.         height = blocks[block].length
  222.     elseif rotation == 2 then
  223.         l = ""
  224.         for a = 1,height do
  225.             for b = 1,length do
  226.                 l = l..blocks[block].layer[a]:sub(length-b+1,length-b+1)
  227.             end
  228.             layer[height-a+1] = l
  229.             l = ""
  230.         end
  231.     elseif rotation == 3 then
  232.         l = ""
  233.         for a = 1,length do
  234.             for b = 1,height do
  235.                 l = l..blocks[block].layer[b]:sub(length-a+1,length-a+1)
  236.             end
  237.             layer[a] = l
  238.             l = ""
  239.         end
  240.         length = blocks[block].height
  241.         height = blocks[block].length
  242.     end
  243.     local canMove = true
  244.     --moves block
  245.     if direction == 1 then
  246.         for a = 1,height do
  247.             for b = 1,length do
  248.                 if x+length > 10 then
  249.                     canMove = false
  250.                 elseif layer[a]:sub(b,b) == "x" and board[y+a-1]:sub(x+b,x+b) == "x" and layer[a]:sub(b+1,b+1) ~= "x" then
  251.                     canMove = false
  252.                 end
  253.             end
  254.         end
  255.         if canMove then
  256.             removeBlock(block,x,y,rotation)
  257.             drawBlock(block,x+1,y,rotation,true)
  258.         end
  259.     elseif direction == 2 then
  260.         for a = 1,height do
  261.             for b = 1,length do
  262.                 if height+y > 16 then
  263.                     canMove = false
  264.                 elseif layer[a]:sub(b,b) == "x" and board[y+a]:sub(x+b-1,x+b-1) == "x" and layer[a+1]:sub(b,b) ~= "x" then
  265.                     canMove = false
  266.                 end
  267.             end
  268.         end
  269.         if canMove then
  270.             removeBlock(block,x,y,rotation)
  271.             drawBlock(block,x,y+1,rotation,false)
  272.         end
  273.     elseif direction == 3 then
  274.         for a = 1,height do
  275.             for b = 1,length do
  276.                 if x < 2 then
  277.                     canMove = false
  278.                 elseif layer[a]:sub(b,b) == "x" and board[y+a-1]:sub(x-b,x-b) == "x" and layer[a]:sub(b-1,b-1) ~= "x" then
  279.                     canMove = false
  280.                 end
  281.             end
  282.         end
  283.         if canMove then
  284.             removeBlock(block,x,y,rotation)
  285.             drawBlock(block,x-1,y,rotation)
  286.         end
  287.     end
  288.     moving = false
  289.     return canMove
  290. end
  291.  
  292. function drawNextBlock(block)
  293.     term.setBackgroundColor(colors.white)
  294.     term.setTextColor(colors.black)
  295.     term.setCursorPos(19,14)
  296.     write("Next")
  297.     term.setCursorPos(20,15)
  298.     term.setBackgroundColor(colors.white)
  299.     write("    ")
  300.     term.setCursorPos(20,16)
  301.     write("    ")
  302.     drawBlock(block,17,13,0,true)
  303. end
  304.  
  305. function drawBoard()
  306.     for a = 1,16 do
  307.         term.setCursorPos(32,2+a)
  308.         write(board[a])
  309.     end
  310. end
  311.  
  312. moving = false
  313. term.setBackgroundColor(colors.white)
  314. term.clear()
  315. term.setBackgroundColor(colors.black)
  316. term.setCursorPos(1,19)
  317. term.clearLine()
  318. paintutils.drawImage(outline,1,1)
  319. running = true
  320. currentBlock = math.random(1,7)
  321. nextBlock = math.random(1,7)
  322. drawNextBlock(nextBlock)
  323. drawBlock(currentBlock,4,1,0,false)
  324. local currentBlockPos = {4,1}
  325. local currentBlockRotation = 0
  326. os.startTimer(0.5)
  327. repeat
  328.     drawScore()
  329.     local e,button,x,y = os.pullEvent()
  330.     if e == "timer" and not moving then
  331.         if moveBlock(currentBlock,currentBlockPos[1],currentBlockPos[2],currentBlockRotation,2) then
  332.             currentBlockPos = {currentBlockPos[1],currentBlockPos[2]+1}
  333.         else
  334.             score = score + 2
  335.             for a = 1,16 do
  336.                 checkRow(a)
  337.             end
  338.             currentBlock = nextBlock
  339.             nextBlock = math.random(1,7)
  340.             drawNextBlock(nextBlock)
  341.             drawBlock(currentBlock,4,1,0,false)
  342.             currentBlockPos = {4,1}
  343.             currentBlockRotation = 0
  344.         end
  345.         drawBoard()
  346.         os.startTimer(0.5)
  347.     elseif e == "key" then
  348.         key = keys.getName(button)
  349.         if key == "left" then
  350.             if moveBlock(currentBlock,currentBlockPos[1],currentBlockPos[2],currentBlockRotation,3) then
  351.             currentBlockPos[1] = currentBlockPos[1] - 1
  352.             end
  353.         elseif key == "right" then
  354.             if moveBlock(currentBlock,currentBlockPos[1],currentBlockPos[2],currentBlockRotation,1) then
  355.             currentBlockPos[1] = currentBlockPos[1] + 1
  356.             end
  357.         end
  358.     end
  359. until not running
Advertisement
Add Comment
Please, Sign In to add comment