Advertisement
Guest User

tetris.lua

a guest
Oct 8th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.16 KB | None | 0 0
  1. local gpu=require("component").gpu
  2. local event=require("event")
  3.  
  4. fieldWidth=20
  5. fieldHeight=26
  6.  
  7. width=30 --width of mas NOT field
  8.  
  9. mas={}
  10.  
  11. StockBlocks = { { start_position = { x = 4, y = 1 },
  12.                   color = { 175, 175, 0 },
  13.                   symbol = "T",
  14.                   transformations = { { { x = -1, y = 0 },
  15.                                         { x = 0, y = 0 },
  16.                                         { x = 1, y = 0 },
  17.                                         { x = 0, y = -1 } },
  18.                                       { { x = 0, y = 0 },
  19.                                         { x = 0, y = -1 },
  20.                                         { x = 0, y = 1 },
  21.                                         { x = 1, y = 0 } },
  22.                                       { { x = -1, y = 0 },
  23.                                         { x = 0, y = 0 },
  24.                                         { x = 1, y = 0 },
  25.                                         { x = 0, y = 1 } },
  26.                                       { { x = 0, y = 0 },
  27.                                         { x = 0, y = -1 },
  28.                                         { x = 0, y = 1 },
  29.                                         { x = -1, y = 0 } } } },
  30.                 { start_position = { x = 4, y = 1 },
  31.                   color = { 0, 255, 0 },
  32.                   symbol = "Z",
  33.                   transformations = { { { x = -1, y = 0 },
  34.                                         { x = 0, y = 0 },
  35.                                         { x = 0, y = -1 },
  36.                                         { x = 1, y = -1 } },
  37.                                       { { x = 0, y = -1 },
  38.                                         { x = 0, y = 0 },
  39.                                         { x = 1, y = 0 },
  40.                                         { x = 1, y = 1 } } } },
  41.                 { start_position = { x = 4, y = 1 },
  42.                   color = { 100, 100, 175 },
  43.                   symbol = "S",
  44.                   transformations = { { { x = 1, y = 0 },
  45.                                         { x = 0, y = 0 },
  46.                                         { x = 0, y = -1 },
  47.                                         { x = -1, y = -1 } },
  48.                                       { { x = 0, y = 1 },
  49.                                         { x = 0, y = 0 },
  50.                                         { x = 1, y = 0 },
  51.                                         { x = 1, y = -1 } } } },
  52.                 { start_position = { x = 4, y = 0 },
  53.                   color = { 255, 255, 100 },
  54.                   symbol = "J",
  55.                   transformations = { { { x = -1, y = 0 },
  56.                                         { x = 0, y = 0 },
  57.                                         { x = 1, y = 0 },
  58.                                         { x = -1, y = 1 } },
  59.                                       { { x = 0, y = -1 },
  60.                                         { x = 0, y = 0 },
  61.                                         { x = 0, y = 1 },
  62.                                         { x = 1, y = 1 } },
  63.                                       { { x = -1, y = 0 },
  64.                                         { x = 0, y = 0 },
  65.                                         { x = 1, y = 0 },
  66.                                         { x = 1, y = -1 } },
  67.                                       { { x = 0, y = -1 },
  68.                                         { x = 0, y = 0 },
  69.                                         { x = 0, y = 1 },
  70.                                         { x = -1, y = -1 } } } },
  71.                 { start_position = { x = 4, y = 0 },
  72.                   color = { 255, 0, 255 },
  73.                   symbol = "L",
  74.                   transformations = { { { x = -1, y = 0 },
  75.                                         { x = 0, y = 0 },
  76.                                         { x = 1, y = 0 },
  77.                                         { x = 1, y = 1 } },
  78.                                       { { x = 0, y = -1 },
  79.                                         { x = 0, y = 0 },
  80.                                         { x = 0, y = 1 },
  81.                                         { x = 1, y = -1 } },
  82.                                       { { x = -1, y = 0 },
  83.                                         { x = 0, y = 0 },
  84.                                         { x = 1, y = 0 },
  85.                                         { x = -1, y = -1 } },
  86.                                       { { x = 0, y = -1 },
  87.                                         { x = 0, y = 0 },
  88.                                         { x = 0, y = 1 },
  89.                                         { x = -1, y = 1 } } } },
  90.                 { start_position = { x = 4, y = 0 },
  91.                   color = { 255, 0, 0 },
  92.                   symbol = "I",
  93.                   transformations = { { { x = -1, y = 0 },
  94.                                         { x = 0, y = 0 },
  95.                                         { x = 1, y = 0 },
  96.                                         { x = 2, y = 0 } },
  97.                                       { { x = 0, y = -1 },
  98.                                         { x = 0, y = 0 },
  99.                                         { x = 0, y = 1 },
  100.                                         { x = 0, y = 2 } } } },
  101.                 { start_position = { x = 4, y = 0 },
  102.                   color = { 0, 0, 255 },
  103.                   symbol = "[]",
  104.                   transformations = { { { x = 0, y = 0 },
  105.                                         { x = 0, y = 1 },
  106.                                         { x = 1, y = 0 },
  107.                                         { x = 1, y = 1 } } } }
  108.              }
  109.  
  110. function toXY(num,wid)
  111.   wid=wid or width
  112.   y=math.floor((num-1)/wid)+1
  113.   return num-(y-1)*width,y
  114. end
  115. function fromXY(x,y,wid)
  116.   wid=wid or width
  117.   return (y-1)*wid+x
  118. end
  119.  
  120. for i=-1000,2500 do
  121.   mas[i]=0
  122. end
  123. --print(StockBlocks[1].transformations[1][1].y)
  124. --[[f=7
  125. tr=1
  126. for i=1,4 do
  127.   gpu.set(StockBlocks[f].transformations[tr][i].x+5,StockBlocks[f].transformations[tr][i].y+5,"!")
  128. end]]--
  129. obj={
  130.   id=-1,
  131.   x=-1,
  132.   y=-1,
  133.   dX=0,
  134.   dY=0,
  135.   dR=0,
  136.   rotate=-1,
  137. }
  138. function newTile(tile)
  139.   tile=tile or (math.random(1,7))
  140.   obj.id=tile
  141.   obj.x,obj.y=StockBlocks[tile].start_position.x+5,StockBlocks[tile].start_position.y
  142.   obj.rotate=1
  143.   if test() then
  144.     gpu.setForeground(0xff0000)
  145.     gpu.set(10,1,"GAME OVER")
  146.     os.sleep(0.5)
  147.     gpu.setForeground(0xffffff)
  148.     isRunning=false
  149.   else
  150.     clearLines()
  151.   end
  152. --  print("!")
  153. end
  154. function drawBlock(x,y,color)
  155.   gpu.set((x-1)*2+1,y,"  ")
  156. end
  157. function drawTile(color)
  158.   gpu.setBackground(color or 0x005500)
  159. --  print(obj.id,obj.rotate)
  160.   for i=1,4 do
  161.     drawBlock(StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y)
  162.   end
  163.   gpu.setBackground(0x000000)
  164. end
  165. function testLine(line)
  166.   for x=1,fieldWidth-1 do
  167.     if mas[fromXY(x,line)]==0 then
  168.       return false
  169.     end
  170.   end
  171.   return true
  172. end
  173. function clearLines()
  174. --  gpu.set(90,1,"!")
  175.   for i=1,fieldHeight do
  176.     if testLine(i) then
  177.       downTiles(i)
  178. --      gpu.set(90,2,"!")
  179.       gpu.set(1,i,string.rep(" ",fieldWidth*2-2))
  180.       os.sleep(0.5)
  181.       gpu.copy(1,i-20,fieldWidth*2-1,20,0,1)
  182.     end
  183.   end
  184. end
  185. function downTiles(line)
  186.   for i=line,1,-1 do
  187.     for j=1,fieldWidth+2 do
  188.       mas[fromXY(j,i)]=mas[fromXY(j,i-1)]
  189.     end
  190.   end
  191. end
  192. function test()
  193.   for i=1,4 do
  194.     local x,y=StockBlocks[obj.id].transformations[obj.rotate][i].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][i].y+obj.y
  195.     if (y==fieldHeight)or(mas[fromXY(x,y)]~=0)or(x>=fieldWidth)or(x<1) then
  196.       return true
  197.     end
  198.   end
  199.   return false
  200. end
  201. function putInMap()
  202.   for j=1,4 do
  203.     mas[fromXY(StockBlocks[obj.id].transformations[obj.rotate][j].x+obj.x,StockBlocks[obj.id].transformations[obj.rotate][j].y+obj.y)]=1
  204.   end
  205. end
  206. function mapUpdate()
  207.   drawTile(0x000000)
  208.   if step2 then
  209.     obj.y=obj.y+1
  210.   end
  211. --  obj.x=obj.x+obj.dX
  212.   if (test())and(step2) then
  213. --    obj.x=obj.x-obj.dX
  214.       obj.y=obj.y-1
  215.     drawTile()
  216.     putInMap()
  217.     newTile()
  218.   else
  219.     if obj.dY==1 then
  220.       drawTile(0x000000)
  221.       obj.y=obj.y+1
  222.       if test() then
  223.         obj.y=obj.y-1
  224.         drawTile()
  225.         putInMap()
  226.         newTile()
  227.       end
  228.     end
  229.     if obj.dX~=0 then
  230.       drawTile(0x000000)
  231.       obj.x=obj.x+obj.dX
  232.       if test() then
  233.         obj.x=obj.x-obj.dX
  234.       end
  235.     end
  236.     if obj.dR~=0 then
  237.       local tmp=obj.rotate
  238.       drawTile(0x000000)
  239.       obj.rotate=(obj.rotate+#StockBlocks[obj.id].transformations)%(#StockBlocks[obj.id].transformations)+1
  240.       if test() then
  241.         obj.rotate=tmp
  242.       end
  243. --      obj.rotate=obj.rotate+1
  244.     end
  245.   end
  246.   obj.dX=0
  247.   obj.dY=0
  248.   obj.dR=0
  249.   drawTile()
  250. end
  251. function onKey(ev,_,code1,code2,player)
  252.   if (code1==113)and(code2==16) then
  253.     isRunning=false
  254.   elseif (code1==0) then
  255.     if code2==203 then--left
  256.       obj.dX=-1
  257.     elseif code2==200 then--up
  258.       obj.dR=1
  259.     elseif code2==205 then--right
  260.       obj.dX=1
  261.     elseif code2==208 then--down
  262.       obj.dY=1
  263.     end
  264.   end
  265. end
  266. newTile()
  267. drawTile()
  268. isRunning=true
  269. event.ignore("key_down",onKey)
  270. event.listen("key_down",onKey)
  271. step2=false
  272. gpu.setBackground(0x222222)
  273. require("term").clear()
  274. gpu.setBackground(0x000000)
  275. gpu.fill(1,1,fieldWidth*2-2,fieldHeight-1," ")
  276. while isRunning do
  277.   step2=not step2
  278.   os.sleep(0.15)
  279.   mapUpdate()
  280. --  for i=1,fieldWidth do
  281. --    gpu.set(70+i,3,tostring(mas[fromXY(i,25)]))
  282. --  end
  283. end
  284. event.ignore("key_down",onKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement