Advertisement
Guest User

tetris.lua

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