--COLOURS--

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

--IMAGES--

track = Image.load("IMAGES/track2.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 = 180

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

--OBJECTS ECT--

trackmiddle = {}
trackmiddle.img = Image.createEmpty(1,1)
trackmiddle.x = 85
trackmiddle.y = 68

--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 = 285
lap.y = 160



--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/championshiprace2.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 + 2)) and (player1.y > lap.y and player1.y < (lap.y + 70)) then
p1laps = p1laps + 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

--TRACK MIDDLE COLLISION--
if (pad:up() and player1.x > trackmiddle.x and player1.x < (trackmiddle.x + 275)) and (player1.y > trackmiddle.y and player1.y < (trackmiddle.y + 95)) then
player1.y = player1.y + 2
end

if (pad:down() and player1.x > trackmiddle.x and player1.x < (trackmiddle.x + 275)) and (player1.y > trackmiddle.y and player1.y < (trackmiddle.y + 95)) then
player1.y = player1.y - 2
end

if (pad:left() and player1.x > trackmiddle.x and player1.x < (trackmiddle.x + 275)) and (player1.y > trackmiddle.y and player1.y < (trackmiddle.y + 95)) then
player1.x = player1.x + 2
player1.y = player1.y - 2
end

if (pad:right() and player1.x > trackmiddle.x and player1.x < (trackmiddle.x + 275)) and (player1.y > trackmiddle.y and player1.y < (trackmiddle.y + 95)) then
player1.x = player1.x - 2
player1.y = player1.y + 2
end


--PLAYER 2 COLLISION--

if (player2.x > lap.x and player2.x < (lap.x + 2)) and (player2.y > lap.y and player2.y < (lap.y + 70)) then
p2laps = p2laps + 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


--TRACK COLLISION--

if (pad:triangle() and player2.x > trackmiddle.x and player2.x < (trackmiddle.x + 275)) and (player2.y > trackmiddle.y and player2.y < (trackmiddle.y + 95)) then
player2.y = player2.y + 2
end

if (pad:cross() and player2.x > trackmiddle.x and player2.x < (trackmiddle.x + 275)) and (player2.y > trackmiddle.y and player2.y < (trackmiddle.y + 95)) then
player2.y = player2.y - 2
end

if (pad:square() and player2.x > trackmiddle.x and player2.x < (trackmiddle.x + 275)) and (player2.y > trackmiddle.y and player2.y < (trackmiddle.y + 95)) then
player2.x = player2.x + 2
player2.y = player2.y - 2
end

if (pad:circle() and player2.x > trackmiddle.x and player2.x < (trackmiddle.x + 275)) and (player2.y > trackmiddle.y and player2.y < (trackmiddle.y + 95)) then
player2.x = player2.x - 2
player2.y = player2.y + 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 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



--PLAYER 1 LAP SYSTEM--

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

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

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

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

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

if p1laps == 5 then
screen:fontPrint(font1,140,140,"Final 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/championshiprace2menu.lua")
end

--PLAYER 2 LAP SYSTEM--

if p2laps == 0 then
screen:fontPrint(font1,265,140,"Lap 1/6",red)
end

if p2laps == 1 then
screen:fontPrint(font1,265,140,"Lap 2/6",red)
end

if p2laps == 2 then
screen:fontPrint(font1,265,140,"Lap 3/6",red)
end

if p2laps == 3 then
screen:fontPrint(font1,265,140,"Lap 4/6",red)
end

if p2laps == 4 then
screen:fontPrint(font1,265,140,"Lap 5/6",red)
end

if p2laps == 5 then
screen:fontPrint(font1,245,140,"Final 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/championshiprace2menu.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/championshiprace2menu.lua")
end



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







































