white = Color.new(255,255,255)


-------Images
bg = Image.load("images/minigames/bouncemp-bg.png")
p2 = Image.load("images/link/red-left1.png")
p1 = Image.load("images/link/right1.png")
getready = Image.load("images/menu/getready.png")

Ph1 = Image.load("images/enemies/peahat1.png")
Ph2 = Image.load("images/enemies/peahat2.png")

---
Coin = { }
Coin.x = 20
Coin.y = 50
Coin.img = Ph1
Coin.speedx = 10
Coin.speedy = 10


Player = { }
Player.x = 10
Player.y = 136
Player.img = p1
Direction = "up"

Player2 = { }
Player2.x = 448
Player2.y = 136
Player2.img = p2
Direction2 = "up"





counter = Timer.new()
score = 0
score2 = 0
timer2 = 0

------Functions



---------------Movement
function moveplayer()
pad = Controls.read()

if pad:up() and Player.y > 33 then 
Player.y = Player.y - 7
Direction = "up"
end

if pad:down() and Player.y < 206 then
Player.y = Player.y + 7
Direction = "down"
end

end

function moveplayer2()
pad = Controls.read()

if pad:triangle() and Player2.y > 33 then 
Player2.y = Player2.y - 7
Direction2 = "up"
end

if pad:cross() and Player2.y < 206 then
Player2.y = Player2.y + 7
Direction2 = "down"
end
end
------------------



function speedcheck()
Coin.x = Coin.x + Coin.speedx
Coin.y = Coin.y + Coin.speedy
end

function speedcheck2()
Coin2.x = Coin2.x + Coin2.speedx
Coin2.y = Coin2.y + Coin2.speedy
end

function speedcheck3()
Coin3.x = Coin3.x + Coin3.speedx
Coin3.y = Coin3.y + Coin3.speedy
end



-----------Coin bounce and score
function bounce()

 if Coin.y > 206 or Coin.y < 28 then
  Coin.speedy = 0-Coin.speedy
 end


 if (Player.x + 28 > Coin.x) and (Player.x < Coin.x + 20) and (Player.y + 32 > Coin.y) and (Player.y < Coin.y + 40) then
 
   if Direction == "up" then
    Coin.speedx = 0-Coin.speedx
    Coin.speedy = 1+Coin.speedy
   end
 
   if Direction == "down" then
    Coin.speedx = 0-Coin.speedx
    Coin.speedy = 1-Coin.speedy
   end 
 
   score = score +1
 end
 
  if (Player2.x + 28 > Coin.x) and (Player2.x < Coin.x + 20) and (Player2.y + 32 > Coin.y) and (Player2.y < Coin.y + 40) then
 
   if Direction2 == "up" then
    Coin.speedx = 0-Coin.speedx
    Coin.speedy = 1+Coin.speedy
   end
 
   if Direction2 == "down" then
    Coin.speedx = 0-Coin.speedx
    Coin.speedy = 1-Coin.speedy
   end 
 
   score2 = score2 +1
 end
 
 

if Coin.x < 2 then
score = score - 1
Coin.x = 200
Coin.y = 100
end

if Coin.x > 478 then
score2 = score2 - 1
Coin.x = 200
Coin.y = 100
end


end
------------
function enemyani()
timer2 = timer2 + 1

   	if timer2>= 21 or timer2<=0  then
    timer2 = 0
  	end
  	

        if timer2==10 then
        Coin.img = Ph1
        end

        if timer2==20 then
        Coin.img = Ph2
        end
 end




-------------begin game
screen:blit(0,0,getready,false)

screen:print(90,135,"Avoid missing      to get high score!",white)
screen:blit(200,125,Coin.img)

screen:print(2,40,"Player=",white)
screen:blit(60,30,Player.img)

screen:print(400,40,"=Player 2",white)
screen:blit(365,30,Player2.img)


screen:print(140,190,"Score < 0 = You lose",white)


screen.flip()
screen.waitVblankStart(250)
Music.playFile("music/zelda 1 - dance rmx.xm",true)


---------Main loop
while true do
pad = Controls.read()
counter:start()
currentTime = counter:time()/1000



screen:clear()
screen.waitVblankStart()

screen:blit(0,0,bg,false)

screen:print(330,253,"P1 Score:",white)
screen:print(385,253,"  "..score,white)

screen:print(330,263,"P2 Score:",white)
screen:print(385,263,"  "..score2,white)

screen:print(130,253,"Press START to Exit",white)
screen:print(130,263,"Press SELECT to Restart",white)

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

speedcheck()
bounce()
moveplayer()
moveplayer2()
enemyani()


---------Game over functions
if score < 0 then
Music.stop()

screen:clear()
screen:print(50,50,"Winner: P2",white)
screen:blit(130,50,Player2.img)
screen:print(130,253,"Press START to Exit",white)
screen:print(130,263,"Press SELECT to Restart",white)
if pad:start() then
Music.stop()
Music.playFile("music/4mat-menu12.mod")
dofile("index.lua")
end
if pad:select() then
screen:clear()
Music.stop()
dofile("bounce-mp.lua")
screen.flip()
end
end

if score2 < 0 then
Music.stop()

screen:clear()
screen:print(50,50,"Winner: P1",white)
screen:blit(130,50,Player.img)
screen:print(130,253,"Press START to Exit",white)
screen:print(130,263,"Press SELECT to Restart",white)

if pad:start() then
Music.stop()
Music.playFile("music/4mat-menu12.mod")
dofile("index.lua")
end
if pad:select() then
screen:clear()
Music.stop()
dofile("bounce-mp.lua")
screen.flip()
end
end

-------------------------



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

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



screen.flip()
end