System.usbDiskModeActivate() 

bx = 120
rightCount = 0
leftCount = 0
upcount = 0
downcount = 0
menucar = 1
style = 1
aimenu = 1
cnttype = 1
bet = 100
load = 0
contup = 0
contdown = 0
bgup = 0
bgdown = 0
bgtype = 1
option = 0
intakeopt = 0
exhaustopt = 0
cpuopt = 0
turboopt = 0
nosopt = 0
tiresopt = 0
tranopt = 0
car1 = "locked"
car2 = "locked"
car3 = "locked"
car4 = "locked"
car5 = "locked"
car6 = "locked"
car7 = "locked"
car8 = "locked"
car9 = "locked"
car10 = "locked"
car11 = "locked"
car12 = "locked"
car13 = "locked"
car14 = "locked"
car15 = "locked"
car16 = "locked"


bgMusic = Sound.load("sound/music.wav", true)
bgMusic:play()

function right()
  bx = bx-360
  menucar = menucar+1
end
function left()
  bx = bx+360
  menucar = menucar-1
end
function betup()
if bet < money then
  bet = bet+100
end
end
function betdown()
if bet > 100 then
  bet = bet-100
end
end

function menucontrols()
menupad = Controls.read()
if menupad:up() then
    downcount = downcount+1
  else downcount = 0
end
if menupad:down() then
    upcount = upcount+1
  else upcount = 0
end
  
  if menupad:select() then 
    dofile "menu.lua" 
end
  
  if menupad:right() and option == 0 then
    rightCount = rightCount+1
  if rightCount == 1 then
    right() end
   else
  rightCount = 0
end
  if menupad:left() and option == 0 then 
    leftCount = leftCount+1
  if leftCount == 1 then
    left() end
   else
  leftCount = 0 
end
  if menupad:right() and option == 1 then 
    contup = contup +1
  else contup = 0 
end
  if menupad:left() and option == 1 then 
    contdown = contdown +1
  else contdown = 0 
end
  if menupad:right() and option == 2 then 
    bgup = bgup+1
  else bgup = 0
end
  if menupad:left() and option == 2 then 
    bgdown = bgdown+1
  else bgdown = 0
end
  if menupad:right() and option == 3 then 
    betup() 
end
  if menupad:left() and option == 3 then 
    betdown() 
end
  if menupad:cross() and option == 4 then 
    bgMusic = nil
    dofile "shop.lua" 
end
  if menupad:cross() and option == 5 then 
    bgMusic = nil
    kickit:play()
    dofile "drag.lua" 
end
end

function loadwins()
  winfile = io.open(winsFile, "r")
  if winfile then
    wins = winfile:read("*n")
    winfile:close()
  end
end
-----
function loadmoney()
  moneyfile = io.open(moneyFile, "r")
  if moneyfile then
    money = moneyfile:read("*n")
    moneyfile:close()
  end
end
-----
function savewins()
  winfile = io.open(winsFile, "w")
  if winfile then
    winfile:write(wins)
    winfile:close()
  end
end
-----
function savemoney()
  moneyfile = io.open(moneyFile, "w")
  if moneyfile then
    moneyfile:write(money)
    moneyfile:close()
  end
end


while true do

load = load+1

if load == 1 then
loadmoney()
loadparts()
loadwins()
end

if wins >= 0 then
car1 = "unlocked"
car2 = "unlocked"
end
if wins >= 1 then
car3 = "unlocked"
end
if wins >= 2 then
car4 = "unlocked"
end
if wins >= 3 then
car5 = "unlocked"
end
if wins >= 3 then
car6 = "unlocked"
end
if wins >= 4 then
car7 = "unlocked"
end
if wins >= 4 then
car8 = "unlocked"
end
if wins >= 5 then
car9 = "unlocked"
end
if wins >= 5 then
car10 = "unlocked"
end
if wins >= 5 then
car11 = "unlocked"
end
if wins >= 6 then
car12 = "unlocked"
end
if wins >= 6 then
car13 = "unlocked"
end
if wins >= 6 then
car14 = "unlocked"
end
if wins >= 7 then
car15 = "unlocked"
end
if wins >= 7 then
car16 = "unlocked"
end

if car1 == "unlocked" then
  carone = s13menu
else carone = locked
end
if car2 == "unlocked" then
  cartwo = crxmenu
else cartwo = locked
end
if car3 == "unlocked" then
  carthree = integramenu
else carthree = locked
end
if car4 == "unlocked" then
  carfour = preludemenu
else carfour = locked
end
if car5 == "unlocked" then
  carfive = supra2menu
else carfive = locked
end
if car6 == "unlocked" then
  carsix = irocmenu
else carsix = locked
end
if car7 == "unlocked" then
  carseven = s2000menu
else carseven = locked
end
if car8 == "unlocked" then
  careight = chargermenu
else careight = locked
end
if car9 == "unlocked" then
  carnine = gt350menu
else carnine = locked
end
if car10 == "unlocked" then
  carten = rx7menu
else carten = locked
end
if car11 == "unlocked" then
  careleven = stingmenu
else careleven = locked
end
if car12 == "unlocked" then
  cartwelve = gtmenu
else cartwelve = locked
end
if car13 == "unlocked" then
  carthirteen = stangmenu
else carthirteen = locked
end
if car14 == "unlocked" then
  carfourteen = gtrmenu
else carfourteen = locked
end
if car15 == "unlocked" then
  carfifteen = supramenu
else carfifteen = locked
end
if car16 == "unlocked" then
  carsixteen = vettemenu
else carsixteen = locked
end

if bx > 120 then bx = -5280 end
if bx < -5400 then bx = 120 end

if menucar > 16 then menucar = 1 end
if menucar < 1 then menucar = 16 end

if menucar == 1 and car1 == "unlocked" then car = s13 end
if menucar == 2 and car2 == "unlocked" then car = crx end
if menucar == 3 and car3 == "unlocked" then car = integra end
if menucar == 4 and car4 == "unlocked" then car = prelude end
if menucar == 5 and car5 == "unlocked" then car = supra2 end
if menucar == 6 and car6 == "unlocked" then car = iroc end
if menucar == 7 and car7 == "unlocked" then car = s2000 end
if menucar == 8 and car8 == "unlocked" then car = charger end
if menucar == 9 and car9 == "unlocked" then car = gt350 end
if menucar == 10 and car10 == "unlocked" then car = rx7 end
if menucar == 11 and car11 == "unlocked" then car = sting end
if menucar == 12 and car12 == "unlocked" then car = gto end
if menucar == 13 and car13 == "unlocked" then car = stang end
if menucar == 14 and car14 == "unlocked" then car = skyline end
if menucar == 15 and car15 == "unlocked" then car = supra end
if menucar == 16 and car16 == "unlocked" then car = vette end

if car == supra or car == vette or car == stang or car == gto or car == gtr or car == s2000 or car == rx7 then
gearmax = 6
elseif car == supra2 or car == s13 or car == crx or car == prelude or car == integra or car == iroc then
gearmax = 5
elseif car == gt350 or car == sting or car == charger then
gearmax = 4
end

--player gears
if ai == supra then 
aigearmax = 6
end
if ai == skyline then 
aigearmax = 6
end
if ai == stang then 
aigearmax = 6
end
if ai == rx7 then 
aigearmax = 6
end
if ai == s13 then 
aigearmax = 5
end
if ai == gto then 
aigearmax = 6
end
if ai == vette then 
aigearmax = 6
end
if ai == charger then 
aigearmax = 4
end
if ai == sting then 
aigearmax = 4
end
if ai == s2000 then 
aigearmax = 6
end
if ai == gt350 then 
aigearmax = 4
end
if ai == crx then 
aigearmax = 5
end
if ai == prelude then 
aigearmax = 5
end
if ai == integra then 
aigearmax = 5
end
if ai == iroc then 
aigearmax = 5
end
if ai == supra2 then 
aigearmax = 5
end


if wins == 0 then 
ai = s2000 
aimenu = 1
end
if wins == 1 then 
ai = rx7 
aimenu = 2
end
if wins == 2 then 
ai = charger 
aimenu = 3
end
if wins == 3 then 
ai = skyline 
aimenu = 4
end
if wins == 4 then 
ai = gto 
aimenu = 5
end
if wins == 5 then 
ai = stang 
aimenu = 6
end
if wins == 6 then 
ai = supra 
aimenu = 7
end
if wins == 7 then 
ai = vette 
aimenu = 8
end

if aimenu == 1 then aimenu = s2000menu end
if aimenu == 2 then aimenu = rx7menu end
if aimenu == 3 then aimenu = chargermenu end
if aimenu == 4 then aimenu = gtrmenu end
if aimenu == 5 then aimenu = gtmenu end
if aimenu == 6 then aimenu = stangmenu end
if aimenu == 7 then aimenu = supramenu end
if aimenu == 8 then aimenu = vettemenu end

if car == charger or car == sting or car == gt350 then style = 1 
else style = 0 end

if contup == 1 then
  cnttype = cnttype+1
end
if contdown == 1 then
  cnttype = cnttype+1
end
if cnttype > 2 then
  cnttype = 1
end

if bgup == 1 then
  bgtype = bgtype+1
end
if bgdown == 1 then
  bgtype = bgtype+1
end

if bgtype > 2 then
  bgtype = 1
end



if cnttype == 1 then
controls = "Normal"
else controls = "Hard"
end

if bgtype == 1 then
road = "Street"
bg = street
finish = finishline2
else 
road = "Track"
bg = track
finish = finishline1
end

if bet > money then
  bet = money
end

if bet < 100 then
  bet = 100
end

if money < 1 then
  dofile "gameover.lua"
end

if upcount == 1 then 
  option = option+1
end
if downcount == 1 then 
  option = option-1
end

if option == 0 then 
  carcolor = orange
else carcolor = yellow
end
if option == 1 then 
  cntcolor = orange
else cntcolor = yellow
end
if option == 2 then 
  loccolor = orange
else loccolor = yellow
end
if option == 3 then 
  betcolor = orange
else betcolor = yellow
end
if option == 4 then 
  shopcolor = orange
else shopcolor = yellow
end
if option == 5 then 
  startcolor = orange
else startcolor = yellow
end

if option > 5 then option = 0 end
if option < 0 then option = 5 end




loadwins()
menucontrols()
screen:clear()
screen:blit(bx, 0, carone)
screen:blit(bx+360, 0, cartwo)
screen:blit(bx+720, 0, carthree)
screen:blit(bx+1080, 0, carfour)
screen:blit(bx+1440, 0, carfive)
screen:blit(bx+1800, 0, carsix)
screen:blit(bx+2160, 0, carseven)
screen:blit(bx+2520, 0, careight)
screen:blit(bx+2880, 0, carnine)
screen:blit(bx+3240, 0, carten)
screen:blit(bx+3600, 0, careleven)
screen:blit(bx+3960, 0, cartwelve)
screen:blit(bx+4320, 0, carthirteen)
screen:blit(bx+4680, 0, carfourteen)
screen:blit(bx+5040, 0, carfifteen)
screen:blit(bx+5400, 0, carsixteen)
screen:blit(120, 136, aimenu)
screen:blit(0, 0, blank)
screen:print(130, 5, "YOU", red)
screen:print(130, 140, "YOUR OPPONENT", red)
screen:print(10, 20, "Car", carcolor)
screen:print(10, 40, "Controls", cntcolor)
screen:print(10, 50, controls, red)
screen:print(10, 70, "Location", loccolor)
screen:print(10, 80, road, red)
screen:print(10, 100, "Bet", betcolor)
screen:print(10, 110, bet, red)
screen:print(10, 130, "Money", yellow)
screen:print(10, 140, money, red)
screen:print(10, 180, "Shop", shopcolor)
screen:print(10, 190, "Start", startcolor)
screen:flip()
screen.waitVblankStart()
end
