Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local maps = {
- '<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>'
- }
- local keys = {up = 73, left = 74, down = 75, right = 76}
- local player = "name"
- local snakeChar = "■"
- local foodChar = "■"
- local snake = {}
- local food = 0
- local bestScore = 0
- local score = 0
- function main()
- tfm.exec.disableAfkDeath(true)
- tfm.exec.disableAutoNewGame(true)
- tfm.exec.disableAutoScore(true)
- tfm.exec.disableAutoShaman(true)
- tfm.exec.disableAutoTimeLeft(true)
- newGame()
- for _,k in pairs(keys) do
- tfm.exec.bindKeyboard(player, k, true)
- end
- end
- function newGame()
- tfm.exec.newGame(maps[math.random(#maps)])
- while #snake ~= 0 do
- gameOver()
- end
- ui.removeTextArea(food)
- moveFood()
- if score > bestScore then bestScore = score end
- snake, posX, posY, dirX, dirY, score = {0, 1}, 24, 10, 1, 0, 0
- ui.addTextArea(-5, "", nil, 25, 50, 750, 300, 0, 1, 0.1, true)
- ui.addTextArea(-6, "<font size='20'>Puntaje : "..score.."</font>", nil, 25, 360, 130, 30, 0, 1, 0.2, true)
- ui.addTextArea(-7, "<font size='20'>Mejor puntaje : "..bestScore.."</font>", nil, 165, 360, 210, 30, 0, 1, 0.2, true)
- end
- function eventKeyboard (name, key, down, x, y)
- if key == keys.left and dirX ~= 1 then
- dirX, dirY = -1, 0
- elseif key == keys.up and dirY ~= 1 then
- dirX, dirY = 0, -1
- elseif key == keys.right and dirX ~= -1 then
- dirX, dirY = 1, 0
- elseif key == keys.down and dirY ~= -1 then
- dirX, dirY = 0, 1
- end
- eventLoop(0,120000)
- end
- function eventLoop (cr, left)
- newPos()
- local place = posX+50*posY
- if (not isInTable(snake, place)) and (posX >= 0) and (posX < 50) and (posY >= 0) and (posY < 20) then
- addSnake(place)
- if place == food then
- score = score+1
- ui.updateTextArea(-6, "<font size='20'>Puntaje : "..score.."</font>", nil)
- moveFood()
- else
- gameOver()
- end
- else
- newGame()
- end
- end
- function isInTable(table, element)
- for k,v in pairs(table) do
- if v == element then return true end
- end
- return false
- end
- function newPos()
- posX = posX+dirX
- posY = posY+dirY
- end
- function coord(a, b)
- return (15*a+25), (15*b+50)
- end
- function addSnake(place)
- local x, y = coord(posX, posY)
- ui.addTextArea (place, snakeChar, nil, x, y, 25, 25, 0, 0, 0, true)
- table.insert(snake, 1, place)
- end
- function gameOver()
- if snake[#snake] ~= food then
- ui.removeTextArea(snake[#snake])
- end
- table.remove(snake)
- end
- function moveFood()
- local a, b = math.random(0,49), math.random(0,19)
- food = 50*b + a
- local x, y = coord(a, b)
- ui.addTextArea (food, foodChar, nil, x, y, 25, 25, 0, 0, 0, true)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement