Advertisement
Nike622000

Tetris

Nov 12th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.65 KB | None | 0 0
  1. shape = {}
  2. shape[1] = {{0,0,0,0,0},
  3.             {0,0,1,0,0},
  4.             {0,0,1,0,0},
  5.             {0,0,1,0,0},
  6.             {0,0,1,0,0}}
  7.            
  8. shape[2] = {{0,0,0,0,0},
  9.             {0,0,1,0,0},
  10.             {0,0,1,0,0},
  11.             {0,0,1,0,0},
  12.             {0,1,1,0,0}}
  13.            
  14. shape[3] = {{0,0,0,0,0},
  15.             {0,0,1,0,0},
  16.             {0,0,1,0,0},
  17.             {0,0,1,0,0},
  18.             {0,0,1,1,0}}
  19.        
  20. shape[4] = {{0,0,0,0,0},
  21.             {0,0,0,0,0},
  22.             {0,0,1,1,0},
  23.             {0,1,1,0,0},
  24.             {0,0,0,0,0}}
  25.            
  26. shape[5] = {{0,0,0,0,0},
  27.             {0,0,0,0,0},
  28.             {0,1,1,0,0},
  29.             {0,0,1,1,0},
  30.             {0,0,0,0,0}}
  31.            
  32. shape[6] = {{0,0,0,0,0},
  33.             {0,0,0,0,0},
  34.             {0,0,1,0,0},
  35.             {0,1,1,1,0},
  36.             {0,0,0,0,0}}
  37.            
  38. shape[7] = {{0,0,0,0,0},
  39.             {0,0,0,0,0},
  40.             {0,1,1,0,0},
  41.             {0,1,1,0,0},
  42.             {0,0,0,0,0}}  
  43.  
  44. local xMin = 2
  45. local yMin = 1
  46.  
  47. local yL   = 4
  48.  
  49. local xMax = 23
  50. local yMax = 18
  51.  
  52. local field = {}
  53.  
  54. function createField()
  55.   for y = yMin,yMax do
  56.     field[y] = {}
  57.     for x = xMin,xMax do
  58.       val = colors.black
  59.       if y == yMax or x == xMin or x == xMax then
  60.         val = colors.gray  
  61.       end
  62.       field[y][x] = val
  63.     end
  64.   end
  65. end
  66.  
  67. function printField()
  68.   for y = yMin,yMax do
  69.     for x = xMin,xMax do
  70.       term.setBackgroundColor(field[y][x])
  71.       term.setCursorPos(x,y)
  72.       term.write(" ")
  73.     end
  74.   end
  75. end
  76.  
  77. function checkCollision(num,dir,xP,yP)
  78.   for y = 1,#shape[num] do
  79.     for x = 1,#shape[num][y] do
  80.       if dir == 1 then
  81.         val = shape[num][y][x]
  82.       elseif dir == 2 then
  83.         val = shape[num][x][y]
  84.       elseif dir == 3 then
  85.         val = shape[num][#shape[num]+1-y][#shape[num][y]+1-x]
  86.       elseif dir == 4 then
  87.         val = shape[num][#shape[num][y]+1-x][#shape[num]+1-y]
  88.       end
  89.       if val == 1 then
  90.         if field[yMin+y+yP-2][xMin+x+xP-2] ~= colors.black then
  91.           return true
  92.         end
  93.       end
  94.     end
  95.   end
  96. end
  97.  
  98. function displayShape(num,dir,xP,yP)
  99.   for y = 1,#shape[num] do
  100.     for x = 1,#shape[num][y] do
  101.       if dir == 1 then
  102.         val = shape[num][y][x]
  103.       elseif dir == 2 then
  104.         val = shape[num][x][y]
  105.       elseif dir == 3 then
  106.         val = shape[num][#shape[num]+1-y][#shape[num][y]+1-x]
  107.       elseif dir == 4 then
  108.         val = shape[num][#shape[num][y]+1-x][#shape[num]+1-y]
  109.       end
  110.       if val == 1 then
  111.         if field[yMin+y+yP-2][xMin+x+xP-2] == colors.black then              
  112.           field[yMin+y+yP-2][xMin+x+xP-2] = colors.blue
  113.         end          
  114.       end
  115.     end
  116.   end
  117. end
  118.  
  119. function clearShape()
  120.   for yP = yMin,yMax do
  121.     for xP = xMin,xMax do
  122.       if field[yP][xP] == colors.blue then
  123.         field[yP][xP] = colors.black
  124.       end
  125.     end
  126.   end
  127. end
  128.  
  129. function moveArray()
  130.   for yP = yMin,yMax do
  131.     for xP = xMin,xMax do
  132.       if field[yP][xP] == colors.blue then
  133.         if yP <= yMin+yL then
  134.           return true
  135.         else
  136.           field[yP][xP] = colors.red
  137.         end
  138.       end
  139.     end
  140.   end
  141. end
  142.  
  143. function checkLine()
  144.   for yP = yMin,yMax do
  145.     stat = true
  146.     for xP = xMin+1,xMax-1 do
  147.       if field[yP][xP] ~= colors.red then
  148.         stat = false
  149.       end
  150.     end
  151.     if stat == true then
  152.       for y = yP,yMin+1,-1 do
  153.         for xP = xMin+1,xMax-1 do
  154.           field[y][xP] = field[y-1][xP]
  155.         end
  156.       end
  157.       for xP = xMin+1,xMax+1 do
  158.         field[yMin][xP] = colors.black
  159.       end
  160.     end
  161.   end
  162. end
  163.  
  164. function printyL()
  165.   term.setBackgroundColor(colors.lime)
  166.   for xP = xMin+1,xMax-1 do
  167.     term.setCursorPos(xP,yL+yMin)
  168.     term.write(" ")
  169.   end
  170. end
  171.  
  172. createField()
  173.  
  174. local xP  = math.floor(xMax/2)
  175. local yP  = yMin
  176. local dir = 1
  177. local s = math.random(1,7)
  178.  
  179. while true do
  180.   event,key = os.pullEvent()
  181.   clearShape()
  182.   if key == keys.q then
  183.     dir = dir+1
  184.     if dir > 4 then
  185.       dir = 1
  186.     end
  187.     if checkCollision(s,dir,xP,yP) then
  188.       dir = dir-1
  189.       if dir < 1 then
  190.         dir = 4
  191.       end
  192.     end
  193.   elseif key == keys.left then
  194.     xP = xP-1
  195.     if checkCollision(s,dir,xP,yP) then
  196.       xP = xP+1
  197.     end
  198.   elseif key == keys.right then
  199.     xP = xP+1
  200.     if checkCollision(s,dir,xP,yP) then
  201.       xP = xP-1
  202.     end
  203.   else
  204.     yP = yP+1
  205.     if checkCollision(s,dir,xP,yP) then
  206.       displayShape(s,dir,xP,yP-1)
  207.       if moveArray() then
  208.         break
  209.       end
  210.       checkLine()
  211.       s = math.random(1,7)
  212.       yP = yMin
  213.       xP = math.floor(xMax/2)
  214.     end
  215.   end
  216.   displayShape(s,dir,xP,yP)
  217.   printField()
  218.   printyL()
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement