--COLOURS--

red = Color.new(200,0,0)
blue = Color.new(0,0,200)
black = Color.new(0,0,0)

--IMAGES--

track = Image.load("IMAGES/track5.png")
bridge = Image.load("IMAGES/track5bridge.png")
player1won = Image.load("IMAGES/player1won.png")
player2won = Image.load("IMAGES/player2won.png")
playersdraw = Image.load("IMAGES/playersdraw.png")

--PLAYER 1 IMAGES--

car1left = Image.load("IMAGES/CAR1/car1left.png")
car1right = Image.load("IMAGES/CAR1/car1right.png")
car1down = Image.load("IMAGES/CAR1/car1down.png")
car1up = Image.load("IMAGES/CAR1/car1up.png")

car1rightdown = Image.load("IMAGES/CAR1/car1rightdown.png")
car1rightup = Image.load("IMAGES/CAR1/car1rightup.png")
car1leftup = Image.load("IMAGES/CAR1/car1leftup.png")
car1leftdown = Image.load("IMAGES/CAR1/car1leftdown.png")

--PLAYER 2 IMAGES--

car2left = Image.load("IMAGES/CAR2/car2left.png")
car2right = Image.load("IMAGES/CAR2/car2right.png")
car2down = Image.load("IMAGES/CAR2/car2down.png")
car2up = Image.load("IMAGES/CAR2/car2up.png")

car2rightdown = Image.load("IMAGES/CAR2/car2rightdown.png")
car2rightup = Image.load("IMAGES/CAR2/car2rightup.png")
car2leftup = Image.load("IMAGES/CAR2/car2leftup.png")
car2leftdown = Image.load("IMAGES/CAR2/car2leftdown.png")

--PLAYERS--

player1 = {}
player1.img = car1right
player1.x = 330
player1.y = 55

player2 = {}
player2.img = car2right
player2.x = 330
player2.y = 30

--OBJECTS ECT--

trackmiddletopleftline = {}
trackmiddletopleftline.img = Image.createEmpty(1,1)
trackmiddletopleftline.x = 93
trackmiddletopleftline.y = 60

trackmiddlerightrectangle = {}
trackmiddlerightrectangle.img = Image.createEmpty(1,1)
trackmiddlerightrectangle.x = 235
trackmiddlerightrectangle.y = 60

trackmiddlemiddlerectangle = {}
trackmiddlemiddlerectangle.img = Image.createEmpty(1,1)
trackmiddlemiddlerectangle.x = 178
trackmiddlemiddlerectangle.y = 60

trackmiddlebottomrectangle = {}
trackmiddlebottomrectangle.img = Image.createEmpty(1,1)
trackmiddlebottomrectangle.x = 170
trackmiddlebottomrectangle.y = 160

trackmiddlebottomleftline = {}
trackmiddlebottomleftline.img = Image.createEmpty(1,1)
trackmiddlebottomleftline.x = 65
trackmiddlebottomleftline.y = 162

trackmiddlemiddleleftline = {}
trackmiddlemiddleleftline.img = Image.createEmpty(1,1)
trackmiddlemiddleleftline.x = 20
trackmiddlemiddleleftline.y = 108

--PLAYER 1 BRIDGE--
player1bridgebottom = {}
player1bridgebottom.img = Image.createEmpty(1,1)
player1bridgebottom.x = 95
player1bridgebottom.y = 120

player1bridgetop = {}
player1bridgetop.img = Image.createEmpty(1,1)
player1bridgetop.x = 400
player1bridgetop.y = 390

player1bridgeactivator = {}
player1bridgeactivator.img = Image.createEmpty(1,1)
player1bridgeactivator.x = 75
player1bridgeactivator.y = 190

player1bridgecounter = 0

--PLAYER 2 BRIDGE--

player2bridgebottom = {}
player2bridgebottom.img = Image.createEmpty(1,1)
player2bridgebottom.x = 95
player2bridgebottom.y = 120

player2bridgetop = {}
player2bridgetop.img = Image.createEmpty(1,1)
player2bridgetop.x = 400
player2bridgetop.y = 390

player2bridgeactivator = {}
player2bridgeactivator.img = Image.createEmpty(1,1)
player2bridgeactivator.x = 75
player2bridgeactivator.y = 190

player2bridgecounter = 0

--FONTS--

font1 = Font.load("FONTS/font1.ttf")
font1:setPixelSizes(25,30)

--SOUNDS--

Wav.load("SOUNDS/carengine.wav",0)


local volume = 1.0
local speed = 1.0

--LAP SYSTEM--

p1laps = 0
p2laps = 0


lap = {}
lap.img = Image.createEmpty(1,1)
lap.x = 290
lap.y = 20



--COUNT DOWN UNTILL RACE STARTS--
screen:clear()
screen:blit(0,0,track)
screen:blit(player1.x, player1.y, player1.img)
screen:blit(player2.x, player2.y, player2.img)

screen:fontPrint(font1,230,140,"3",black)
screen:flip()
screen.waitVblankStart(70)

screen:blit(0,0,track)
screen:blit(player1.x, player1.y, player1.img)
screen:blit(player2.x, player2.y, player2.img)

screen:fontPrint(font1,230,140,"2",black)
screen:flip()
screen.waitVblankStart(70)

screen:blit(0,0,track)
screen:blit(player1.x, player1.y, player1.img)
screen:blit(player2.x, player2.y, player2.img)

screen:fontPrint(font1,230,140,"1",black)
screen:flip()
screen.waitVblankStart(70)

screen:blit(0,0,track)
screen:blit(player1.x, player1.y, player1.img)
screen:blit(player2.x, player2.y, player2.img)

screen:fontPrint(font1,225,140,"GO!",black)
screen:flip()
screen.waitVblankStart(70)



while true do
pad = Controls.read()
screen:clear()

screen:blit(0,0,track,false)
screen:blit(player1.x, player1.y, player1.img)
screen:blit(player2.x, player2.y, player2.img)



--CONTROLS--

if pad:cross() or pad:circle() or pad:triangle() or pad:square() or pad:up() or pad:down() or pad:left() or pad:right() then
Wav.play(false,0)
end


if pad:start() then
screen:fontPrint(font1,180,140,"Exiting...",black)
screen:flip()
dofile("FILES/MULTIPLAYER/multiplayermenu.lua")
end

if pad:select() then
screen:fontPrint(font1,170,140,"Restarting...",black)
screen:flip()
dofile("FILES/MULTIPLAYER/championshiprace5.lua")
end


--PLAYER 1 CONTROLS--

if pad:right() and player1.x < 423 then
player1.x = player1.x + 2
player1.img = car1right
end

if pad:left() and player1.x > 30 then
player1.x = player1.x - 2
player1.img = car1left
end

if pad:down() and player1.y < 217 then
player1.y = player1.y + 2
player1.img = car1down
end

if pad:up() and player1.y > 30 then
player1.y = player1.y - 2
player1.img = car1up
end

--PLAYER 1 DIAGONALS--

if pad:right() and pad:down() then
player1.img = car1rightdown
end

if pad:right() and pad:up() then
player1.img = car1rightup
end

if pad:left() and pad:down() then
player1.img = car1leftdown
end

if pad:left() and pad:up() then
player1.img = car1leftup
end


--PLAYER 2 CONTROLS--

if pad:circle() and player2.x < 423 then
player2.x = player2.x + 2
player2.img = car2right
end

if pad:square() and player2.x > 30 then
player2.x = player2.x - 2
player2.img = car2left
end

if pad:cross() and player2.y < 217 then
player2.y = player2.y + 2
player2.img = car2down
end

if pad:triangle() and player2.y > 30 then
player2.y = player2.y - 2
player2.img = car2up
end

--PLAYER 2 DIAGONALS--

if pad:circle() and pad:cross() then
player2.img = car2rightdown
end

if pad:circle() and pad:triangle() then
player2.img = car2rightup
end

if pad:square() and pad:cross() then
player2.img = car2leftdown
end

if pad:square() and pad:triangle() then
player2.img = car2leftup
end


--PLAYER 1 COLLISION--

if (player1.x > lap.x and player1.x < (lap.x + 3)) and (player1.y > lap.y and player1.y < (lap.y + 70)) then
p1laps = p1laps + 1
player1bridgecounter = player1bridgecounter + 1
end

--ANTI CHEAT SYSTEM--

if (pad:left() and player1.x > lap.x and player1.x < (lap.x + 30)) and (player1.y > lap.y and player1.y < (lap.y + 70)) then
player1.x = player1.x + 2
end


--BRIDGE COLLISION--

if (pad:right() and player1.x > player1bridgeactivator.x and player1.x < (player1bridgeactivator.x + 3)) and (player1.y > player1bridgeactivator.y and player1.y < (player1bridgeactivator.y + 97)) then
player1bridgecounter = player1bridgecounter + 1
end

--BRIDGE TOP--

if (pad:left() and player1.x > player1bridgetop.x and player1.x < (player1bridgetop.x + 97)) and (player1.y > player1bridgetop.y and player1.y < (player1bridgetop.y + 50)) then
player1.x = player1.x + 2
end

if (pad:right() and player1.x > player1bridgetop.x and player1.x < (player1bridgetop.x + 97)) and (player1.y > player1bridgetop.y and player1.y < (player1bridgetop.y + 50)) then
player1.x = player1.x - 2
end

--BRIDGE BOTTOM--

if (player1.x > player1bridgebottom.x and player1.x < (player1bridgebottom.x + 97)) and (player1.y > player1bridgebottom.y and player1.y < (player1bridgebottom.y + 65)) then
screen:blit(125,129,bridge)
end

if (pad:left() and player1.x > player1bridgebottom.x and player1.x < (player1bridgebottom.x + 97)) and (player1.y > player1bridgebottom.y and player1.y < (player1bridgebottom.y + 65)) then
end

if (pad:right() and player1.x > player1bridgebottom.x and player1.x < (player1bridgebottom.x + 97)) and (player1.y > player1bridgebottom.y and player1.y < (player1bridgebottom.y + 65)) then
end

if (pad:down() and player1.x > player1bridgebottom.x and player1.x < (player1bridgebottom.x + 97)) and (player1.y > player1bridgebottom.y and player1.y < (player1bridgebottom.y + 65)) then
player1.y = player1.y - 2
end

if (pad:up() and player1.x > player1bridgebottom.x and player1.x < (player1bridgebottom.x + 97)) and (player1.y > player1bridgebottom.y and player1.y < (player1bridgebottom.y + 65)) then
player1.y = player1.y + 2
end


--BRIDGE COUNTER--

if player1bridgecounter == 0 then
end

if player1bridgecounter == 1 then
player1bridgebottom.x = 400
player1bridgebottom.y = 400
player1bridgetop.x = 95
player1bridgetop.y = 120
end

if player1bridgecounter == 2 then
player1bridgebottom.x = 95
player1bridgebottom.y = 120
player1bridgetop.x = 400
player1bridgetop.y = 400
end

if player1bridgecounter == 3 then
player1bridgebottom.x = 400
player1bridgebottom.y = 400
player1bridgetop.x = 95
player1bridgetop.y = 120
end

if player1bridgecounter == 4 then
player1bridgebottom.x = 95
player1bridgebottom.y = 120
player1bridgetop.x = 400
player1bridgetop.y = 400
end

if player1bridgecounter == 5 then
player1bridgebottom.x = 400
player1bridgebottom.y = 400
player1bridgetop.x = 95
player1bridgetop.y = 120
end

if player1bridgecounter == 6 then
player1bridgebottom.x = 95
player1bridgebottom.y = 120
player1bridgetop.x = 400
player1bridgetop.y = 400
end

if player1bridgecounter == 7 then
player1bridgebottom.x = 400
player1bridgebottom.y = 400
player1bridgetop.x = 95
player1bridgetop.y = 120
end

if player1bridgecounter == 8 then
player1bridgebottom.x = 95
player1bridgebottom.y = 120
player1bridgetop.x = 400
player1bridgetop.y = 400
end

if player1bridgecounter == 9 then
player1bridgebottom.x = 400
player1bridgebottom.y = 400
player1bridgetop.x = 95
player1bridgetop.y = 120
end

if player1bridgecounter == 10 then
player1bridgebottom.x = 95
player1bridgebottom.y = 120
player1bridgetop.x = 400
player1bridgetop.y = 400
end

if player1bridgecounter == 11 then
player1bridgebottom.x = 400
player1bridgebottom.y = 400
player1bridgetop.x = 95
player1bridgetop.y = 120
end



--TRACK COLLISION--


--TRACK MIDDLE TOP LEFT LINE COLLISION--

if (pad:up() and player1.x > trackmiddletopleftline.x and player1.x < (trackmiddletopleftline.x + 100)) and (player1.y > trackmiddletopleftline.y and player1.y < (trackmiddletopleftline.y + 10)) then
player1.y = player1.y + 2
end

if (pad:down() and player1.x > trackmiddletopleftline.x and player1.x < (trackmiddletopleftline.x + 100)) and (player1.y > trackmiddletopleftline.y and player1.y < (trackmiddletopleftline.y + 10)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddletopleftline.x and player1.x < (trackmiddletopleftline.x + 100)) and (player1.y > trackmiddletopleftline.y and player1.y < (trackmiddletopleftline.y + 10)) then
end

if (pad:right() and player1.x > trackmiddletopleftline.x and player1.x < (trackmiddletopleftline.x + 100)) and (player1.y > trackmiddletopleftline.y and player1.y < (trackmiddletopleftline.y + 10)) then
player1.x = player1.x - 2
end


--TRACK MIDDLE RIGHT RECTANGLE COLLISION--

if (pad:up() and player1.x > trackmiddlerightrectangle.x and player1.x < (trackmiddlerightrectangle.x + 145)) and (player1.y > trackmiddlerightrectangle.y and player1.y < (trackmiddlerightrectangle.y + 110)) then
player1.y = player1.y + 2
end

if (pad:down() and player1.x > trackmiddlerightrectangle.x and player1.x < (trackmiddlerightrectangle.x + 145)) and (player1.y > trackmiddlerightrectangle.y and player1.y < (trackmiddlerightrectangle.y + 110)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddlerightrectangle.x and player1.x < (trackmiddlerightrectangle.x + 145)) and (player1.y > trackmiddlerightrectangle.y and player1.y < (trackmiddlerightrectangle.y + 110)) then
player1.x = player1.x + 2
player1.y = player1.y + 2
end

if (pad:right() and player1.x > trackmiddlerightrectangle.x and player1.x < (trackmiddlerightrectangle.x + 145)) and (player1.y > trackmiddlerightrectangle.y and player1.y < (trackmiddlerightrectangle.y + 110)) then
player1.x = player1.x - 2
end

--TRACK MIDDLE MIDDLE RECTANGLE COLLISION--

if (pad:up() and player1.x > trackmiddlemiddlerectangle.x and player1.x < (trackmiddlemiddlerectangle.x + 180)) and (player1.y > trackmiddlemiddlerectangle.y and player1.y < (trackmiddlemiddlerectangle.y + 70)) then
player1.y = player1.y + 2
end

if (pad:down() and player1.x > trackmiddlemiddlerectangle.x and player1.x < (trackmiddlemiddlerectangle.x + 180)) and (player1.y > trackmiddlemiddlerectangle.y and player1.y < (trackmiddlemiddlerectangle.y + 70)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddlemiddlerectangle.x and player1.x < (trackmiddlemiddlerectangle.x + 180)) and (player1.y > trackmiddlemiddlerectangle.y and player1.y < (trackmiddlemiddlerectangle.y + 70)) then
end

if (pad:right() and player1.x > trackmiddlemiddlerectangle.x and player1.x < (trackmiddlemiddlerectangle.x + 180)) and (player1.y > trackmiddlemiddlerectangle.y and player1.y < (trackmiddlemiddlerectangle.y + 70)) then
player1.x = player1.x - 2
end

--TRACK MIDDLE BOTTOM RECTANGLE COLLISION--

if (pad:down() and player1.x > trackmiddlebottomrectangle.x and player1.x < (trackmiddlebottomrectangle.x + 30)) and (player1.y > trackmiddlebottomrectangle.y and player1.y < (trackmiddlebottomrectangle.y + 80)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddlebottomrectangle.x and player1.x < (trackmiddlebottomrectangle.x + 30)) and (player1.y > trackmiddlebottomrectangle.y and player1.y < (trackmiddlebottomrectangle.y + 80)) then
player1.x = player1.x + 2
end

if (pad:right() and player1.x > trackmiddlebottomrectangle.x and player1.x < (trackmiddlebottomrectangle.x + 30)) and (player1.y > trackmiddlebottomrectangle.y and player1.y < (trackmiddlebottomrectangle.y + 80)) then
player1.x = player1.x - 2
end

--TRACK MIDDLE BOTTOM LEFT LINE COLLISION--

if (pad:up() and player1.x > trackmiddlebottomleftline.x and player1.x < (trackmiddlebottomleftline.x + 40)) and (player1.y > trackmiddlebottomleftline.y and player1.y < (trackmiddlebottomleftline.y + 10)) then
player1.y = player1.y + 2
end

if (pad:down() and player1.x > trackmiddlebottomleftline.x and player1.x < (trackmiddlebottomleftline.x + 40)) and (player1.y > trackmiddlebottomleftline.y and player1.y < (trackmiddlebottomleftline.y + 10)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddlebottomleftline.x and player1.x < (trackmiddlebottomleftline.x + 40)) and (player1.y > trackmiddlebottomleftline.y and player1.y < (trackmiddlebottomleftline.y + 10)) then
player1.x = player1.x + 2
end

if (pad:right() and player1.x > trackmiddlebottomleftline.x and player1.x < (trackmiddlebottomleftline.x + 40)) and (player1.y > trackmiddlebottomleftline.y and player1.y < (trackmiddlebottomleftline.y + 10)) then
player1.x = player1.x - 2
end

--TRACK MIDDLE MIDDLE LEFT LINE COLLISION--

if (pad:up() and player1.x > trackmiddlemiddleleftline.x and player1.x < (trackmiddlemiddleleftline.x + 100)) and (player1.y > trackmiddlemiddleleftline.y and player1.y < (trackmiddlemiddleleftline.y + 20)) then
player1.y = player1.y + 2
end

if (pad:down() and player1.x > trackmiddlemiddleleftline.x and player1.x < (trackmiddlemiddleleftline.x + 100)) and (player1.y > trackmiddlemiddleleftline.y and player1.y < (trackmiddlemiddleleftline.y + 20)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddlemiddleleftline.x and player1.x < (trackmiddlemiddleleftline.x + 100)) and (player1.y > trackmiddlemiddleleftline.y and player1.y < (trackmiddlemiddleleftline.y + 20)) then
player1.x = player1.x + 2
end



--PLAYER 2 COLLISION--



if (player2.x > lap.x and player2.x < (lap.x + 3)) and (player2.y > lap.y and player2.y < (lap.y + 70)) then
p2laps = p2laps + 1
player2bridgecounter = player2bridgecounter + 1
end


--ANTI CHEAT SYSTEM--

if (pad:square() and player2.x > lap.x and player2.x < (lap.x + 30)) and (player2.y > lap.y and player2.y < (lap.y + 70)) then
player2.x = player2.x + 2
end

--BRIDGE COLLISION--

if (pad:circle() and player2.x > player2bridgeactivator.x and player2.x < (player2bridgeactivator.x + 3)) and (player2.y > player2bridgeactivator.y and player2.y < (player2bridgeactivator.y + 97)) then
player2bridgecounter = player2bridgecounter + 1
end

--BRIDGE TOP--

if (pad:square() and player2.x > player2bridgetop.x and player2.x < (player2bridgetop.x + 97)) and (player2.y > player2bridgetop.y and player2.y < (player2bridgetop.y + 60)) then
player2.x = player2.x + 2
end

if (pad:circle() and player2.x > player2bridgetop.x and player2.x < (player2bridgetop.x + 97)) and (player2.y > player2bridgetop.y and player2.y < (player2bridgetop.y + 60)) then
player2.x = player2.x - 2
end

--BRIDGE BOTTOM--

if (player2.x > player2bridgebottom.x and player2.x < (player2bridgebottom.x + 97)) and (player2.y > player2bridgebottom.y and player2.y < (player2bridgebottom.y + 65)) then
screen:blit(125,129,bridge)
end

if (pad:square() and player2.x > player2bridgebottom.x and player2.x < (player2bridgebottom.x + 97)) and (player2.y > player2bridgebottom.y and player2.y < (player2bridgebottom.y + 65)) then
end

if (pad:circle() and player2.x > player2bridgebottom.x and player2.x < (player2bridgebottom.x + 97)) and (player2.y > player2bridgebottom.y and player2.y < (player2bridgebottom.y + 65)) then
end

if (pad:cross() and player2.x > player2bridgebottom.x and player2.x < (player2bridgebottom.x + 97)) and (player2.y > player2bridgebottom.y and player2.y < (player2bridgebottom.y + 65)) then
player2.y = player2.y - 2
end

if (pad:triangle() and player2.x > player2bridgebottom.x and player2.x < (player2bridgebottom.x + 97)) and (player2.y > player2bridgebottom.y and player2.y < (player2bridgebottom.y + 65)) then
player2.y = player2.y + 2
end


--BRIDGE COUNTER--

if player2bridgecounter == 0 then
end

if player2bridgecounter == 1 then
player2bridgebottom.x = 400
player2bridgebottom.y = 400
player2bridgetop.x = 95
player2bridgetop.y = 120
end

if player2bridgecounter == 2 then
player2bridgebottom.x = 95
player2bridgebottom.y = 120
player2bridgetop.x = 400
player2bridgetop.y = 400
end

if player2bridgecounter == 3 then
player2bridgebottom.x = 400
player2bridgebottom.y = 400
player2bridgetop.x = 95
player2bridgetop.y = 120
end

if player2bridgecounter == 4 then
player2bridgebottom.x = 95
player2bridgebottom.y = 120
player2bridgetop.x = 400
player2bridgetop.y = 400
end

if player2bridgecounter == 5 then
player2bridgebottom.x = 400
player2bridgebottom.y = 400
player2bridgetop.x = 95
player2bridgetop.y = 120
end

if player2bridgecounter == 6 then
player2bridgebottom.x = 95
player2bridgebottom.y = 120
player2bridgetop.x = 400
player2bridgetop.y = 400
end

if player2bridgecounter == 7 then
player2bridgebottom.x = 400
player2bridgebottom.y = 400
player2bridgetop.x = 95
player2bridgetop.y = 120
end

if player2bridgecounter == 8 then
player2bridgebottom.x = 95
player2bridgebottom.y = 120
player2bridgetop.x = 400
player2bridgetop.y = 400
end

if player2bridgecounter == 9 then
player2bridgebottom.x = 400
player2bridgebottom.y = 400
player2bridgetop.x = 95
player2bridgetop.y = 120
end

if player2bridgecounter == 10 then
player2bridgebottom.x = 95
player2bridgebottom.y = 120
player2bridgetop.x = 400
player2bridgetop.y = 400
end

if player2bridgecounter == 11 then
player2bridgebottom.x = 400
player2bridgebottom.y = 400
player2bridgetop.x = 95
player2bridgetop.y = 120
end



--TRACK COLLISION--

--TRACK MIDDLE TOP LEFT LINE COLLISION--

if (pad:triangle() and player2.x > trackmiddletopleftline.x and player2.x < (trackmiddletopleftline.x + 100)) and (player2.y > trackmiddletopleftline.y and player2.y < (trackmiddletopleftline.y + 10)) then
player2.y = player2.y + 2
end

if (pad:cross() and player2.x > trackmiddletopleftline.x and player2.x < (trackmiddletopleftline.x + 100)) and (player2.y > trackmiddletopleftline.y and player2.y < (trackmiddletopleftline.y + 10)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddletopleftline.x and player2.x < (trackmiddletopleftline.x + 100)) and (player2.y > trackmiddletopleftline.y and player2.y < (trackmiddletopleftline.y + 10)) then
end

if (pad:circle() and player2.x > trackmiddletopleftline.x and player2.x < (trackmiddletopleftline.x + 100)) and (player2.y > trackmiddletopleftline.y and player2.y < (trackmiddletopleftline.y + 10)) then
player2.x = player2.x - 2
end

--TRACK MIDDLE RIGHT RECTANGLE COLLISION--

if (pad:triangle() and player2.x > trackmiddlerightrectangle.x and player2.x < (trackmiddlerightrectangle.x + 145)) and (player2.y > trackmiddlerightrectangle.y and player2.y < (trackmiddlerightrectangle.y + 110)) then
player2.y = player2.y + 2
end

if (pad:cross() and player2.x > trackmiddlerightrectangle.x and player2.x < (trackmiddlerightrectangle.x + 145)) and (player2.y > trackmiddlerightrectangle.y and player2.y < (trackmiddlerightrectangle.y + 110)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddlerightrectangle.x and player2.x < (trackmiddlerightrectangle.x + 145)) and (player2.y > trackmiddlerightrectangle.y and player2.y < (trackmiddlerightrectangle.y + 110)) then
player2.x = player2.x + 2
player2.y = player2.y + 2
end

if (pad:circle() and player2.x > trackmiddlerightrectangle.x and player2.x < (trackmiddlerightrectangle.x + 145)) and (player2.y > trackmiddlerightrectangle.y and player2.y < (trackmiddlerightrectangle.y + 110)) then
player2.x = player2.x - 2
end

--TRACK MIDDLE MIDDLE RECTANGLE COLLISION--

if (pad:triangle() and player2.x > trackmiddlemiddlerectangle.x and player2.x < (trackmiddlemiddlerectangle.x + 180)) and (player2.y > trackmiddlemiddlerectangle.y and player2.y < (trackmiddlemiddlerectangle.y + 70)) then
player2.y = player2.y + 2
end

if (pad:cross() and player2.x > trackmiddlemiddlerectangle.x and player2.x < (trackmiddlemiddlerectangle.x + 180)) and (player2.y > trackmiddlemiddlerectangle.y and player2.y < (trackmiddlemiddlerectangle.y + 70)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddlemiddlerectangle.x and player2.x < (trackmiddlemiddlerectangle.x + 180)) and (player2.y > trackmiddlemiddlerectangle.y and player2.y < (trackmiddlemiddlerectangle.y + 70)) then
end

if (pad:circle() and player2.x > trackmiddlemiddlerectangle.x and player2.x < (trackmiddlemiddlerectangle.x + 180)) and (player2.y > trackmiddlemiddlerectangle.y and player2.y < (trackmiddlemiddlerectangle.y + 70)) then
player2.x = player2.x - 2
end

--TRACK MIDDLE BOTTOM RECTANGLE COLLISION--

if (pad:cross() and player2.x > trackmiddlebottomrectangle.x and player2.x < (trackmiddlebottomrectangle.x + 30)) and (player2.y > trackmiddlebottomrectangle.y and player2.y < (trackmiddlebottomrectangle.y + 80)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddlebottomrectangle.x and player2.x < (trackmiddlebottomrectangle.x + 30)) and (player2.y > trackmiddlebottomrectangle.y and player2.y < (trackmiddlebottomrectangle.y + 80)) then
player2.x = player2.x + 2
end

if (pad:circle() and player2.x > trackmiddlebottomrectangle.x and player2.x < (trackmiddlebottomrectangle.x + 30)) and (player2.y > trackmiddlebottomrectangle.y and player2.y < (trackmiddlebottomrectangle.y + 80)) then
player2.x = player2.x - 2
end

--TRACK BOTTOM LEFT LINE COLLISION--

if (pad:triangle() and player2.x > trackmiddlebottomleftline.x and player2.x < (trackmiddlebottomleftline.x + 40)) and (player2.y > trackmiddlebottomleftline.y and player2.y < (trackmiddlebottomleftline.y + 10)) then
player2.y = player2.y + 2
end

if (pad:cross() and player2.x > trackmiddlebottomleftline.x and player2.x < (trackmiddlebottomleftline.x + 40)) and (player2.y > trackmiddlebottomleftline.y and player2.y < (trackmiddlebottomleftline.y + 10)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddlebottomleftline.x and player2.x < (trackmiddlebottomleftline.x + 40)) and (player2.y > trackmiddlebottomleftline.y and player2.y < (trackmiddlebottomleftline.y + 10)) then
player2.x = player2.x + 2
end

if (pad:circle() and player2.x > trackmiddlebottomleftline.x and player2.x < (trackmiddlebottomleftline.x + 40)) and (player2.y > trackmiddlebottomleftline.y and player2.y < (trackmiddlebottomleftline.y + 10)) then
player2.x = player2.x - 2
end

--TRACK MIDDLE MIDDLE LEFT LINE COLLISION--

if (pad:triangle() and player2.x > trackmiddlemiddleleftline.x and player2.x < (trackmiddlemiddleleftline.x + 100)) and (player2.y > trackmiddlemiddleleftline.y and player2.y < (trackmiddlemiddleleftline.y + 20)) then
player2.y = player2.y + 2
end

if (pad:cross() and player2.x > trackmiddlemiddleleftline.x and player2.x < (trackmiddlemiddleleftline.x + 100)) and (player2.y > trackmiddlemiddleleftline.y and player2.y < (trackmiddlemiddleleftline.y + 20)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddlemiddleleftline.x and player2.x < (trackmiddlemiddleleftline.x + 100)) and (player2.y > trackmiddlemiddleleftline.y and player2.y < (trackmiddlemiddleleftline.y + 20)) then
player2.x = player2.x + 2
end



--WINS SYSTEM--

if p1laps == 6 and p2laps < 6 then
p1wins = p1wins + 1
end

if p2laps == 6 and p1laps < 6 then
p2wins = p2wins + 1
end


if p1wins == 0 then
screen:print(20,260,"Player 1 wins = 0",blue)
end

if p1wins == 1 then
screen:print(20,260,"Player 1 wins = 1",blue)
end

if p1wins == 2 then
screen:print(20,260,"Player 1 wins = 2",blue)
end

if p1wins == 3 then
screen:print(20,260,"Player 1 wins = 3",blue)
end

if p1wins == 4 then
screen:print(20,260,"Player 1 wins = 4",blue)
end



if p2wins == 0 then
screen:print(320,260,"Player 2 wins = 0",red)
end

if p2wins == 1 then
screen:print(320,260,"Player 2 wins = 1",red)
end

if p2wins == 2 then
screen:print(320,260,"Player 2 wins = 2",red)
end

if p2wins == 3 then
screen:print(320,260,"Player 2 wins = 3",red)
end

if p2wins == 4 then
screen:print(320,260,"Player 2 wins = 4",red)
end



--PLAYER 1 LAP SYSTEM--

if p1laps == 0 then
screen:fontPrint(font1,275,130,"Lap",blue)
screen:fontPrint(font1,275,160,"1/6",blue)
end

if p1laps == 1 then
screen:fontPrint(font1,275,130,"Lap",blue)
screen:fontPrint(font1,275,160,"2/6",blue)
end

if p1laps == 2 then
screen:fontPrint(font1,275,130,"Lap",blue)
screen:fontPrint(font1,275,160,"3/6",blue)
end

if p1laps == 3 then
screen:fontPrint(font1,275,130,"Lap",blue)
screen:fontPrint(font1,275,160,"4/6",blue)
end

if p1laps == 4 then
screen:fontPrint(font1,275,130,"Lap",blue)
screen:fontPrint(font1,275,160,"5/6",blue)
end

if p1laps == 5 then
screen:fontPrint(font1,275,130,"Final",blue)
screen:fontPrint(font1,278,160,"Lap",blue)
end

if p1laps == 6 and p2laps < 6 then
screen:clear()
screen:blit(0,0,player1won)
screen:flip()
screen.waitVblankStart(250)
dofile("FILES/MULTIPLAYER/championshiprace5menu.lua")
end

--PLAYER 2 LAP SYSTEM--

if p2laps == 0 then
screen:fontPrint(font1,340,130,"Lap",red)
screen:fontPrint(font1,340,160,"1/6",red)
end

if p2laps == 1 then
screen:fontPrint(font1,340,130,"Lap",red)
screen:fontPrint(font1,340,160,"2/6",red)
end

if p2laps == 2 then
screen:fontPrint(font1,340,130,"Lap",red)
screen:fontPrint(font1,340,160,"3/6",red)
end

if p2laps == 3 then
screen:fontPrint(font1,340,130,"Lap",red)
screen:fontPrint(font1,340,160,"4/6",red)
end

if p2laps == 4 then
screen:fontPrint(font1,340,130,"Lap",red)
screen:fontPrint(font1,340,160,"5/6",red)
end

if p2laps == 5 then
screen:fontPrint(font1,340,130,"Final",red)
screen:fontPrint(font1,343,160,"Lap",red)
end

if p2laps == 6 and p1laps < 6 then
screen:clear()
screen:blit(0,0,player2won)
screen:flip()
screen.waitVblankStart(250)
dofile("FILES/MULTIPLAYER/championshiprace5menu.lua")
end

--PLAYERS DRAW--

if p2laps == 6 and p1laps == 6 then
screen:clear()
screen:blit(0,0,playersdraw)
screen:flip()
screen.waitVblankStart(250)
dofile("FILES/MULTIPLAYER/championshiprace5menu.lua")
end



collectgarbage()
screen.waitVblankStart()
screen.flip()
end