blue = Color.new(0,0,255)
green = Color.new(0,255,0)
white = Color.new(255,255,255)
red = Color.new(255,0,0)


getready = Image.load("images/menu/getready.png")
chasebg = Image.load("images/minigames/chase-bg.png")
coinpic = Image.load("images/minigames/rupee.png")


left = Image.load("images/link/left1.png")
left2 = Image.load("images/link/left2.png")
right = Image.load("images/link/right1.png")
right2 = Image.load("images/link/right2.png")
up = Image.load("images/link/up1.png")
up2 = Image.load("images/link/up2.png")
down = Image.load("images/link/down1.png")
down2 = Image.load("images/link/down2.png")


rleft = Image.load("images/link/red-left1.png")
rleft2 = Image.load("images/link/red-left2.png")
rright = Image.load("images/link/red-right1.png")
rright2 = Image.load("images/link/red-right2.png")
rup = Image.load("images/link/red-up1.png")
rup2 = Image.load("images/link/red-up2.png")
rdown = Image.load("images/link/red-down1.png")
rdown2 = Image.load("images/link/red-down2.png")



counter = Timer.new()
math.randomseed(os.time()) 

timer = 0

rany = math.random(28,210)
ranx = math.random(25,418)
Coin = { x = ranx, y = rany, img = coinpic }




Player = { x = 40, y = 40, img = right } 
oldplayerx = Player.x
oldplayery = Player.y



Player2 = { x = 400, y = 200, img = rleft }
oldplayer2x = Player2.x
oldplayer2y = Player2.y


function collisionCheck2()
if (Player2.x + 32 > Coin.x) and (Player2.x < Coin.x + 32) and (Player2.y + 32 > Coin.y) and (Player2.y < Coin.y + 32) then
Player2.x = oldplayer2x
Player2.y = oldplayer2y
Player2endLevel()
end
end



function collisionCheck()
if (Player.x + 32 > Coin.x) and (Player.x < Coin.x + 32) and (Player.y + 32 > Coin.y) and (Player.y < Coin.y + 32) then
Player.x = oldplayerx
Player.y = oldplayery
PlayerendLevel()
end
end









function movePlayer()
oldplayerx = Player.x
oldplayery = Player.y

pad = Controls.read()
if pad:left() and Player.x > 25  then
moveleft()
Player.x = Player.x - 6
end

if pad:right() and Player.x < 418 then
moveright()
Player.x = Player.x + 6
end

if pad:up() and Player.y > 28 then
moveup()
Player.y = Player.y - 6
end

if pad:down() and Player.y < 218 then
movedown()
Player.y = Player.y + 6
end
end



function movePlayer2()
oldplayer2x = Player2.x
oldplayer2y = Player2.y

pad = Controls.read()

if pad:square() and Player2.x > 25  then
moveleft2()
Player2.x = Player2.x - 6
end

if pad:circle() and Player2.x < 418 then
moveright2()
Player2.x = Player2.x + 6
end

if pad:triangle() and Player2.y > 28 then
moveup2()
Player2.y = Player2.y - 6
end

if pad:cross() and Player2.y < 218 then
movedown2()
Player2.y = Player2.y + 6
end
end




function Player2endLevel()
Coin.y = math.random(28,210)
Coin.x = math.random(25,418)
rscore = rscore + 1
while true do
screen.waitVblankStart()
screen:clear()
movePlayer()
movePlayer2()
collisionCheck()
collisionCheck2()
screen:blit(0,0,chasebg,false)
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Player2.x,Player2.y,Player2.img)
screen:blit(Coin.x,Coin.y,Coin.img)
screen:print(365,254,"P1 Score:",green)
screen:print(430,254,"  "..Level,white)
screen:print(365,263,"P2 Score:",red)
screen:print(430,263,"  "..rscore,white)
screen:print(100,254,"Press START to Exit",white)
screen:print(100,263,"Press SELECT to Restart",white)
pad = Controls.read()
if pad:start() then
Music.stop()
Music.playFile("music/4mat-menu12.mod")
dofile("index.lua")
end
if pad:select() then
Music.stop()
dofile("chase-mp.lua")
end
screen.flip()
end
end



function PlayerendLevel()
Coin.y = math.random(28,210)
Coin.x = math.random(25,418)
Level = Level+1
while true do
screen.waitVblankStart()
screen:clear()
movePlayer()
movePlayer2()
collisionCheck()
collisionCheck2()
screen:blit(0,0,chasebg,false)
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Player2.x,Player2.y,Player2.img)
screen:blit(Coin.x,Coin.y,Coin.img)
screen:print(365,254,"P1 Score:",green)
screen:print(430,254,"  "..Level,white)
screen:print(365,263,"P2 Score:",red)
screen:print(430,263,"  "..rscore,white)
screen:print(100,254,"Press START to Exit",white)
screen:print(100,263,"Press SELECT to Restart",white)
pad = Controls.read()
if pad:start() then
Music.stop()
Music.playFile("music/4mat-menu12.mod")
dofile("index.lua")
end
if pad:select() then
Music.stop()
dofile("chase-mp.lua")
end
screen.flip()
end
end




----------Player1 movement
function movedown()

timer = timer + 1


   	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player.img = down

        end



        if timer==10 then

        Player.img = down2

        end

end





function moveup()

timer = timer + 1


   	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player.img = up

        end



        if timer==10 then

        Player.img = up2

        end

end




function moveleft()

timer = timer + 1


   	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player.img = left

        end



        if timer==10 then

        Player.img = left2

        end

end



function moveright()

timer = timer + 1

	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player.img = right

        end



        if timer==10 then

        Player.img = right2

        end
end



--------Player2 move
function movedown2()

timer = timer + 1


   	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player2.img = rdown

        end



        if timer==10 then

        Player2.img = rdown2

        end

end





function moveup2()

timer = timer + 1


   	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player2.img = rup

        end



        if timer==10 then

        Player2.img = rup2

        end

end




function moveleft2()

timer = timer + 1


   	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player2.img = rleft

        end



        if timer==10 then

        Player2.img = rleft2

        end

end



function moveright2()

timer = timer + 1

	if timer>= 11 or timer<=0  then

    timer = 0

  	end



        if timer==5 then

        Player2.img = rright

        end



        if timer==10 then

        Player2.img = rright2

        end
end








------
Music.stop()
screen:blit(0,0,getready,false)
screen:print(100,165,"Collect as many    as you can!",white)
screen:blit(225,145,Coin.img)
screen:print(2,30,"Player1=",white)
screen:blit(60,20,Player.img)
screen:print(410,30,"=Player2",white)
screen:blit(375,20,Player2.img)
screen.flip()
screen.waitVblankStart(150)

Music.playFile("music/disco.it", true)
Level = 0
rscore = 0


while true do
screen:clear()

movePlayer()
movePlayer2()
collisionCheck(Coin)
collisionCheck2()


screen:blit(0,0,chasebg,false)
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Player2.x,Player2.y,Player2.img)
screen:blit(Coin.x,Coin.y,Coin.img)

screen:print(365,254,"P1 Score:",green)
screen:print(430,254,"  "..Level,white)
screen:print(365,263,"P2 Score:",red)
screen:print(430,263,"  "..rscore,white)
screen:print(100,254,"Press START to Exit",white)
screen:print(100,263,"Press SELECT to Restart",white)


pad = Controls.read()

if pad:start() then
Music.stop()
Music.playFile("music/4mat-menu12.mod")
dofile("index.lua")
end

if pad:select() then
Music.stop()
dofile("chase-mp.lua")
end




screen.waitVblankStart()
screen.flip()
end