Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.68 KB | None | 0 0
  1. black=Color.new(0,0,0)
  2. white=Color.new(255,255,255)
  3. green=Color.new(0,255,0)
  4. red=Color.new(255,0,0)
  5. yellow=Color.new(255,255,0)
  6.  
  7.      
  8. function message(text, choice)
  9.     ok = true
  10.     pad = Controls.read()
  11.     oldpad = pad
  12.     while 1 do
  13.         screen:clear()
  14.         pad = Controls.read()
  15.         screen:print(100,100,text,white)
  16.         if choice then
  17.             screen:print(100,140,"Yes",ok and red or white)
  18.             screen:print(140,140,"No",not(ok)  and red or white)
  19.        
  20.             if (pad:left() and not oldpad:left()) or (pad:right() and not oldpad:right()) then
  21.                 ok = not(ok)
  22.             end
  23.         end
  24.         if pad:circle() and not oldpad:circle() then
  25.             ok = false
  26.             break
  27.         elseif pad:cross() and not oldpad:cross() then
  28.             break
  29.         end
  30.         oldpad= pad
  31.         screen.waitVblankStart()
  32.         screen.flip()
  33.     end
  34.     return ok
  35. end
  36.  
  37. function DoesFileExist(file) -- Fonction pour verifier l'existence d'un fichier.
  38.     local fic = io.open("./"..file,"r")
  39.     local exist
  40.     if fic then
  41.         exist = true
  42.         io.close(fic)
  43.     else
  44.         exist = false
  45.     end
  46.     return exist
  47. end
  48.  
  49. function Keyboard()
  50.   nombre = 0
  51.   chaine = ""
  52.   local clavier =
  53.   {
  54.     {"1", "2", "3"},
  55.     {"4", "5", "6"},
  56.     {"7", "8", "9"}
  57.   }
  58.  
  59.   local x=1
  60.   local y=1
  61.   local oldpad = Controls.read()
  62.  
  63.   while (1) do
  64.     pad = Controls.read()
  65.     if (pad:up() and not oldpad:up()) and (y > 1) then
  66.       y = y - 1
  67.     elseif (pad:down() and not oldpad:down()) and (y < 3) then
  68.       y = y + 1
  69.     elseif (pad:left() and not oldpad:left()) and (x > 1) then
  70.       x = x - 1
  71.     elseif (pad:right() and not oldpad:right()) and (x < 3) then
  72.       x = x + 1
  73.     end
  74.     if pad:cross() and not oldpad:cross() then
  75.       chaine = chaine..clavier[y][x]
  76.     elseif pad:start() and not oldpad:start() then
  77.       break;
  78.     elseif pad:square() and not oldpad:square() then
  79.       chaine = string.sub(chaine,1, #chaine - 1)
  80.     end
  81.    
  82.     screen:clear()--Color.new(255,255,255))
  83.     for i = 1, 3 do
  84.       for j = 1, 3 do
  85.         screen:print(j*41+20,i*31+20,clavier[i][j],(j==x and i==y) and red or white)
  86.       end
  87.     end
  88.     screen:print(272-chaine:len()*4.5,2,chaine,white);
  89.     oldpad = pad;
  90.     screen.waitVblankStart()
  91.     screen.flip()
  92.   end
  93.  
  94.  
  95.   ToucheOFF = nil
  96.   ToucheON = nil
  97.   x=nil
  98.   collectgarbage()
  99.   return tonumber(chaine);
  100. end
  101.  
  102. function ChooseContent()
  103.     local Files = System.listDirectory()
  104.     local oldpod = Controls.read()
  105.     local pod = oldpod
  106.     local selected = 1
  107.     local i = 1
  108.     table.remove(Files, 1)
  109.     table.remove(Files, 1)
  110.     while true do
  111.         if Files[i].name:reverse():sub(0,4) ~= "TXT." then
  112.             table.remove(Files, i)
  113.         end
  114.         i = i + 1
  115.         if i > #Files then
  116.             break
  117.         end
  118.     end
  119.     while true do
  120.         pod = Controls.read()
  121.         screen:clear()
  122.         if pod:down() and not oldpod:down() then
  123.             selected = selected < #Files and selected + 1 or 1
  124.          
  125.         elseif pod:up() and not oldpod:up() then
  126.             selected = selected > 1 and selected - 1 or #Files
  127.          
  128.         end
  129.         if pod:cross() then
  130.             break
  131.         end
  132.        
  133.         for i = 1, #Files do
  134.             screen:print(10,10*i,Files[i].name,i == selected and red or white)
  135.         end
  136.         screen.waitVblankStart() -- On attend que l'ecran soit en position pour afficher
  137.         screen.flip() -- On met a jour l'ecran
  138.         oldpod = pod
  139.     end
  140.     return Files[selected].name
  141. end
  142.  
  143.  
  144. function save(grille, name)
  145.     if DoesFileExist("MAP-"..name..".TXT") then
  146.         if not message("This file already exist, replace",true) then
  147.             return
  148.         end
  149.     end
  150.     file = io.open("MAP-"..name..".TXT","w")
  151.     for i = 1, 27 do
  152.         for j = 1, 48 do
  153.             file:write(grille[i][j].content)
  154.         end
  155.         file:write("\n")
  156.     end
  157.     file:close()
  158.     message("Done", false)
  159. end
  160.  
  161. function printGrille(H,L,color)
  162.     for i = 0, H do
  163.         screen:drawLine(i*10,0,i*10,L*10,color)
  164.     end
  165.     for i = 0, L do
  166.         screen:drawLine(0,i*10,H*10,i*10, color)
  167.     end
  168. end
  169.  
  170. function printBlocs(grille)
  171.     for i = 1, #grille[1] do
  172.         for j = 1, #grille do
  173.             if grille[j][i].content == 1 then
  174.                 screen:fillRect(grille[j][i].x,grille[j][i].y,9,9,black)
  175.             elseif grille[j][i].content == 2 then
  176.                 screen:fillRect(grille[j][i].x,grille[j][i].y,9,9,green)
  177.             end
  178.         end
  179.     end
  180. end
  181.  
  182. function Canevas(grille)
  183.     for i = 1, 26, 2 do
  184.         for j = 1, 48, 2 do
  185.             grille[i][j].content = 0
  186.             grille[i][j+1].content = 1
  187.         end
  188.         for j = 1, 48 do
  189.             grille[i+1][j].content = 1
  190.         end
  191.     end
  192.     for j = 1, 48, 2 do
  193.         grille[27][j].content = 0
  194.         grille[27][j+1].content = 1
  195.     end
  196.     return grille
  197. end
  198.  
  199. function Editor(grille)
  200. x,y = 1, 1
  201. couleurSelecteur = red
  202. move = 0
  203. oldpad = Controls.read()
  204. while true do
  205.     pad = Controls.read()
  206.     screen:clear(white)
  207.     printGrille(#grille[1],#grille,yellow)
  208.     if (pad:right() and not oldpad:right() or pad:analogX() > 60) and x < #grille[1] then
  209.         x = x + 1
  210.         move = 1
  211.     end
  212.     if (pad:left() and not oldpad:left() or pad:analogX() < -60) and x > 1 then
  213.         x = x - 1
  214.         move =1
  215.     end
  216.     if (pad:down() and not oldpad:down() or pad:analogY() > 60) and y < #grille then
  217.         y = y + 1
  218.         move = 1
  219.     end
  220.     if (pad:up() and not oldpad:up() or pad:analogY() < -60) and y > 1 then
  221.         y = y - 1
  222.         move = 1
  223.     end
  224.     if pad:cross() and move == 1 then
  225.         grille[y][x].content = grille[y][x].content == 1 and 0 or 1
  226.         move = 0
  227.     elseif pad:square() then
  228.         grille[y][x].content = 2
  229.     end
  230.     if pad:select() and message("Apply defined canevas?",true) then
  231.         grille=Canevas(grille)
  232.     end
  233.     if pad:start() and message("Save map?",true) then
  234.         name = Keyboard()
  235.         save(grille,name)
  236.     end
  237.     if pad:circle() and message("Quit editor? All unsaved changes would be deleted.",true) then break end
  238.     printBlocs(grille)
  239.     screen:fillRect(grille[y][x].x,grille[y][x].y,9,9,couleurSelecteur)
  240.     screen.flip()
  241.     oldpad = pad
  242. end
  243. end
  244.  
  245.  
  246. map = ChooseContent()
  247.  
  248. file = io.open(map,"r")
  249. carte = {}
  250. grille = {}
  251. for i = 1, 27 do
  252.     grille[i]= {}
  253.     ligne = file:read()
  254.     for j = 1, 48 do
  255.         grille[i][j]={x=10*j-9,y=10*i-9,content= tonumber(string.sub(ligne,j,j))}
  256.     end
  257. end
  258. file:close()
  259.  
  260.  
  261. x,y = 1, 1
  262. couleurSelecteur = Color.new(255,0,0)
  263. oldpad = Controls.read()
  264.  
  265. while true do
  266.     pad = Controls.read()
  267.     screen:clear(white)
  268.     printGrille(#grille[1],#grille,Color.new(0,0,0))
  269.     if (pad:right() or pad:analogX() > 60) and x < #grille[1] and grille[y][x+1].content ~= 1 then
  270.         x = x + 1
  271.     elseif (pad:left() or pad:analogX() < -60) and x > 1 and grille[y][x-1].content ~= 1 then
  272.         x = x - 1
  273.     elseif (pad:down() or pad:analogY() > 60) and y < #grille and grille[y+1][x].content ~= 1 then
  274.         y = y + 1
  275.     elseif (pad:up() or pad:analogY() < -60) and y > 1 and grille[y-1][x].content ~= 1 then
  276.         y = y - 1
  277.     end
  278.     if pad:cross() and not oldpad:cross() and message("Launch level editor?", true) then
  279.         Editor(grille)
  280.     end
  281.     if pad:start() then
  282.         break
  283.     end
  284.     if grille[y][x].content == 2 then message("Oh yeah!",false); break end
  285.     printBlocs(grille)
  286.     screen:fillRect(grille[y][x].x,grille[y][x].y,9,9,couleurSelecteur)
  287.     screen.flip();
  288.     oldpad=pad
  289.     --System.sleep(100)
  290. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement