Advertisement
podoko_Lua

Snake [v1.0]

May 25th, 2014
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1.  
  2. --[[    Snake [v 1.0]
  3.        
  4.         Pensez à mettre votre pseudo à la place de Pseudo à la ligne 15.
  5.        
  6.         Vous pouvez choisir le caractère représentant le serpent à la liste 17 en remplaçant ■ par de caractère de votre choix
  7.         Même remarque pour le caractère représentant la cible à la ligne 18.
  8.  
  9.        
  10.         terrain 50x20 => 1000 cases
  11.  
  12. ]]--
  13.  
  14.  
  15. player = "Pseudo"
  16.  
  17. car = "■"
  18. carCible = "■"
  19.  
  20.  
  21.  --------------------------------------
  22.  --  Évitez de toucher à ce qui suit --
  23.  --------------------------------------
  24.  
  25.  
  26. serpent ={}
  27. cible = 0
  28.  
  29.  
  30. meilleurScore = 0
  31. score = 0
  32.  
  33. function init ()
  34.    
  35.     tfm.exec.disableAutoNewGame(true)
  36.     tfm.exec.newGame('<C><P /><Z><S /><D /><O /></Z></C>')
  37.  
  38.     for key = 37, 40 do
  39.         tfm.exec.bindKeyboard(player, key, true, true)
  40.     end
  41.  
  42.     newGame()
  43.    
  44. end
  45.  
  46. function newGame()
  47.    
  48.     while #serpent ~= 0 do suppS() end
  49.    
  50.     ui.removeTextArea(cible)
  51.     moveCible()
  52.     if score > meilleurScore then meilleurScore = score end
  53.     serpent, posX, posY, dirX, dirY, score = {0, 1}, 24, 10, 1, 0, 0
  54.    
  55.     ui.addTextArea(-5, "", nil, 25, 50, 750, 300, 0, 1, 0.1, true)
  56.     ui.addTextArea(-6, "<font size='20'>score : "..score.."</font>", nil, 25, 360, 130, 30, 0, 1, 0.2, true)
  57.     ui.addTextArea(-7, "<font size='20'>Meilleur score : "..meilleurScore.."</font>", nil, 165, 360, 210, 30, 0, 1, 0.2, true)
  58.    
  59. end
  60.  
  61.  
  62. function eventKeyboard (name, key, down, x, y)
  63.    
  64.     if key == 37 and dirX ~= 1 then
  65.         dirX, dirY = -1, 0
  66.     elseif key == 38 and dirY ~= 1 then
  67.         dirX, dirY = 0, -1
  68.     elseif key == 39 and dirX ~= -1 then
  69.         dirX, dirY = 1, 0
  70.     elseif key == 40 and dirY ~= -1 then
  71.         dirX, dirY = 0, 1
  72.     end
  73.     eventLoop(0,120000)
  74.    
  75. end
  76.  
  77.  
  78.  
  79. function eventLoop (current, left)
  80.    
  81.     newPose()
  82.     local case = posX+50*posY
  83.  
  84.     if (not isInTable(serpent, case)) and (posX >= 0) and (posX < 50) and (posY >= 0) and (posY < 20) then
  85.        
  86.         addS(case)
  87.        
  88.         if case == cible then
  89.             score = score+1
  90.             ui.updateTextArea(-6, "<font size='20'>score : "..score.."</font>", nil)
  91.             moveCible()
  92.         else
  93.             suppS()
  94.         end
  95.        
  96.     else
  97.         newGame()
  98.     end
  99. end
  100.  
  101.  
  102.  
  103.  
  104. function isInTable(tab, elmt)
  105.    
  106.     for k,v in pairs(tab) do
  107.         if v == elmt then return true end
  108.     end
  109.     return false
  110.    
  111. end
  112.  
  113.  
  114. function newPose()
  115.     posX = posX+dirX
  116.     posY = posY+dirY
  117. end
  118.  
  119. function coord (a, b)
  120.     return (15*a+25), (15*b+50)
  121. end
  122.  
  123.  
  124.  
  125. function addS (case)
  126.    
  127.     local x, y = coord(posX, posY)
  128.    
  129.     ui.addTextArea (case, car, nil, x, y, 25, 25, 0, 0, 0, true)
  130.     table.insert(serpent, 1, case)
  131.    
  132. end
  133.  
  134.  
  135. function suppS ()
  136.    
  137.     if serpent[#serpent] ~= cible then
  138.         ui.removeTextArea(serpent[#serpent])
  139.     end
  140.     table.remove(serpent)
  141.  
  142. end
  143.  
  144. function moveCible ()
  145.    
  146.     local a, b = math.random(0,49), math.random(0,19)
  147.     cible = 50*b + a
  148.    
  149.     local x, y = coord(a, b)
  150.    
  151.     ui.addTextArea (cible, carCible, nil, x, y, 25, 25, 0, 0, 0, true)
  152.    
  153. end
  154.  
  155.  
  156. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement