Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- black=Color.new(0,0,0)
- white=Color.new(255,255,255)
- green=Color.new(0,255,0)
- red=Color.new(255,0,0)
- yellow=Color.new(255,255,0)
- function message(text, choice)
- ok = true
- pad = Controls.read()
- oldpad = pad
- while 1 do
- screen:clear()
- pad = Controls.read()
- screen:print(100,100,text,white)
- if choice then
- screen:print(100,140,"Yes",ok and red or white)
- screen:print(140,140,"No",not(ok) and red or white)
- if (pad:left() and not oldpad:left()) or (pad:right() and not oldpad:right()) then
- ok = not(ok)
- end
- end
- if pad:circle() and not oldpad:circle() then
- ok = false
- break
- elseif pad:cross() and not oldpad:cross() then
- break
- end
- oldpad= pad
- screen.waitVblankStart()
- screen.flip()
- end
- return ok
- end
- function DoesFileExist(file) -- Fonction pour verifier l'existence d'un fichier.
- local fic = io.open("./"..file,"r")
- local exist
- if fic then
- exist = true
- io.close(fic)
- else
- exist = false
- end
- return exist
- end
- function Keyboard()
- nombre = 0
- chaine = ""
- local clavier =
- {
- {"1", "2", "3"},
- {"4", "5", "6"},
- {"7", "8", "9"}
- }
- local x=1
- local y=1
- local oldpad = Controls.read()
- while (1) do
- pad = Controls.read()
- if (pad:up() and not oldpad:up()) and (y > 1) then
- y = y - 1
- elseif (pad:down() and not oldpad:down()) and (y < 3) then
- y = y + 1
- elseif (pad:left() and not oldpad:left()) and (x > 1) then
- x = x - 1
- elseif (pad:right() and not oldpad:right()) and (x < 3) then
- x = x + 1
- end
- if pad:cross() and not oldpad:cross() then
- chaine = chaine..clavier[y][x]
- elseif pad:start() and not oldpad:start() then
- break;
- elseif pad:square() and not oldpad:square() then
- chaine = string.sub(chaine,1, #chaine - 1)
- end
- screen:clear()--Color.new(255,255,255))
- for i = 1, 3 do
- for j = 1, 3 do
- screen:print(j*41+20,i*31+20,clavier[i][j],(j==x and i==y) and red or white)
- end
- end
- screen:print(272-chaine:len()*4.5,2,chaine,white);
- oldpad = pad;
- screen.waitVblankStart()
- screen.flip()
- end
- ToucheOFF = nil
- ToucheON = nil
- x=nil
- collectgarbage()
- return tonumber(chaine);
- end
- function ChooseContent()
- local Files = System.listDirectory()
- local oldpod = Controls.read()
- local pod = oldpod
- local selected = 1
- local i = 1
- table.remove(Files, 1)
- table.remove(Files, 1)
- while true do
- if Files[i].name:reverse():sub(0,4) ~= "TXT." then
- table.remove(Files, i)
- end
- i = i + 1
- if i > #Files then
- break
- end
- end
- while true do
- pod = Controls.read()
- screen:clear()
- if pod:down() and not oldpod:down() then
- selected = selected < #Files and selected + 1 or 1
- elseif pod:up() and not oldpod:up() then
- selected = selected > 1 and selected - 1 or #Files
- end
- if pod:cross() then
- break
- end
- for i = 1, #Files do
- screen:print(10,10*i,Files[i].name,i == selected and red or white)
- end
- screen.waitVblankStart() -- On attend que l'ecran soit en position pour afficher
- screen.flip() -- On met a jour l'ecran
- oldpod = pod
- end
- return Files[selected].name
- end
- function save(grille, name)
- if DoesFileExist("MAP-"..name..".TXT") then
- if not message("This file already exist, replace",true) then
- return
- end
- end
- file = io.open("MAP-"..name..".TXT","w")
- for i = 1, 27 do
- for j = 1, 48 do
- file:write(grille[i][j].content)
- end
- file:write("\n")
- end
- file:close()
- message("Done", false)
- end
- function printGrille(H,L,color)
- for i = 0, H do
- screen:drawLine(i*10,0,i*10,L*10,color)
- end
- for i = 0, L do
- screen:drawLine(0,i*10,H*10,i*10, color)
- end
- end
- function printBlocs(grille)
- for i = 1, #grille[1] do
- for j = 1, #grille do
- if grille[j][i].content == 1 then
- screen:fillRect(grille[j][i].x,grille[j][i].y,9,9,black)
- elseif grille[j][i].content == 2 then
- screen:fillRect(grille[j][i].x,grille[j][i].y,9,9,green)
- end
- end
- end
- end
- function Canevas(grille)
- for i = 1, 26, 2 do
- for j = 1, 48, 2 do
- grille[i][j].content = 0
- grille[i][j+1].content = 1
- end
- for j = 1, 48 do
- grille[i+1][j].content = 1
- end
- end
- for j = 1, 48, 2 do
- grille[27][j].content = 0
- grille[27][j+1].content = 1
- end
- return grille
- end
- function Editor(grille)
- x,y = 1, 1
- couleurSelecteur = red
- move = 0
- oldpad = Controls.read()
- while true do
- pad = Controls.read()
- screen:clear(white)
- printGrille(#grille[1],#grille,yellow)
- if (pad:right() and not oldpad:right() or pad:analogX() > 60) and x < #grille[1] then
- x = x + 1
- move = 1
- end
- if (pad:left() and not oldpad:left() or pad:analogX() < -60) and x > 1 then
- x = x - 1
- move =1
- end
- if (pad:down() and not oldpad:down() or pad:analogY() > 60) and y < #grille then
- y = y + 1
- move = 1
- end
- if (pad:up() and not oldpad:up() or pad:analogY() < -60) and y > 1 then
- y = y - 1
- move = 1
- end
- if pad:cross() and move == 1 then
- grille[y][x].content = grille[y][x].content == 1 and 0 or 1
- move = 0
- elseif pad:square() then
- grille[y][x].content = 2
- end
- if pad:select() and message("Apply defined canevas?",true) then
- grille=Canevas(grille)
- end
- if pad:start() and message("Save map?",true) then
- name = Keyboard()
- save(grille,name)
- end
- if pad:circle() and message("Quit editor? All unsaved changes would be deleted.",true) then break end
- printBlocs(grille)
- screen:fillRect(grille[y][x].x,grille[y][x].y,9,9,couleurSelecteur)
- screen.flip()
- oldpad = pad
- end
- end
- map = ChooseContent()
- file = io.open(map,"r")
- carte = {}
- grille = {}
- for i = 1, 27 do
- grille[i]= {}
- ligne = file:read()
- for j = 1, 48 do
- grille[i][j]={x=10*j-9,y=10*i-9,content= tonumber(string.sub(ligne,j,j))}
- end
- end
- file:close()
- x,y = 1, 1
- couleurSelecteur = Color.new(255,0,0)
- oldpad = Controls.read()
- while true do
- pad = Controls.read()
- screen:clear(white)
- printGrille(#grille[1],#grille,Color.new(0,0,0))
- if (pad:right() or pad:analogX() > 60) and x < #grille[1] and grille[y][x+1].content ~= 1 then
- x = x + 1
- elseif (pad:left() or pad:analogX() < -60) and x > 1 and grille[y][x-1].content ~= 1 then
- x = x - 1
- elseif (pad:down() or pad:analogY() > 60) and y < #grille and grille[y+1][x].content ~= 1 then
- y = y + 1
- elseif (pad:up() or pad:analogY() < -60) and y > 1 and grille[y-1][x].content ~= 1 then
- y = y - 1
- end
- if pad:cross() and not oldpad:cross() and message("Launch level editor?", true) then
- Editor(grille)
- end
- if pad:start() then
- break
- end
- if grille[y][x].content == 2 then message("Oh yeah!",false); break end
- printBlocs(grille)
- screen:fillRect(grille[y][x].x,grille[y][x].y,9,9,couleurSelecteur)
- screen.flip();
- oldpad=pad
- --System.sleep(100)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement