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


-------Images
bg = Image.load("images/minigames/bounce-bg.png")
link = Image.load("images/link/right1.png")
gover = Image.load("images/menu/gameover.png")
getready = Image.load("images/menu/getready.png")


p1 = Image.load("images/enemies/peahat1.png")
p2 = Image.load("images/enemies/peahat2.png")

bp1 = Image.load("images/enemies/blue-peahat1.png")
bp2 = Image.load("images/enemies/blue-peahat2.png")

gp1 = Image.load("images/enemies/green-peahat1.png")
gp2 = Image.load("images/enemies/green-peahat2.png")

---It isn't coins!!1
Coin = { }
Coin.x = 20
Coin.y = 50
Coin.img = p1
Coin.speedx = 7
Coin.speedy = 7

Coin2 = { }
Coin2.x = 300
Coin2.y = 100
Coin2.img = bp2
Coin2.speedx = 6
Coin2.speedy = 5

Coin3 = { }
Coin3.x = 200
Coin3.y = 100
Coin3.img = gp1
Coin3.speedx = 8
Coin3.speedy = 5
-------


Player = { }
Player.x = 1
Player.y = 136
Player.img = link
Direction = "up"


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

------Functions

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 enemyani()
timer2 = timer2 + 1

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

        if timer2==10 then
        Coin.img = p1
        Coin2.img = bp1
        Coin3.img = gp1
        end

        if timer2==20 then
        Coin.img = p2
        Coin2.img = bp2
        Coin3.img = gp2
        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




function bounce()

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

 if Coin.x > 420 or Coin.x < 0 then
  Coin.speedx = 0-Coin.speedx
 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 Coin.x < 2 then
score = score - 10
Coin.x = 200
Coin.y = 100
end



end


function bounce2()

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

 if Coin2.x > 420 or Coin2.x < 0 then
 Coin2.speedx = 0-Coin2.speedx
 end

 if (Player.x + 32 > Coin2.x) and (Player.x < Coin2.x + 32) and (Player.y + 32 > Coin2.y) and (Player.y < Coin2.y + 32) then
if Direction == "up" then
 Coin2.speedx = 0-Coin2.speedx
 Coin2.speedy = 1+Coin2.speedy
 end
 
 if Direction == "down" then
 Coin2.speedx = 0-Coin2.speedx
 Coin2.speedy = 1-Coin2.speedy
 end 
 
 score = score + 1
 end
 

 if (Coin2.x + 20 > Coin.x) and (Coin2.x < Coin.x + 20) and (Coin2.y + 40 > Coin.y) and (Coin2.y < Coin.y + 40) then
  Coin.speedy = 0-Coin.speedy
  Coin.speedx = 0-Coin.speedx
 
  Coin2.speedy = 0-Coin2.speedy
  Coin2.speedx = 0-Coin2.speedx
 end
 
 
if Coin2.x < 2 then
score = score - 10
Coin2.x = 300
Coin2.y = 100
end

end




function bounce3()

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

 if Coin3.x > 420 or Coin3.x < 0 then
 Coin3.speedx = 0-Coin3.speedx
 end

 if (Player.x + 32 > Coin3.x) and (Player.x < Coin3.x + 32) and (Player.y + 32 > Coin3.y) and (Player.y < Coin3.y + 32) then
if Direction == "up" then
 Coin3.speedx = 0-Coin3.speedx
 Coin3.speedy = 1+Coin3.speedy
 end
 
 if Direction == "down" then
 Coin3.speedx = 0-Coin3.speedx
 Coin3.speedy = 1-Coin3.speedy
 end 
 
 score = score + 1
 end
 

 if (Coin3.x + 20 > Coin.x) and (Coin3.x < Coin.x + 20) and (Coin3.y + 40 > Coin.y) and (Coin3.y < Coin.y + 40) then
  Coin.speedy = 0-Coin.speedy
  Coin.speedx = 0-Coin.speedx
 
  Coin3.speedy = 0-Coin3.speedy
  Coin3.speedx = 0-Coin3.speedx
 end
 
  if (Coin3.x + 20 > Coin2.x) and (Coin3.x < Coin2.x + 20) and (Coin3.y + 40 > Coin2.y) and (Coin3.y < Coin2.y + 40) then
  Coin2.speedy = 0-Coin2.speedy
  Coin2.speedx = 0-Coin2.speedx
 
  Coin3.speedy = 0-Coin3.speedy
  Coin3.speedx = 0-Coin3.speedx
 end
 


if Coin3.x < 2 then
score = score - 10
Coin3.x = 400
Coin3.y = 100
end

end
------------




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

screen:print(60,135,"Avoid missing              to get high score!",white)
screen:blit(170,125,Coin.img)
screen:blit(205,127,Coin2.img)
screen:blit(235,125,Coin3.img)

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

screen:print(400,70,"=Bouncers",white)
screen:blit(300,60,Coin.img)
screen:blit(335,63,Coin2.img)
screen:blit(365,60,Coin3.img)

screen:print(140,190,"Score < 0 = GAME OVER",white)


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


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



screen:clear()
screen.waitVblankStart()

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

screen:print(350,254,"Score:",white)
screen:print(385,254,"  "..score,white)

screen:print(350,264,"Time:",white)
screen:print(385,264," "..currentTime,white)

screen:print(110,254,"Press START to Exit",white)
screen:print(110,264,"Press SELECT to Restart",white)

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

speedcheck()
bounce()
moveplayer()


if score<0 then

Music.stop()
Music.playFile("music/gameover.xm")

while true do
screen.waitVblankStart()
screen:clear()
screen:blit(0,0,gover)
screen.flip()
pad = Controls.read()
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.lua")
end
end
end

enemyani()


if score>4 then
speedcheck2()
bounce2()
screen:blit(Coin2.x,Coin2.y,Coin2.img)
end

if score > 9 then
speedcheck3()
bounce3()
screen:blit(Coin3.x,Coin3.y,Coin3.img)
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.lua")
end



screen.flip()
end