Advertisement
Eliaseeg

Snake game

Apr 27th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. local users = {"Eliaseeg", "Uriburu"}
  2. local score1 = 0
  3. local score2 = 0
  4. local ID = 1
  5. local main = {X=100,Y = 200, lenght = 2}
  6. local colors = {[0] = 0}
  7. local snakeTail = {[1] = {X = 9999, Y= 9999}}
  8. local food = {X = 999, Y = 999}
  9. local pX = main.X
  10. local pY = main.Y
  11. local dirY = 0
  12. local dirX = 0
  13. local gameOver = false
  14. local snake_up,snake_down,snake_left,snake_right = false,false,false,false
  15.  
  16. function showUI()
  17.     ui.addTextArea(0, "", nil, 05, 33, 370, 320, 0x6a7495, 0xFFFFFF, 1, true)
  18.     ui.addTextArea(1, "<font size='20'>"..users[1].." - "..score1, nil, 05, 365, 320, 100, 0x6a7495, 0xFFFFFF, 1, true)
  19.     ui.addTextArea(8888, "<font size='20'>"..users[2].." - "..score2, nil, 440, 365, 320, 100, 0x6a7495, 0xFFFFFF, 1, true)
  20.     ui.addTextArea(9999, "", nil, 387, 33, 370, 320, 0x6a7495, 0xFFFFFF, 1, true)
  21. end
  22.  
  23. function colorCell(pX,pY)
  24.     ID = ID+1
  25.     ui.addTextArea(ID, "■</font>", nil, pX, pY, 25, 25, 0, 0, 0, true)
  26.     snakeTail[ID] = {X = pX, Y= pY}
  27.     if ID >= main.lenght then
  28.         ID = 1
  29.     end
  30. end
  31.  
  32. function addFood()
  33.     food.X = math.floor(math.random()*77)*10
  34.     food.Y = math.floor(math.random()*33)*10
  35.     ui.addTextArea(99, "<font color='#F95555'>■</font>", nil, food.X, food.Y, 25, 25, 0, 0, 0, true)
  36. end
  37.  
  38. function eventLoop()
  39.     if gameOver == false then
  40.         pY = pY+dirY
  41.         pX = pX+dirX
  42.         ui.addTextArea(999, "■</font>", nil, pX, pY, 25, 25, 0, 0, 0, true)
  43.             colorCell(pX, pY)
  44.         if pX >= 790 then pX = 10
  45.             elseif pX <= 10 then pX = 790
  46.             elseif pY >= 390 then pY = 10
  47.             elseif pY <= 10 then pY = 390
  48.         end
  49.         if pX == food.X and pY == food.Y then
  50.             main.lenght = main.lenght+1
  51.             addFood()
  52.         end
  53.     end
  54. end
  55.  
  56. function eventKeyboard(player, keyCode, down, posX, posY)
  57. -- up = 73, left = 74, down = 75, right = 76
  58.     dirX = 0
  59.     dirY = 0
  60.     if keyCode == 76 and not snake_left then
  61.         dirX = 10
  62.         snake_up,snake_down,snake_left,snake_right = false,false,true,false
  63.     elseif keyCode == 74 and not snake_right then
  64.         dirX = -10
  65.         snake_up,snake_down,snake_left,snake_right = false,false,false,true    
  66.     elseif keyCode == 73 and not snake_down then
  67.         dirY = -10
  68.         snake_up,snake_down,snake_left,snake_right = false,true,false,false
  69.     elseif keyCode == 75 and not snake_up then
  70.         dirY = 10
  71.         snake_up,snake_down,snake_left,snake_right = true,false,false,false
  72.     end
  73. end
  74.  
  75. function onLoad()
  76.     tfm.exec.disableAutoShaman(true)
  77.     tfm.exec.disableAutoNewGame(true)
  78.     tfm.exec.disableAfkDeath(true)
  79.     tfm.exec.newGame('<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>')
  80.     for none, name in pairs(users) do
  81.         for i=1,100 do
  82.             tfm.exec.bindKeyboard(name, i, true, true)
  83.         end
  84.     end
  85.     showUI()
  86.     addFood()
  87. end
  88.  
  89. onLoad()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement