System.setHigh()

noir = Color.new(0,0,0)
blanc = Color.new(255,255,255)
rouge = Color.new(255,0,0)
orange = Color.new(255,153,0)
jaune = Color.new(255,255,0)
vert = Color.new(0,255,0)
bleu = Color.new(0,0,255)

sccnt=1

name = ""
chrono = ""
text2 = Font.load("text2.ttf") 
text2:setPixelSizes(0, 16) 

img_collisions = Image.load("images/img_collisions.png")
img_background = Image.load("images/img_background.png")
img_voiture = Image.load("images/img_voiture.png")
img_collisionvoiture = Image.load("images/img_collisionvoiture.png")
mnu_jeuencours = Image.load("images/mnu_jeuencours.png")

transparent = img_collisions:pixel(0,0)

collisionsX = 0
collisionsY = 0
collisionvoiture = 0
positionX = 0
positionY = 0
axeX = 155
axeY = -80
vecteur = 0 
vitesse = 0 

minuteur = Timer.new() 
minuteur:start()

dofile("./fonction.lua")

while true do
pad = Controls.read()
screen:clear()
file = io.open("savename.txt", "r")
name = file:read()
file:close()
file = io.open("sauvegarde/"..name..".txt", "r")
chrono = file:read("*n")
file:close()
ZoomIMG(img_voiture,axeX,axeY,0,vecteur)
screen:blit(collisionsX, collisionsY, img_collisions)
screen:blit(collisionsX, collisionsY, img_background)
screen:blit(positionX, positionY, mnu_jeuencours)
screen:fontPrint(text2,290,25,name,jaune)

currentTime = minuteur:time() 
minutes = math.floor(currentTime/60000) 
secondes = math.floor(math.mod(currentTime,60000)/1000)
milisecondes = math.floor(math.mod(currentTime,60000)/10)
screen:fontPrint(text2,362,67, ""..minutes..":"..secondes..":"..milisecondes,blanc) 

if chrono ~= nil then
chminutes = math.floor(chrono/60000) 
chsecondes = math.floor(math.mod(chrono,60000)/1000)
chmilisecondes = math.floor(math.mod(chrono,60000)/10)
screen:fontPrint(text2,362,92, ""..chminutes..":"..chsecondes..":"..chmilisecondes,orange)
end

oldY = axeY
oldY = axeY
axeXn = axeX + 104
axeYn = axeY - 56

if pad:left() and pad:circle() and vitesse < -0.3 then 
vecteur = vecteur - 1.5 
end 
if pad:right() and pad:circle() and vitesse < -0.3 then 
vecteur = vecteur + 1.5 
end 
if pad:left() and pad:triangle() and vitesse > 0.3 then 
vecteur = vecteur + 1.5 
end 
if pad:right() and pad:triangle() and vitesse > 0.3 then 
vecteur = vecteur - 1.5 
end 

axeX = axeX + math.sin(math.rad(vecteur))*vitesse
axeY = axeY + math.cos(math.rad(vecteur))*vitesse

if pad:triangle() and collisionvoiture == 0 then 
vitesse = vitesse + 0.005 
else 
vitesse = vitesse - 0.08 
if pad:cross() and vitesse < 0 then 
vitesse = vitesse + 0.3
else if vitesse > 0 then
vitesse = 0
end end
collisionvoiture = 0
end 

if pad:circle() and collisionvoiture == 0 then 
vitesse = vitesse - 0.005 
else 
vitesse = vitesse + 0.08 
if pad:cross() and vitesse > 0 then 
vitesse = vitesse - 0.3
else if vitesse < 0 then
vitesse = 0
end end
collisionvoiture = 0
end 

if (Rot >= -11 and Rot < 11) or (Rot >= 348 and Rot < 371) then
axeXARD = axeX + 51
axeYARD = axeY + 91
axeXAR = axeX + 26
axeYAR = axeY + 95
axeXARG = axeX + 3
axeYARG = axeY + 91
axeXPARD = axeX + 53
axeYPARD = axeY + 62
axeXPARG = axeX + 0
axeYPARG = axeY + 62
axeXPAVD = axeX + 53
axeYPAVD = axeY + 27
axeXPAVG = axeX + 0
axeYPAVG = axeY + 27
axeXAVD = axeX + 49
axeYAVD = axeY - 3
axeXAV = axeX + 26
axeYAV = axeY - 7
axeXAVG = axeX + 3
axeYAVG = axeY - 3
rotation = nord
end
if (Rot >= 11 and Rot < 34) or (Rot >= 371 and Rot < 394) then
axeXARD = axeX + 65
axeYARD = axeY + 80
axeXAR = axeX + 44
axeYAR = axeY + 92
axeXARG = axeX + 25
axeYARG = axeY + 96
axeXPARD = axeX + 58
axeYPARD = axeY + 49
axeXPARG = axeX + 8
axeYPARG = axeY + 71
axeXPAVD = axeX + 44
axeYPAVD = axeY + 16
axeXPAVG = axeX - 7
axeYPAVG = axeY + 38
axeXAVD = axeX + 28
axeYAVD = axeY - 9
axeXAV = axeX + 6
axeYAV = axeY - 5
axeXAVG = axeX - 12
axeYAVG = axeY + 8
rotation = nordnordouest
end
if (Rot >= 34 and Rot < 56) or (Rot >= 394 and Rot < 416) then
axeXARD = axeX + 77
axeYARD = axeY + 61
axeXAR = axeX + 63
axeYAR = axeY + 80
axeXARG = axeX + 44
axeYARG = axeY + 94
axeXPARD = axeX + 59
axeYPARD = axeY + 39
axeXPARG = axeX + 22
axeYPARG = axeY + 76
axeXPAVD = axeX + 33
axeYPAVD = axeY + 13
axeXPAVG = axeX - 5
axeYPAVG = axeY + 49
axeXAVD = axeX + 7
axeYAVD = axeY - 7
axeXAV = axeX - 12
axeYAV = axeY + 6
axeXAVG = axeX - 23
axeYAVG = axeY + 25
rotation = nordouest
end
if (Rot >= 56 and Rot < 79) or (Rot >= 416 and Rot < 439) then
axeXARD = axeX + 79
axeYARD = axeY + 40
axeXAR = axeX + 73
axeYAR = axeY + 63
axeXARG = axeX + 62
axeYARG = axeY + 84
axeXPARD = axeX + 54
axeYPARD = axeY + 26
axeXPARG = axeX + 32
axeYPARG = axeY + 74
axeXPAVD = axeX + 20
axeYPAVD = axeY + 12
axeXPAVG = axeX - 1
axeYPAVG = axeY + 61
axeXAVD = axeX - 9
axeYAVD = axeY + 4
axeXAV = axeX - 22
axeYAV = axeY + 24
axeXAVG = axeX - 26
axeYAVG = axeY + 46
rotation = ouestnordouest
end
if (Rot >= 79 and Rot < 102) or (Rot >= 439 and Rot < 462) then
axeXARD = axeX + 74
axeYARD = axeY + 20
axeXAR = axeX + 76
axeYAR = axeY + 44
axeXARG = axeX + 74
axeYARG = axeY + 67
axeXPARD = axeX + 44
axeYPARD = axeY + 16
axeXPARG = axeX + 44
axeYPARG = axeY + 70
axeXPAVD = axeX + 7
axeYPAVD = axeY + 16
axeXPAVG = axeX + 7
axeYPAVG = axeY + 70
axeXAVD = axeX - 21
axeYAVD = axeY + 20
axeXAV = axeX - 27
axeYAV = axeY + 43
axeXAVG = axeX - 21
axeYAVG = axeY + 66
rotation = ouest
end
if (Rot >= 102 and Rot < 124) or (Rot >= 462 and Rot < 484) then
axeXARD = axeX + 62
axeYARD = axeY + 3
axeXAR = axeX + 73
axeYAR = axeY + 24
axeXARG = axeX + 79
axeYARG = axeY + 47
axeXPARD = axeX + 31
axeYPARD = axeY + 11
axeXPARG = axeX + 53
axeYPARG = axeY + 61
axeXPAVD = axeX - 1
axeYPAVD = axeY + 25
axeXPAVG = axeX + 21
axeYPAVG = axeY + 75
axeXAVD = axeX - 27
axeYAVD = axeY + 40
axeXAV = axeX -22
axeYAV = axeY + 64
axeXAVG = axeX - 10
axeYAVG = axeY + 82
rotation = ouestsudouest
end 
if (Rot >= 124 and Rot < 146) or (Rot >= 484 and Rot < 506) then
axeXARD = axeX + 41
axeYARD = axeY - 6
axeXAR = axeX + 61
axeYAR = axeY + 8
axeXARG = axeX + 76
axeYARG = axeY + 27
axeXPARD = axeX + 20
axeYPARD = axeY + 11
axeXPARG = axeX + 56
axeYPARG = axeY + 50
axeXPAVD = axeX - 6
axeYPAVD = axeY + 36
axeXPAVG = axeX + 30
axeYPAVG = axeY + 75
axeXAVD = axeX - 24
axeYAVD = axeY + 60
axeXAV = axeX -12
axeYAV = axeY + 80
axeXAVG = axeX + 7
axeYAVG = axeY + 93
rotation = sudouest
end 
if (Rot >= 146 and Rot < 169) or (Rot >= 506 and Rot < 529) then
axeXARD = axeX + 23
axeYARD = axeY - 10
axeXAR = axeX + 47
axeYAR = axeY - 4
axeXARG = axeX + 66
axeYARG = axeY + 7
axeXPARD = axeX + 7
axeYPARD = axeY + 16
axeXPARG = axeX + 59
axeYPARG = axeY + 37
axeXPAVD = axeX - 7
axeYPAVD = axeY + 50
axeXPAVG = axeX + 44
axeYPAVG = axeY + 70
axeXAVD = axeX - 14
axeYAVD = axeY + 78
axeXAV = axeX + 5
axeYAV = axeY + 91
axeXAVG = axeX + 27
axeYAVG = axeY + 96
rotation = sudsudouest
end
if (Rot >= 169 and Rot < 191) or (Rot >= 529 and Rot < 551) then
axeXARD = axeX + 2
axeYARD = axeY - 4
axeXAR = axeX + 26
axeYAR = axeY - 7
axeXARG = axeX + 49
axeYARG = axeY - 4
axeXPARD = axeX + 0
axeYPARD = axeY + 27
axeXPARG = axeX + 51
axeYPARG = axeY + 27
axeXPAVD = axeX + 0
axeYPAVD = axeY + 62
axeXPAVG = axeX + 51
axeYPAVG = axeY + 62
axeXAVD = axeX + 2
axeYAVD = axeY + 90
axeXAV = axeX + 25
axeYAV = axeY + 96
axeXAVG = axeX + 49
axeYAVG = axeY + 90
rotation = sud
end
if (Rot >= 191 and Rot < 214) or (Rot >= 551 and Rot < 573) then
axeXARD = axeX - 14
axeYARD = axeY + 7
axeXAR = axeX + 5
axeYAR = axeY - 4
axeXARG = axeX + 28
axeYARG = axeY - 10
axeXPARD = axeX - 6
axeYPARD = axeY + 36
axeXPARG = axeX + 44
axeYPARG = axeY + 17
axeXPAVD = axeX + 6
axeYPAVD = axeY + 71
axeXPAVG = axeX + 58
axeYPAVG = axeY + 50
axeXAVD = axeX + 23
axeYAVD = axeY + 96
axeXAV = axeX + 46
axeYAV = axeY + 92
axeXAVG = axeX + 65
axeYAVG = axeY + 80
rotation = sudsudest
end
if (Rot >= 214 and Rot < 236) or (Rot >= 573 and Rot < 595) then
axeXARD = axeX - 24
axeYARD = axeY + 26
axeXAR = axeX - 10
axeYAR = axeY + 8
axeXARG = axeX + 8
axeYARG = axeY - 7
axeXPARD = axeX - 6
axeYPARD = axeY + 50
axeXPARG = axeX + 32
axeYPARG = axeY + 11
axeXPAVD = axeX + 19
axeYPAVD = axeY + 76
axeXPAVG = axeX + 57
axeYPAVG = axeY + 37
axeXAVD = axeX + 43
axeYAVD = axeY + 92
axeXAV = axeX + 63
axeYAV = axeY + 81
axeXAVG = axeX + 75
axeYAVG = axeY + 59
rotation = sudest
end
if (Rot >= 236 and Rot < 259) or (Rot >= 595 and Rot < 618) then
axeXARD = axeX - 28
axeYARD = axeY + 46
axeXAR = axeX - 21
axeYAR = axeY + 23
axeXARG = axeX - 11
axeYARG = axeY + 3
axeXPARD = axeX - 2
axeYPARD = axeY + 63
axeXPARG = axeX + 19
axeYPARG = axeY + 11
axeXPAVD = axeX + 33
axeYPAVD = axeY + 76
axeXPAVG = axeX + 52
axeYPAVG = axeY + 24
axeXAVD = axeX + 61
axeYAVD = axeY + 83
axeXAV = axeX + 73
axeYAV = axeY + 64
axeXAVG = axeX + 78
axeYAVG = axeY + 44
rotation = estsudest
end
if (Rot >= 259 and Rot < 281) or (Rot >= 618 and Rot < 640) then
axeXARD = axeX - 23
axeYARD = axeY + 68
axeXAR = axeX - 25
axeYAR = axeY + 44
axeXARG = axeX - 23
axeYARG = axeY + 21
axeXPARD = axeX + 7
axeYPARD = axeY + 71
axeXPARG = axeX + 7
axeYPARG = axeY + 17
axeXPAVD = axeX + 45
axeYPAVD = axeY + 71
axeXPAVG = axeX + 45
axeYPAVG = axeY + 18
axeXAVD = axeX + 73
axeYAVD = axeY + 68
axeXAV = axeX + 78
axeYAV = axeY + 44
axeXAVG = axeX + 73
axeYAVG = axeY + 21
rotation = est
end
if (Rot >= 281 and Rot < 304) or (Rot >= 640 and Rot < 663) then
axeXARD = axeX - 10
axeYARD = axeY + 85
axeXAR = axeX - 22
axeYAR = axeY + 64
axeXARG = axeX - 28
axeYARG = axeY + 42
axeXPARD = axeX + 19
axeYPARD = axeY + 76
axeXPARG = axeX - 2
axeYPARG = axeY + 25
axeXPAVD = axeX + 52
axeYPAVD = axeY + 62
axeXPAVG = axeX + 32
axeYPAVG = axeY + 12
axeXAVD = axeX + 78
axeYAVD = axeY + 45
axeXAV = axeX + 75
axeYAV = axeY + 24
axeXAVG = axeX + 62
axeYAVG = axeY + 5
rotation = estnordest
end
if (Rot >= 304 and Rot < 326) or (Rot >= 663 and Rot < 685) then
axeXARD = axeX + 9
axeYARD = axeY + 94
axeXAR = axeX -10
axeYAR = axeY + 80
axeXARG = axeX - 24
axeYARG = axeY + 61
axeXPARD = axeX + 32
axeYPARD = axeY + 76
axeXPARG = axeX - 6
axeYPARG = axeY + 38
axeXPAVD = axeX + 58
axeYPAVD = axeY + 48
axeXPAVG = axeX + 19
axeYPAVG = axeY + 13
axeXAVD = axeX + 75
axeYAVD = axeY + 26
axeXAV = axeX + 63
axeYAV = axeY + 7
axeXAVG = axeX + 43
axeYAVG = axeY - 5
rotation = nordest
end
if (Rot >= 326 and Rot < 348) or (Rot >= 685 and Rot < 707) then
axeXARD = axeX + 29
axeYARD = axeY + 98
axeXAR = axeX + 6
axeYAR = axeY + 92
axeXARG = axeX - 15
axeYARG = axeY + 79
axeXPARD = axeX + 45
axeYPARD = axeY + 72
axeXPARG = axeX - 5
axeYPARG = axeY + 50
axeXPAVD = axeX + 58
axeYPAVD = axeY + 39
axeXPAVG = axeX + 6
axeYPAVG = axeY + 18
axeXAVD = axeX + 65
axeYAVD = axeY + 8
axeXAV = axeX + 46
axeYAV = axeY - 4
axeXAVG = axeX + 22
axeYAVG = axeY - 9
rotation = nordnordest
end

if collisionvoiture == 1 and rotation == nord and pad:triangle() then
vitesse = 0
axeY = axeY + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordnordouest and pad:triangle() then
vitesse = 0
axeY = axeY + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordouest and pad:triangle() then
vitesse = 0
axeX = axeX + 2
axeY = axeY + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == ouestnordouest and pad:triangle() then
vitesse = 0
axeX = axeX + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == ouest and pad:triangle() then
vitesse = 0
axeX = axeX + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == ouestsudouest and pad:triangle() then
vitesse = 0
axeX = axeX + 2
axeY = axeY - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudouest and pad:triangle() then
vitesse = 0
axeX = axeX - 2
axeY = axeY - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudsudouest and pad:triangle() then
vitesse = 0
axeX = axeX
axeY = axeY - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sud and pad:triangle() then
axeY = axeY - 2
end
if collisionvoiture == 1 and rotation == sudsudest and pad:triangle() then
vitesse = 0
axeX = axeX
axeY = axeY - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudest and pad:triangle() then
vitesse = 0
axeY = axeY - 2
axeX = axeX + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == estsudest and pad:triangle() then
vitesse = 0
axeX = axeX - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == est and pad:triangle() then
vitesse = 0
axeX = axeX - 2
axeY = oldY
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == estnordest and pad:triangle() then
vitesse = 0
axeX = axeX - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordest and pad:triangle() then
vitesse = 0
axeX = axeX - 2
axeY = axeY + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordnordest and pad:triangle() then
vitesse = 0
axeY = axeY + 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end

if collisionvoiture == 1 and rotation == nord and pad:cross() then
vitesse = 0
axeY = axeY - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordnordouest and pad:cross() then
vitesse = 0
axeY = axeY - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordouest and pad:cross() then
vitesse = 0
axeX = axeX - 2
axeY = axeY - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == ouestnordouest and pad:cross() then
vitesse = 0
axeX = axeX + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == ouest and pad:cross() then
vitesse = 0
axeX = axeX + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == ouestsudouest and pad:cross() then
vitesse = 0
axeX = axeX + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudouest and pad:cross() then
vitesse = 0
axeY = axeY - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudsudouest and pad:cross() then
vitesse = 0
axeY = axeY - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sud and pad:cross() then
vitesse = 0
axeY = axeY + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudsudest and pad:cross() then
vitesse = 0
axeY = axeY - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == sudest and pad:cross() then
vitesse = 0
axeX = axeX + 2
axeY = axeY + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == estsudest and pad:cross() then
vitesse = 0
axeX = axeX - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == est and pad:cross() then
vitesse = 0
axeX = axeX - 2
vitesse = vitesse - 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == estnordest and pad:cross() then
vitesse = 0
axeX = axeX - 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordest and pad:cross() then
vitesse = 0
axeX = axeX - 2
axeY = axeY + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end
if collisionvoiture == 1 and rotation == nordnordest and pad:cross() then
vitesse = 0
axeY = axeY + 2
vitesse = vitesse + 0.1
collisionvoiture = 0
end

if axeY <= 15 and currentTime <= 2200 then
pixelARD = pixelARD
pixelAR = pixelAR
pixelARG = pixelARG
pixelPARD = pixelPARD
pixelPARG = pixelPARG
pixelPAVD = pixelPAVD
pixelPAVG = pixelPAVG
end
if  axeY > 15 and currentTime > 2200 then
pixelARD = img_collisions:pixel(axeXARD,axeYARD)
pixelAR = img_collisions:pixel(axeXAR,axeYAR)
pixelARG = img_collisions:pixel(axeXARG,axeYARG)
pixelPARD = img_collisions:pixel(axeXPARD,axeYPARD)
pixelPARG = img_collisions:pixel(axeXPARG,axeYPARG)
pixelPAVD = img_collisions:pixel(axeXPAVD,axeYPAVD)
pixelPAVG = img_collisions:pixel(axeXPAVG,axeYPAVG)
pixelAVD = img_collisions:pixel(axeXAVD,axeYAVD)
pixelAV = img_collisions:pixel(axeXAV,axeYAV)
pixelAVG = img_collisions:pixel(axeXAVG,axeYAVG)
testcollision()
end

if collisionvoiture == 1 then
collision()
end


if pad:l() and pad:r() then
photo()
mnu_photo = Image.load("images/mnu_photo.png")
screen:blit(positionX, positionY, mnu_photo)
end

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

if (Rot >= 267 and Rot < 273) or (Rot >= 87 and Rot < 93) then
alignvoiture = 1
else
alignvoiture = 0
end

if alignvoiture == 1 and axeX > 164 and axeX < 178 and axeY > 160 and axeY < 166 then
controletime()
if currentTime > chrono then
partieterminee()
end
if currentTime < chrono then
nouveaurecord()
end
end

if pad:start() and oldpad:start() ~= pad:start() then
dofile("main.lua")
end
if pad:select() and oldpad:select() ~= pad:select() then
dofile("intro.lua")
end

screen.flip()
oldpad = pad 
screen.waitVblankStart()
end

img_collisions = nil
img_background = nil
img_voiture = nil
img_collisionvoiture = nil
mnu_jeuencours = nil
mnu_photo = nil