text = Font.load("text.ttf") 
text:setPixelSizes(0, 14) 
blanc = Color.new(255,255,255) 
gris = Color.new(204,204,204) 
fichiers = System.listDirectory("sauvegarde/") 
n_fichiers = table.getn(fichiers) 
position = 1 
compter = 1 
charger = 0 
page = 1 
i = 3 
ligne1 = "" 
ligne2 = "" 
ligne3 = "" 
ligne4 = "" 
ligne5 = "" 
ligne6 = "" 
ligne7 = ""
currentTime1 = ""
currentTime2 = ""
currentTime3 = ""
currentTime4 = ""
currentTime5 = ""
currentTime6 = ""
currentTime7 = ""

if n_fichiers > 9 then 
ligne8 = "L : Back -- R : Next" 
else 
ligne8 = "" 
end 

xx = 290 
yy = 55 

positionX = 0
positionY = 0

curseur = Image.createEmpty(5,5)
curseur:clear(blanc)

mnu_joueurenreg = Image.load("images/mnu_joueurenreg.png")
img_background = Image.load("images/img_background.png")
mnu_joueurcharge = Image.load("images/mnu_joueurcharge.png")

while true do 
screen:clear() 
pad = Controls.read() 
screen:blit(positionX, positionY, img_background)
screen:blit(positionX, positionY, mnu_joueurenreg)

fichier = {fichier1, fichier2, fichier3, fichier4, fichier5, fichier6, fichier7} 
   if ligne1 == "" and compter == 1 and n_fichiers > 2 then 
      ligne1 = "1 : "..fichiers[i].name 
      ligne1 = string.gsub(ligne1,".txt","") 
      fichier1 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier1, "r")
	  currentTime1 = file:read()
      file:close()
   elseif ligne2 == "" and compter == 1 and n_fichiers > 2 then 
      ligne2 = "2 : "..fichiers[i].name 
      ligne2 = string.gsub(ligne2,".txt","") 
      fichier2 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier2, "r")
	  currentTime2 = file:read()
      file:close()
   elseif ligne3 == "" and compter == 1 and n_fichiers > 2 then 
      ligne3 = "3 : "..fichiers[i].name 
      ligne3 = string.gsub(ligne3,".txt","") 
      fichier3 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier3, "r")
	  currentTime3 = file:read()
      file:close()
   elseif ligne4 == "" and compter == 1 and n_fichiers > 2 then 
      ligne4 = "4 : "..fichiers[i].name 
      ligne4 = string.gsub(ligne4,".txt","") 
      fichier4 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier4, "r")
	  currentTime4 = file:read()
      file:close()
   elseif ligne5 == "" and compter == 1 and n_fichiers > 2 then 
      ligne5 = "5 : "..fichiers[i].name 
      ligne5 = string.gsub(ligne5,".txt","") 
      fichier5 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier5, "r")
	  currentTime5 = file:read()
      file:close()
   elseif ligne6 == "" and compter == 1 and n_fichiers > 2 then 
      ligne6 = "6 : "..fichiers[i].name 
      ligne6 = string.gsub(ligne6,".txt","") 
      fichier6 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier6, "r")
	  currentTime6 = file:read()
      file:close()
   elseif ligne7 == "" and compter == 1 and n_fichiers > 2 then 
      ligne7 = "7 : "..fichiers[i].name 
      ligne7 = string.gsub(ligne7,".txt","") 
      fichier7 = fichiers[i].name 
	  file = io.open("sauvegarde/"..fichier7, "r")
	  currentTime7 = file:read()
      file:close()
   end 
   if n_fichiers > i then 
      i = i + 1 
   else 
      compter = 0 
   end 

screen:fontPrint(text,300,60,ligne1,blanc) 
screen:fontPrint(text,320,70,".........................."..currentTime1,gris)
screen:fontPrint(text,300,85,ligne2,blanc)
screen:fontPrint(text,320,95,".........................."..currentTime2,gris) 
screen:fontPrint(text,300,110,ligne3,blanc) 
screen:fontPrint(text,320,120,"........................."..currentTime3,gris)
screen:fontPrint(text,300,135,ligne4,blanc) 
screen:fontPrint(text,320,145,"........................."..currentTime4,gris)
screen:fontPrint(text,300,160,ligne5,blanc) 
screen:fontPrint(text,320,170,"........................."..currentTime5,gris)
screen:fontPrint(text,300,185,ligne6,blanc) 
screen:fontPrint(text,320,195,"........................."..currentTime6,gris)
screen:fontPrint(text,300,210,ligne7,blanc) 
screen:fontPrint(text,320,220,"........................."..currentTime7,gris)
screen:fontPrint(text,300,240,ligne8,blanc) 
screen:blit(xx,yy,curseur) 

   if pad:select() and oldpad:select() ~= pad:select() then 
   break 
   elseif pad:down() and oldpad:down() ~= pad:down() and position < 7 then 
   position = position + 1 
   yy = yy + 25 
   elseif pad:up() and oldpad:up() ~= pad:up() and position > 1 then 
   position = position - 1 
   yy = yy - 25 
   elseif pad:start() and oldpad:start() ~= pad:start() then 
   charger = fichier[position] 
   
   if charger ~= nil then 
   break 
   end 
   elseif pad:l() and oldpad:l() ~= pad:l() and page > 1 then 
   page = page - 1 
   i = 3 + page * 7 - 7 
   position = 1 
   compter = 1 
   yy = 40 
   ligne1 = "" 
   ligne2 = "" 
   ligne3 = "" 
   ligne4 = "" 
   ligne5 = "" 
   ligne6 = "" 
   ligne7 = "" 
   ligne8 = "L : Back -- R : Next" 
   elseif pad:r() and oldpad:r() ~= pad:r() and ligne8 == "L : Back -- R : Next" then 
   page = page + 1 
   i = 3+7*(page-1) 
   position = 1 
   compter = 1 
   yy = 40 
   ligne1 = "" 
   ligne2 = "" 
   ligne3 = "" 
   ligne4 = "" 
   ligne5 = "" 
   ligne6 = "" 
   ligne7 = "" 
      if n_fichiers-7 > i then 
      ligne8 = "L : Back -- R : Next" 
      else 
      ligne8 = "" 
      end    
   end 
screen.waitVblankStart() 
screen.flip() 
oldpad = pad 
end 

if charger ~= 0 then 
file = io.open("savename.txt", "w")
charger = string.gsub(charger,".txt","")
file:write(charger)
file:close()
	while true do 
	screen:clear() 
	pad = Controls.read() 
	file = io.open("savename.txt", "r")
	ourline = file:read()
	file:close()
	screen:clear() 
	screen:blit(positionX, positionY, img_background)
	screen:blit(positionX, positionY, mnu_joueurcharge)
	screen:print(310,47,ourline,blanc)
				if pad:select() and oldpad:select() ~= pad:select() then
		dofile("intro.lua")
		end
		if pad:start() and oldpad:start() ~= pad:start() then 
		dofile("main.lua")
		end
	screen.flip()
	screen.waitVblankStart()
	oldpad = pad
	end
end 

mnu_joueurenreg = nil
img_background = nil
mnu_joueurcharge = nil
