red = Color.new(255,0,0)
black = Color.new(0,0,0)
blue = Color.new(0,0,255)
green = Color.new(0,255,0)
n = Image.load("minigame/berries.png")
c = Image.load("minigame/candy.png")
land = Image.load("minigame/land.png")
gametimer=Timer.new()
time=30
gametimer:start()

math.randomseed(os.time()) --/// prevent same random numbers from occuring each run of the game///

Player = { x = 300, y = 240, img = c } --/// players position and image info///

-- clear players image to color blue

Enemy = { x = 50, y = 50, img = n } --///enemy's info///

 --/// clear enemy image to green///

function movePlayer() --/// function to move our player when directionals are pressed///
pad = Controls.read()
if pad:left() then
Player.x = Player.x - 3
end
if pad:right() then
Player.x = Player.x + 3
end
if pad:up() then
Player.y = Player.y  - 3
end
if pad:down() then
Player.y = Player.y + 3
end
end

PlayerWidth = 20
PlayerHeight = 20

function collisionCheck()
if (Player.x + PlayerWidth > Enemy.x) and (Player.x < Enemy.x) and (Player.y + PlayerHeight > Enemy.y) and (Player.y < Enemy.y) then
dofile("./loser.lua")
end 
end
function ender()
if time < 1 then
dofile("./win.lua")
end 
end
function chasePlayer() --/// function to make the enemy chase the player///
stallchase = math.random(2) --/// stallchase variable set to random number. this variable prevents the enemy from being too perfect while chasing you.///
if stallchase == 1 then --/// if stallchase resulted to 1 then move x value towards players x value///
if Enemy.x > Player.x then
Enemy.x = Enemy.x - 3
elseif Enemy.x < Player.x then
Enemy.x = Enemy.x + 3
end
end
stallchase = math.random(2)
if stallchase == 1 then --/// if stallchase resulted to 1 then move y value towards players y value///
if Enemy.y > Player.y then
Enemy.y = Enemy.y - 3
elseif Enemy.y < Player.y then
Enemy.y = Enemy.y + 3
end
end
end --///end the function///

--///loop///
while true do
gamestime=gametimer:time()
screen:clear()
if gamestime>1000 then time=time - 1 gametimer:reset(0) gametimer:start()
end
collisionCheck()
ender()
movePlayer() --///call movePlayer function///
screen:blit(0,0,land, true)
chasePlayer() --///call chasePlayer function////

--/// paste player and enemy to screen///
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen:print(220, 25, time, red)

screen.waitVblankStart()
screen.flip()
if pad:select() then screen:save("screenshot.jpg") end
end 

