Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Snake [v 1.0]
- Pensez à mettre votre pseudo à la place de Pseudo à la ligne 15.
- Vous pouvez choisir le caractère représentant le serpent à la liste 17 en remplaçant ■ par de caractère de votre choix
- Même remarque pour le caractère représentant la cible à la ligne 18.
- terrain 50x20 => 1000 cases
- ]]--
- player = "Pseudo"
- car = "■"
- carCible = "■"
- --------------------------------------
- -- Évitez de toucher à ce qui suit --
- --------------------------------------
- serpent ={}
- cible = 0
- meilleurScore = 0
- score = 0
- function init ()
- tfm.exec.disableAutoNewGame(true)
- tfm.exec.newGame('<C><P /><Z><S /><D /><O /></Z></C>')
- for key = 37, 40 do
- tfm.exec.bindKeyboard(player, key, true, true)
- end
- newGame()
- end
- function newGame()
- while #serpent ~= 0 do suppS() end
- ui.removeTextArea(cible)
- moveCible()
- if score > meilleurScore then meilleurScore = score end
- serpent, 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'>score : "..score.."</font>", nil, 25, 360, 130, 30, 0, 1, 0.2, true)
- ui.addTextArea(-7, "<font size='20'>Meilleur score : "..meilleurScore.."</font>", nil, 165, 360, 210, 30, 0, 1, 0.2, true)
- end
- function eventKeyboard (name, key, down, x, y)
- if key == 37 and dirX ~= 1 then
- dirX, dirY = -1, 0
- elseif key == 38 and dirY ~= 1 then
- dirX, dirY = 0, -1
- elseif key == 39 and dirX ~= -1 then
- dirX, dirY = 1, 0
- elseif key == 40 and dirY ~= -1 then
- dirX, dirY = 0, 1
- end
- eventLoop(0,120000)
- end
- function eventLoop (current, left)
- newPose()
- local case = posX+50*posY
- if (not isInTable(serpent, case)) and (posX >= 0) and (posX < 50) and (posY >= 0) and (posY < 20) then
- addS(case)
- if case == cible then
- score = score+1
- ui.updateTextArea(-6, "<font size='20'>score : "..score.."</font>", nil)
- moveCible()
- else
- suppS()
- end
- else
- newGame()
- end
- end
- function isInTable(tab, elmt)
- for k,v in pairs(tab) do
- if v == elmt then return true end
- end
- return false
- end
- function newPose()
- posX = posX+dirX
- posY = posY+dirY
- end
- function coord (a, b)
- return (15*a+25), (15*b+50)
- end
- function addS (case)
- local x, y = coord(posX, posY)
- ui.addTextArea (case, car, nil, x, y, 25, 25, 0, 0, 0, true)
- table.insert(serpent, 1, case)
- end
- function suppS ()
- if serpent[#serpent] ~= cible then
- ui.removeTextArea(serpent[#serpent])
- end
- table.remove(serpent)
- end
- function moveCible ()
- local a, b = math.random(0,49), math.random(0,19)
- cible = 50*b + a
- local x, y = coord(a, b)
- ui.addTextArea (cible, carCible, nil, x, y, 25, 25, 0, 0, 0, true)
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement