file = io.open("high_score/j1.txt", "r")
blanc = Color.new(255, 255, 255)


score1 = file:read()

file:close()

file = io.open("high_score/j2.txt", "r")



score2 = file:read()


file:close()


file = io.open("high_score/j3.txt", "r")



score3 = file:read()

file:close()


file = io.open("high_score/j4.txt", "r")

score4 = file:read()

file:close()


file = io.open("high_score/j5.txt", "r")

score5 = file:read()

file:close()

file = io.open("score_total.txt", "r")

score_total = file:read()
nb_reponse = file:read()

file:close()

fini = Image.load("images/fin.png")
blanc = Color.new(255,255,255)

nom = " "









function name()

-- dÈfinition de 2 couleurs

white = Color.new(255,255,255)

red = Color.new(255,0,0)



--dÈfinition de la chaine de caracteres

characters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " " }



--effacement de l'Ècran

selector = Image.createEmpty(10,10)

selector:clear(red)



--dÈfinition des variables :

charX = 100

charY= 100

currentX = 107

currentY = 98

currentLetter = 1

uppercase = true

name = ""

oldpad = Controls.read()

addX = 0

addY = -10

addRow = -9



--gÈnÈration d'une fonction permettant d'afficher les caracteres

function drawLetters()

for a = 1, 3 do

addX = 0

addY = addY + 10

addRow = addRow + 9

for b = 1, 9 do

addX = addX + 10

if uppercase == false then

screen:print(charX + addX,charY + addY, characters[b + addRow],white)

else

screen:print(charX + addX,charY + addY,string.upper(characters[b + addRow]),white)

end

end

end

addX = 0

addY = -10

addRow = -9

end



--affichage: choississez votre nom

while true do

pad = Controls.read()

screen:clear()

screen:print(10,10,"Enter Name: "..name,white)

screen:blit(currentX,currentY, selector)

drawLetters()



--dÈtection de la sÈlection d'un caractËre

if pad:cross() and oldpad:cross() ~= pad:cross() then

if uppercase == false then

name = name .. characters[currentLetter]

else

name = name .. string.upper(characters[currentLetter])

end

end



--possibilitÈ de correction du nom

if pad:square() and oldpad:square() ~= pad:square() then

name = string.sub(name, 1, string.len(name) - 1)

end



--sÈlection majuscule/minuscule

if pad:triangle() and oldpad:triangle() ~= pad:triangle() then

if uppercase == false then

uppercase = true

else

uppercase = false

end

end



-- MOVE SELECTOR RIGHT :

if pad:right() and currentLetter ~= 9 and currentLetter ~= 18 and currentLetter ~= 27 and oldpad:right() ~= pad:right() then

currentLetter = currentLetter + 1

currentX = currentX + 10

elseif pad:right() and oldpad:right() ~= pad:right() then

if currentLetter == 9 or currentLetter == 18 or currentLetter == 27 then

currentLetter = currentLetter - 8

currentX = 107

end

end



-- MOVE SELECTOR LEFT

if pad:left() and currentLetter ~= 1 and currentLetter ~= 10 and currentLetter ~= 19 and oldpad:left() ~= pad:left() then

currentLetter = currentLetter - 1

currentX = currentX - 10

elseif pad:left() and oldpad:left() ~= pad:left() then

if currentLetter == 1 or currentLetter == 10 or currentLetter == 19 then

currentX = 107 + 80

currentLetter = currentLetter + 8

end

end



-- MOVE SELECTOR DOWN

if pad:down() and currentLetter < 19 and oldpad:down() ~= pad:down() then

currentLetter = currentLetter + 9

currentY = currentY + 10

elseif pad:down() and currentLetter > 18 and oldpad:down() ~= pad:down() then

currentY = 98

currentLetter = currentLetter - 18

end



-- MOVE SELECTOR UP

if pad:up() and currentLetter > 9 and oldpad:up() ~= pad:up() then

currentLetter = currentLetter - 9

currentY = currentY - 10

elseif pad:up() and currentLetter < 18 and oldpad:up() ~= pad:up() then

currentY = 118

currentLetter = currentLetter + 18

end

if pad:start() then
nom = name
return nom
end


screen.flip()

screen.waitVblankStart()

oldpad = pad

end



end










while true do
screen:clear()

pad=Controls.read()

screen:blit(0,0,fini)
screen:print(432,208,nb_reponse,blanc)
screen:print(296,247,score_total,blanc)

if score_total > score5 and score_total < score4 then
name()
file = io.open("high_score/j5.txt", "w")
file:write(nom)
file:close()

file = io.open("high_score/j5.txt", "a")
file:write(score_total)
file:close()

end

if score_total > score4 and score_total < score3 then
name()
file = io.open("high_score/j4.txt", "w")
file:write(nom)
file:close()

file = io.open("high_score/j4.txt", "a")
file:write(score_total)
file:close()

end

if score_total > score3 and score_total < score2 then
name()
file = io.open("high_score/j3.txt", "w")
file:write(nom)
file:close()

file = io.open("high_score/j3.txt", "a")
file:write(score_total)
file:close()

end

if score_total > score2 and score_total < score1 then
name()
file = io.open("high_score/j2.txt", "w")
file:write(nom)
file:close()

file = io.open("high_score/j2.txt", "a")
file:write(score_total)
file:close()

end

if score_total > score1 then
name()
file = io.open("high_score/j1.txt", "w")
file:write(nom)
file:close()

file = io.open("high_score/j1.txt", "a")
file:write(score_total)
file:close()

end


if score_total < score5 then
dofile("menu.lua")
end


screen.flip() 

screen.waitVblankStart() 

end
