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

getready = Image.load("images/menu/getready.png")


enemy1 = Image.load("images/enemies/mummy1.png")
enemy2 = Image.load("images/enemies/mummy2.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")



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 = 100, y = 100, img = right } 
oldplayerx = Player.x
oldplayery = Player.y



Enemy = { x = 50, y = 50, img = enemy1 }


function enemycollisionCheck()
if (Enemy.x + 32 > Coin.x) and (Enemy.x < Coin.x + 32) and (Enemy.y + 32 > Coin.y) and (Enemy.y < Coin.y + 32) then
Enemy.x = oldenemyx
Enemy.y = oldenemyy
EnemyendLevel()
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 < 423 then
moveright()
Player.x = Player.x + 6
end

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

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

aX = pad:analogX()
aY = pad:analogY()
      
if aX < -23 and Player.x > 25 then
moveleft()
Player.x = Player.x - 6
end        
      
if aX > 23 and Player.x < 423 then
moveright()
Player.x = Player.x + 6
end 

if aY < -23 and Player.y > 26 then
moveup()
Player.y = Player.y - 6
end        
   
if aY > 23 and Player.y < 215 then
   movedown()
Player.y = Player.y + 6
end 


end

function enemyanimation()
timer = timer + 1
   	if timer>= 11 or timer<=0  then
    timer = 0
  	end

        if timer==5 then
        Enemy.img = enemy1
        end

        if timer==10 then
        Enemy.img = enemy2
        end
end



function catch() 
oldenemyx = Enemy.x
oldenemyy = Enemy.y

if Enemy.x > Coin.x then enemyanimation()
Enemy.x = Enemy.x - 3
elseif Enemy.x < Coin.x then enemyanimation()
Enemy.x = Enemy.x + 3
end
		if Enemy.y > Coin.y then enemyanimation()
		Enemy.y = Enemy.y - 3
		elseif Enemy.y < Coin.y then enemyanimation()
		Enemy.y = Enemy.y + 3
		end
		end

if Enemy.x > 423 then
Enemy.x = oldenemyx
end
if Enemy.y > 215 then	
Enemy.y = oldenemyy
end	
if Enemy.x < 25 then
Enemy.x = oldplayerx
end
if Enemy.y < 26 then
Enemy.y = oldplayery
end


function EnemyendLevel()
Coin.y = math.random(28,210)
Coin.x = math.random(25,418)
enemyLevel = enemyLevel+1
while true do
screen.waitVblankStart()
screen:clear()
movePlayer()
collisionCheck()
catch()
enemycollisionCheck()
screen:blit(0,0,chasebg,false)
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen:blit(Coin.x,Coin.y,Coin.img)
screen:print(310,254,"Player Score:",white)
screen:print(400,254,"  "..Level,white)
screen:print(310,264,"Enemy Score:",white)
screen:print(400,264,"  "..enemyLevel,white)
screen:print(80,264,"Press SELECT to Restart",white)
screen:print(80,254,"Press START to Exit",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.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()
collisionCheck()
catch()
enemycollisionCheck()
screen:blit(0,0,chasebg,false)
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen:blit(Coin.x,Coin.y,Coin.img)
screen:print(310,254,"Player Score:",white)
screen:print(400,254,"  "..Level,white)
screen:print(310,264,"Enemy Score:",white)
screen:print(400,264,"  "..enemyLevel,white)
screen:print(80,264,"Press SELECT to Restart",white)
screen:print(80,254,"Press START to Exit",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.lua")
end
screen.flip()
end
end

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







------
screen:blit(0,0,getready)
screen:print(150,63,"Collect as many     as you can!",white)
screen:blit(280,43,Coin.img)
screen:print(2,30,"Player=",white)
screen:blit(65,20,Player.img)
screen:print(2,100,"Enemy=",white)
screen:blit(55,90,Enemy.img)
screen.flip()
screen.waitVblankStart(250)

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


while true do
screen:clear()

movePlayer()
collisionCheck(Coin)
enemycollisionCheck()
catch()


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

screen:print(310,254,"Player Score:",white)
screen:print(400,254,"  "..Level,white)
screen:print(310,264,"Enemy Score:",white)
screen:print(400,264,"  "..enemyLevel,white)

screen:print(80,264,"Press SELECT to Restart",white)
screen:print(80,254,"Press START to Exit",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.lua")
end

screen.waitVblankStart()
screen.flip()
end