Advertisement
podoko_Lua

Tetris [v1.0]

Jul 18th, 2014
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. --[[ Tetris [v1.0] ]]--
  2. --[[
  3.    
  4.     commande pour lancer la partie : !Tetris L H
  5.     (L et H sont respectivement la longueur et la hauteur du terrain (15 et 25 par défaut)
  6.    
  7. ]]--
  8.  
  9.  --------------------------------------
  10.  --  Évitez de toucher à ce qui suit --
  11.  --------------------------------------
  12.  
  13.  
  14.  
  15. ig = false
  16.  
  17. grid = {}
  18. pos = {}
  19.  
  20. color = { "#00f0ef", "#0000f0", "#f09f00", "#f1f000", "#01ef00", "#9e00f0", "#f00001" }
  21.  
  22.  
  23.  
  24. function new_game(name, l, h)
  25.    
  26.     L, H, admin = l, h, name
  27.    
  28.     cen = math.ceil(L/2)
  29.     dec_x = math.floor((80-L)*5)
  30.    
  31.     grid = {}
  32.     for x=1,L do
  33.         grid[x] = {}
  34.         for y=1,H do
  35.             grid[x][y] = {"empty"}
  36.         end
  37.     end
  38.    
  39.     tfm.exec.bindKeyboard(admin,37, true, true)
  40.     tfm.exec.bindKeyboard(admin,38, true, true)
  41.     tfm.exec.bindKeyboard(admin,39, true, true)
  42.     tfm.exec.bindKeyboard(admin,40, true, true)
  43.    
  44.     score = 0
  45.    
  46.     ui.addTextArea(-1, "", nil, dec_x+10, 30, 10*L, 10*(H+1), 1, 1, 1, true)
  47.     ui.addTextArea(-2, "<font color='#ffffff'>Score : 0</font>", nil, dec_x+10, 30, 100, 20, 1, 1, 0, true)
  48.     ui.addTextArea(-3, "", nil, dec_x+10*(L-5), 30, 60, 40, 1, 1, 0, true)
  49.    
  50.    
  51.     current_p, next_p = math.random(7), math.random(7)
  52.     place(current_p)
  53.    
  54.     show(next_p)
  55.    
  56. end
  57.  
  58.  
  59.  
  60. function eventLoop(past, left)
  61.    
  62.     if ig then
  63.         if possible(0,1) then
  64.             move(0,1)
  65.         else
  66.             find_line(H)
  67.             current_p = next_p
  68.            
  69.             place(current_p)
  70.             next_p = math.random(7)
  71.             show(next_p)
  72.         end
  73.     end
  74.    
  75. end
  76.  
  77.  
  78.  
  79. function eventKeyboard(name, key, down)
  80.    
  81.     if key == 37 then
  82.         if possible(-1,0) then move(-1,0) end
  83.     elseif key == 39 then
  84.         if possible(1,0) then move(1,0) end
  85.     elseif key == 40 then
  86.         if possible(0,1) then move(0,1) end
  87.     elseif key == 38 then
  88.         rotation()
  89.     end
  90.    
  91. end
  92.  
  93.  
  94.  
  95. function show(piece)
  96.    
  97.     local str = ""
  98.     if piece ==1 then str = "■■■■"
  99.     elseif piece == 2 then str = "■■■\n   ■"
  100.     elseif piece == 3 then str = "■■■\n■"
  101.     elseif piece == 4 then str = "■■\n■■"
  102.     elseif piece == 5 then str = "  ■■\n■■"
  103.     elseif piece == 6 then str = "■■■\n    ■"
  104.     elseif piece == 7 then str = "■■\n  ■■"
  105.     end
  106.    
  107.     ui.updateTextArea(-3, "<font color='"..color[next_p].."'>"..str.."</font")
  108.    
  109. end
  110.  
  111.  
  112.  
  113. function move(x,y)
  114.    
  115.     for _, val in ipairs(pos) do
  116.         ui.removeTextArea(L*val.y+val.x)
  117.         grid[val.x][val.y] = {"empty"}
  118.     end
  119.    
  120.     for key, val in ipairs(pos) do
  121.         pos[key] = {x=val.x+x, y=val.y+y}
  122.         ui.addTextArea(L*(val.y+y)+(val.x+x), "<font color='"..color[current_p].."'>■</font>", nil, dec_x+10*(val.x+x), 30+10*(val.y+y), 20, 20, 1, 1, 0, true)
  123.         grid[val.x+x][val.y+y] = {"moving", current_p}
  124.     end
  125.    
  126. end
  127.  
  128.  
  129. function possible(x,y)
  130.    
  131.     for _,coord in ipairs(pos) do
  132.         if coord.x+x > L or coord.x+x < 1 or coord.y+y > H or grid[coord.x+x][coord.y+y][1] == "fixed" then
  133.            
  134.             if x==0 and y==1 then
  135.                 for _, val in pairs(pos) do
  136.                     grid[val.x][val.y] = {"fixed", current_p}
  137.                 end
  138.                  
  139.             end
  140.             return false
  141.         end
  142.     end
  143.    
  144.     return true
  145. end
  146.  
  147.  
  148. function find_line(line)
  149.    
  150.     local complet = true
  151.     for i=1, L do
  152.         if grid[i][line][1] ~= "fixed" then
  153.             complet = false
  154.             break
  155.         end
  156.     end
  157.     if complet then
  158.         local delete = ui.removeTextArea
  159.         local show = ui.addTextArea
  160.        
  161.         for x=1, L do
  162.             if grid[x][line][1] ~= "empty" then
  163.                 delete(x+L*line)
  164.                 grid[x][line] = {"empty"}
  165.             end
  166.         end
  167.        
  168.        
  169.         for l=line-1, 1, -1 do
  170.             for x=1, L do
  171.                 if grid[x][l][1] ~= "empty" then
  172.                     show(x+(l+1)*L, "<font color='"..color[grid[x][l][2]].."'>■</font>", nil, dec_x+10*x, 30+10*(l+1), 20, 20, 1, 1, 0, true)
  173.                     grid[x][l+1] = {grid[x][l][1], grid[x][l][2]}
  174.                     delete(x+L*l)
  175.                     grid[x][l][1] = "empty"
  176.                 end
  177.             end
  178.         end
  179.        
  180.        
  181.         score = score + L
  182.         ui.updateTextArea(-2, "<font color='#ffffff'>Score : "..score.."</font>")
  183.        
  184.         find_line(line)
  185.     elseif line > 1 then
  186.         find_line(line-1)
  187.     end
  188.    
  189. end
  190.  
  191.  
  192.  
  193. function place(piece)
  194.    
  195.     if piece == 1 then
  196.         pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen+2,y=1}}
  197.     elseif piece == 2 then
  198.         pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen+1,y=2}}
  199.     elseif piece == 3 then
  200.         pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen-1,y=2}}
  201.     elseif piece == 4 then
  202.         pos = {{x=cen,y=2}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen+1,y=2}}
  203.     elseif piece == 5 then
  204.         pos = {{x=cen,y=2}, {x=cen,y=1}, {x=cen-1,y=2}, {x=cen+1,y=1}}
  205.     elseif piece == 6 then
  206.         pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen,y=2}}
  207.     elseif piece == 7 then
  208.         pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=2}, {x=cen,y=2}}
  209.     end
  210.    
  211. end
  212.  
  213.  
  214.  
  215. function rotation()
  216.    
  217.     local x, y = pos[2].x, pos[2].y
  218.     local poss = true
  219.    
  220.     for _,v in ipairs(pos) do
  221.         if not free_pos(x-(y-v.y), y+(x-v.x))
  222.             then poss = false
  223.             break
  224.         end
  225.     end
  226.    
  227.     if poss then
  228.         local delete = ui.removeTextArea
  229.         local show = ui.addTextArea
  230.        
  231.         for key, val in ipairs(pos) do
  232.             grid[val.x][val.y] = {"empty"}
  233.             delete(val.y*L+val.x)
  234.             local a, b = x-(y-val.y), y+(x-val.x)
  235.             pos[key] = {x=a,y=b}
  236.         end
  237.         for _,val in ipairs(pos) do
  238.             grid[val.x][val.y] = {"moving",current_p}
  239.             show(val.x+val.y*L, "<font color='"..color[current_p].."'>■</font>", nil, dec_x+10*val.x, 30+10*val.y, 20, 20, 1, 1, 0, true)
  240.         end
  241.    
  242.     end
  243.    
  244. end
  245.  
  246.  
  247.  
  248.  
  249. function free_pos(x, y)
  250.     if x>0 and x<=L and y<=H and y>0 and grid[x][y][1] ~= "fixed" then
  251.         return true
  252.     else
  253.         return false
  254.     end
  255. end
  256.  
  257. function eventChatCommand(name, command)
  258.    
  259.     if not ig then
  260.         local lw = {}
  261.         for w in command:gmatch('%S+') do table.insert(lw, w) end
  262.        
  263.         if lw[1]=="Tetris" or lw[1] == "tetris" then
  264.             new_game(name, tonumber(lw[2]) or 15, tonumber(lw[3]) or 25)
  265.         end
  266.         ig = true
  267.     end
  268.    
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement