



timer = 0
timer2 = 0
Level = 0
--images
enemy = Image.load("images/enemies/snake1.png")
enemy2 = Image.load("images/enemies/snake2.png")
avoidbg = Image.load("images/minigames/avoid-bg.PNG")
gover = Image.load("images/menu/gameover.png")
getready = Image.load("images/menu/getready.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")



Direction="Right"

XAxisHeight = 32
XAxisWidth = 32

YAxisHeight = 32
YAxisWidth = 32






player = {}
player.x = 20
player.y = 110
player.img = right



--misc
white = Color.new(255,255,255)
counter = Timer.new()
RanNumber = 0
math.randomseed(os.time())

block = enemy

enemies = 12
---Functions


--game over
function gameover()
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
Music.stop()	
dofile("avoid.lua")
end
end
end
--


--collision
function collisionCheck(object)
if (player.x + XAxisWidth > object.x) and (player.x < object.x + object.width) and (player.y + YAxisHeight > object.y) and (player.y < object.y + object.height) then
player.x = oldx
player.y = oldy
gameover()
end
end
--


--enemy animation
function enemyani()
timer2 = timer2 + 1

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

        if timer2==10 then
        block=enemy
        end

        if timer2==20 then
        block=enemy2
        end
 end





--movements
function movedown()
direction = "down"
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()
direction = "up"
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()
direction = "left"
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()
direction = "right"
pad = Controls.read()
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



function moveplayer()

if pad:right() and player.x < 480 then
moveright()
player.x = player.x + 3
end

if pad:up() and player.y > 20 then
moveup()
player.y = player.y - 3
end

if pad:down() and player.y < 217 then
movedown()
player.y= player.y + 3
end

end
--



--restart
function restart()
Level = 0

player.x = 20
player.y = 110
Direction = "Right"

XAxisHeight = 32
XAxisWidth = 32

YAxisHeight = 32
YAxisWidth = 32

Block = {}
Block[1] = { x = 85, y = math.random(20,217), height = block:height(), width = block:width() }
Block[2] = { x = 115, y = math.random(20,217), height = block:height(), width = block:width() }
Block[3] = { x = 135, y = math.random(20,217), height = block:height(), width = block:width() }
Block[4] = { x = 165, y = math.random(20,217), height = block:height(), width = block:width() }
Block[5] = { x = 165, y = math.random(20,217), height = block:height(), width = block:width() }
Block[6] = { x = 195, y = math.random(20,217), height = block:height(), width = block:width() }
Block[7] = { x = 195, y = math.random(20,217), height = block:height(), width = block:width() }
Block[8] = { x = 255, y = math.random(20,217), height = block:height(), width = block:width() }
Block[9] = { x = 285, y = math.random(20,217), height = block:height(), width = block:width() }
Block[10] = { x = 315, y = math.random(20,217), height = block:height(), width = block:width() }
Block[11] = { x = 345, y = math.random(20,217), height = block:height(), width = block:width() }
Block[12] = { x = 345, y = math.random(20,217), height = block:height(), width = block:width() }
 counter:reset(0)
Music.playFile("music/dung.it")
while true do
screen.waitVblankStart()
screen:clear()

currentTimenew = counter:time()/1000


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


 counter:start()
 
 enemyani()

for a = 1,enemies do
collisionCheck(Block[a])
end

for a = 1,enemies do
screen:blit(Block[a].x, Block[a].y,block)
end

moveplayer() 

  
screen:print(320,264,"Time:",white)
screen:print(355,264," ".. currentTime,white)
screen:print(320,254,"Level:",white)
screen:print(355,254,"  "..Level,white)
screen:print(80,264,"Press SELECT to Restart",white)
screen:print(80,254,"Press START to Exit",white)

	screen:blit(player.x, player.y, player.img)

if player.x > 440 then
score()
end
		
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("avoid.lua")
end

screen.flip()
end
end 
--

--new level
function score()
Level = Level + 1



player.x = 20
player.y = 110
Direction="Right"

XAxisHeight = 32
XAxisWidth = 32

YAxisHeight = 32
YAxisWidth = 32

Block = {}
Block[1] = { x = 85, y = math.random(20,217), height = block:height(), width = block:width() }
Block[2] = { x = 115, y = math.random(20,217), height = block:height(), width = block:width() }
Block[3] = { x = 135, y = math.random(20,217), height = block:height(), width = block:width() }
Block[4] = { x = 165, y = math.random(20,217), height = block:height(), width = block:width() }
Block[5] = { x = 165, y = math.random(20,217), height = block:height(), width = block:width() }
Block[6] = { x = 195, y = math.random(20,217), height = block:height(), width = block:width() }
Block[7] = { x = 195, y = math.random(20,217), height = block:height(), width = block:width() }
Block[8] = { x = 255, y = math.random(20,217), height = block:height(), width = block:width() }
Block[9] = { x = 285, y = math.random(20,217), height = block:height(), width = block:width() }
Block[10] = { x = 315, y = math.random(20,217), height = block:height(), width = block:width() }
Block[11] = { x = 345, y = math.random(20,217), height = block:height(), width = block:width() }
Block[12] = { x = 345, y = math.random(20,217), height = block:height(), width = block:width() }
while true do
screen.waitVblankStart()
screen:clear()




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

counter:start()

enemyani()

for a = 1,enemies do
collisionCheck(Block[a])
end

for a = 1,enemies do
screen:blit(Block[a].x, Block[a].y,block)
end

moveplayer() 
currentTimenew = counter:time()/1000
  
screen:print(320,264,"Time:",white)
screen:print(355,264," ".. currentTimenew,white)
screen:print(320,254,"Level:",white)
screen:print(355,254,"  "..Level,white)
screen:print(80,264,"Press SELECT to Restart",white)
screen:print(80,254,"Press START to Exit",white)

	screen:blit(player.x, player.y, player.img)

if player.x > 440 then
score()
end
		
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("avoid.lua")
end

screen.flip()
end
end 
--end of functions





--Player


--Enemies Pos
Block = {}
Block[1] = { x = 85, y = math.random(20,217), height = block:height(), width = block:width() }
Block[2] = { x = 115, y = math.random(20,217), height = block:height(), width = block:width() }
Block[3] = { x = 135, y = math.random(20,217), height = block:height(), width = block:width() }
Block[4] = { x = 165, y = math.random(20,217), height = block:height(), width = block:width() }
Block[5] = { x = 165, y = math.random(20,217), height = block:height(), width = block:width() }
Block[6] = { x = 195, y = math.random(20,217), height = block:height(), width = block:width() }
Block[7] = { x = 195, y = math.random(20,217), height = block:height(), width = block:width() }
Block[8] = { x = 255, y = math.random(20,217), height = block:height(), width = block:width() }
Block[9] = { x = 285, y = math.random(20,217), height = block:height(), width = block:width() }
Block[10] = { x = 315, y = math.random(20,217), height = block:height(), width = block:width() }
Block[11] = { x = 345, y = math.random(20,217), height = block:height(), width = block:width() }
Block[12] = { x = 345, y = math.random(20,217), height = block:height(), width = block:width() }







screen:blit(0,0,getready,false)
screen:print(170,100,"Avoid    and go to the other side!",white)
screen:blit(215,90,enemy)
screen:print(2,70,"Player=",white)
screen:blit(60,60,down)
screen:print(2,130,"Enemies=",white)
screen:blit(65,120,enemy)
screen.flip()
screen.waitVblankStart(150)
Music.playFile("music/laby_storm.xm",true)

--Begin Game
while true do

pad = Controls.read()
screen.waitVblankStart()
screen:clear()
screen:blit(0, 0, avoidbg,  false)

counter:start()

enemyani()

for a = 1,enemies do
collisionCheck(Block[a])
end

for a = 1,enemies do
screen:blit(Block[a].x, Block[a].y,block)
end

currentTime = counter:time()/1000

   moveplayer() 
screen:print(320,264,"Time:",white)
screen:print(355,264," ".. currentTime,white)
screen:print(320,254,"Level:",white)
screen:print(355,254,"  "..Level,white)
screen:print(80,264,"Press SELECT to Restart",white)
screen:print(80,254,"Press START to Exit",white)

	screen:blit(player.x, player.y, player.img)
--Side Collision Check
if player.x > 440 then
score()
end
--
	
	
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("avoid.lua")
end
	
screen.flip()
end 