Advertisement
Eliaseeg

Juego de la serpiente

Jun 10th, 2014
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. local maps = {
  2. '<C><P /><Z><S><S L="10" H="48" X="145" Y="-319" T="0" P="0,0,0.3,0.2,0,0,0,0" /><S L="10" H="46" X="97" Y="-318" T="0" P="0,0,0.3,0.2,0,0,0,0" /><S L="58" H="10" X="121" Y="-338" T="0" P="0,0,0.3,0.2,0,0,0,0" /><S L="58" X="121" H="10" Y="-299" T="0" P="0,0,0.3,0.2,0,0,0,0" /></S><D><DS Y="-317" X="118" /></D><O /></Z></C>'
  3. }
  4. local keys = {up = 73, left = 74, down = 75, right = 76}
  5. local player = "name"
  6. local snakeChar = "■"
  7. local foodChar = "■"
  8. local snake = {}
  9. local food = 0
  10. local bestScore = 0
  11. local score = 0
  12.  
  13. function main()
  14.   tfm.exec.disableAfkDeath(true)
  15.   tfm.exec.disableAutoNewGame(true)
  16.   tfm.exec.disableAutoScore(true)
  17.   tfm.exec.disableAutoShaman(true)
  18.   tfm.exec.disableAutoTimeLeft(true)
  19.   newGame()
  20.   for _,k in pairs(keys) do
  21.     tfm.exec.bindKeyboard(player, k, true)
  22.   end
  23. end
  24.  
  25. function newGame()
  26. tfm.exec.newGame(maps[math.random(#maps)])
  27.  while #snake ~= 0 do
  28.    gameOver()
  29.  end
  30. ui.removeTextArea(food)
  31. moveFood()
  32.    if score > bestScore then bestScore = score end
  33.     snake, posX, posY, dirX, dirY, score = {0, 1}, 24, 10, 1, 0, 0
  34.     ui.addTextArea(-5, "", nil, 25, 50, 750, 300, 0, 1, 0.1, true)
  35.     ui.addTextArea(-6, "<font size='20'>Puntaje : "..score.."</font>", nil, 25, 360, 130, 30, 0, 1, 0.2, true)
  36.     ui.addTextArea(-7, "<font size='20'>Mejor puntaje : "..bestScore.."</font>", nil, 165, 360, 210, 30, 0, 1, 0.2, true)  
  37. end
  38.  
  39. function eventKeyboard (name, key, down, x, y)
  40.   if key == keys.left and dirX ~= 1 then
  41.     dirX, dirY = -1, 0
  42.   elseif key == keys.up and dirY ~= 1 then
  43.     dirX, dirY = 0, -1
  44.   elseif key == keys.right and dirX ~= -1 then
  45.     dirX, dirY = 1, 0
  46.   elseif key == keys.down and dirY ~= -1 then
  47.     dirX, dirY = 0, 1
  48.     end
  49.    eventLoop(0,120000)  
  50. end
  51.  
  52. function eventLoop (cr, left)
  53. newPos()
  54. local place = posX+50*posY
  55. if (not isInTable(snake, place)) and (posX >= 0) and (posX < 50) and (posY >= 0) and (posY < 20) then
  56.   addSnake(place)
  57.   if place == food then
  58.     score = score+1
  59.     ui.updateTextArea(-6, "<font size='20'>Puntaje : "..score.."</font>", nil)
  60.     moveFood()
  61.   else
  62.     gameOver()
  63.   end
  64.   else
  65.    newGame()
  66.   end
  67. end
  68.  
  69. function isInTable(table, element)
  70.   for k,v in pairs(table) do
  71.     if v == element then return true end
  72.     end
  73.     return false  
  74. end
  75.  
  76. function newPos()
  77.   posX = posX+dirX
  78.   posY = posY+dirY
  79. end
  80.  
  81. function coord(a, b)
  82.   return (15*a+25), (15*b+50)
  83. end
  84.  
  85. function addSnake(place)
  86.   local x, y = coord(posX, posY)
  87.   ui.addTextArea (place, snakeChar, nil, x, y, 25, 25, 0, 0, 0, true)
  88.   table.insert(snake, 1, place)
  89. end
  90.  
  91. function gameOver()
  92.   if snake[#snake] ~= food then
  93.     ui.removeTextArea(snake[#snake])
  94.    end
  95.     table.remove(snake)
  96. end
  97.  
  98. function moveFood()
  99.   local a, b = math.random(0,49), math.random(0,19)
  100.   food = 50*b + a      
  101.   local x, y = coord(a, b)
  102.   ui.addTextArea (food, foodChar, nil, x, y, 25, 25, 0, 0, 0, true)  
  103. end
  104.  
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement