red = Color.new(255,0,0)
green = Color.new(0,255,0)




timer = 0
timer2 = 0
score1 = 0
score2 = 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")


rright = Image.load("images/link/red-right1.png")
rright2 = Image.load("images/link/red-right2.png")
rup = Image.load("images/link/red-up1.png")
rup2 = Image.load("images/link/red-up2.png")
rdown = Image.load("images/link/red-down1.png")
rdown2 = Image.load("images/link/red-down2.png")
rleft = Image.load("images/link/red-left1.png")
--

Direction="Right"
Direction2="Right2"

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

player2 = {}
player2.x = 20
player2.y = 140
player2.img = rright


XAxisHeight = 32
XAxisWidth = 32

YAxisHeight = 32
YAxisWidth = 32

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

block = enemy

enemies = 12
---Functions



--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

player.x = 20
player.y = 100


end
end


function collisionCheck2(object)
if (player2.x + XAxisWidth > object.x) and (player2.x < object.x + object.width) and (player2.y + YAxisHeight > object.y) and (player2.y < object.y + object.height) then
player2.x = oldx2
player2.y = oldy2

player2.x = 20
player2.y = 140


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 movedown2()
timer = timer + 1

   	if timer>= 11 or timer<=0  then
    timer = 0
  	end

        if timer==5 then
        player2.img = rdown
        end

        if timer==10 then
        player2.img = rdown2
        end
end





function moveup2()
timer = timer + 1

   	if timer>= 11 or timer<=0  then
    timer = 0
 	end

        if timer==5 then
        player2.img = rup
        end

        if timer==10 then
        player2.img = rup2
        end
end




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

        if timer==5 then
        player2.img = rright
        end

        if timer==10 then
        player2.img = rright2
        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


function moveplayer2()

if pad:circle() then
moveright2()
player2.x = player2.x+3
end


if pad:triangle() and player2.y > 20 then
moveup2()
player2.y = player2.y - 3
end

if pad:cross() and player2.y < 217 then
movedown2()
player2.y= player2.y + 3
end
end
--




--score1
function ettscore()
score1 = score1 + 1



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

player2 = {}
player2.x = 20
player2.y = 140
player2.img = rright

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])
collisionCheck2(Block[a])
end

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

moveplayer() 
moveplayer2()
currentTimenew = counter:time()/1000
  
screen:print(320,254,"P1 Score:",green)
screen:print(375,254,"  "..score1,white)

screen:print(320,264,"P2 Score:",red)
screen:print(375,264,"  "..score2,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)
		screen:blit(player2.x, player2.y, player2.img)

if player.x > 440 then
ettscore()
end

if player2.x > 440 then
tvascore()
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-mp.lua")
end		

screen.flip()
end
end 


--score2
function tvascore()
score2 = score2 + 1



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

player2 = {}
player2.x = 20
player2.y = 140
player2.img = rright

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])
collisionCheck2(Block[a])
end

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

moveplayer() 
moveplayer2()
currentTimenew = counter:time()/1000
  
screen:print(320,254,"P1 Score:",green)
screen:print(375,254,"  "..score1,white)

screen:print(320,264,"P2 Score:",red)
screen:print(375,264,"  "..score2,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)
		screen:blit(player2.x, player2.y, player2.img)

if player.x > 440 then
ettscore()
end

if player2.x > 440 then
tvascore()
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-mp.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(110,150,"Avoid    and go to the other side!",white)
screen:blit(155,140,enemy)
screen:print(2,30,"Player1=",white)
screen:blit(60,20,player.img)
screen:print(410,30,"=Player2",white)
screen:blit(375,20,rleft)
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])
collisionCheck2(Block[a])
end

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

currentTime = counter:time()/1000

   moveplayer() 
      moveplayer2() 

screen:print(320,254,"P1 Score:",green)
screen:print(375,254,"  "..score1,white)

screen:print(320,264,"P2 Score:",red)
screen:print(375,264,"  "..score2,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)
		screen:blit(player2.x, player2.y, player2.img)
--Side Collision Check
if player.x > 440 then
ettscore()
end

if player2.x > 440 then
tvascore()
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-mp.lua")
end		
	
screen.flip()
end 