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

bullet = Image.load("images/ztype/bullet.png")
player = Image.load("images/ztype/ship.png")
scroll1 = Image.load("images/ztype/scroll1.png")
scroll2 = Image.load("images/ztype/scroll2.png")
Gameover = Image.load("images/menu/gameover.png")
getready = Image.load("images/menu/getready.png")

shot = Sound.load("music/shot1.wav",false)

math.randomseed(os.time())

Enemy = { x = 5, y = math.random(30,250), img = Image.load("images/ztype/astroid1.png"),health = 100 }
Enemy2 = { x = 5, y = math.random(30,250), img = Image.load("images/ztype/astroid2.png"),health = 100 }
Enemy3 = { x = 5, y = math.random(30,250), img = Image.load("images/ztype/astroid3.png"),health = 100 }
Enemy4 = { x = -100, y = math.random(30,120), img = Image.load("images/ztype/astroid4.png"),health = 100 }



timer = 0
counter = Timer.new()
counter2 = Timer.new()
counter3 = Timer.new()

oldpad = Controls.read()
currentBullet = 0
direction = "up"
randy = math.random(28,210)
randx = math.random(25,418)

Player = {}
Player = { x = 410, y = 136, health = 100}

slide = {}

slide[1] = { x = 0, y = 0, pic = scroll1 }
slide[2] = { x = -480, y = 0, pic = scroll2 }

healthbar = {}
healthbar.y = 10




BulletInfo = {}
for a = 1,5 do
BulletInfo[a] = { pic = bullet , firing = false, direction = "up", x = Player.x + 64,
y = Player.y + 16 }
end

--Functions


function enemyanimation()
Enemy.y = Enemy.y
Enemy.x = Enemy.x + 6

 if Enemy.x > 440 then
Enemy.x = -32
Enemy.y = math.random(30,250)
 end 

end

 
 function enemy2animation()

Enemy2.x = Enemy2.x + 3

 
 if Enemy2.x > 448 then
 Enemy2.x = -32
 Enemy2.y = math.random(30,250)
 end
end

function enemy3animation()

Enemy3.x = Enemy3.x + 4.5

 
 if Enemy3.x > 448 then
 Enemy3.x = -32
 Enemy3.y = math.random(30,250)
 end
end

function enemy4animation()

Enemy4.x = Enemy4.x + 4.5

 
 if Enemy4.x > 480 then
 dofile("ztype-stage3.lua")
 end

end



function bulletSetup()
shot:play()

if currentBullet < 5 then
currentBullet = currentBullet + 1
else
currentBullet = 1
end

if direction == "up" then
BulletInfo[currentBullet].x = Player.x + 32
BulletInfo[currentBullet].y = Player.y + 16
end
if direction == "down" then
BulletInfo[currentBullet].x = Player.x + 32
BulletInfo[currentBullet].y = Player.y + 16
end

BulletInfo[currentBullet].direction = direction
BulletInfo[currentBullet].firing = true
end

function bulletFire()
for i = 1,5 do
if BulletInfo[i].firing == true then

if BulletInfo[i].direction == "up" then BulletInfo[i].x = BulletInfo[i].x - 10 end
if BulletInfo[i].direction == "down" then BulletInfo[i].x = BulletInfo[i].x - 10 end
screen:blit(BulletInfo[i].x,BulletInfo[i].y,BulletInfo[i].pic)
end
end
end


function movePlayer()
pad = Controls.read()
oldplayerx = Player.x
oldplayery = Player.y

if pad:up() then
Player.y = Player.y - 3
direction = "up"
end
if pad:down() then
Player.y = Player.y + 3
direction = "down"
end


if Player.y > 272 - 32 then
Player.y = oldplayery
end


if Player.y < 30 then
Player.y = oldplayery
end

end



function gameover()
if Player.health < 0 then
Music.stop()
Music.playFile("music/elise.mod")
while true do
pad = Controls.read()
screen:clear()
screen:blit(0,0,Gameover)

if pad:start() then
Music.stop()
dofile("ztype-menu.lua")
end

if pad:select() then
Music.stop()
dofile("ztype-stage2.lua")
end

screen.waitVblankStart()
screen.flip()
end
end
end


function stageclear()
while true do
pad = Controls.read()
screen:clear()

if pad:cross() and oldpad:cross() then
bulletSetup()
end

for blit_slides = 1, 2 do
screen:blit(slide[blit_slides].x, slide[blit_slides].y, slide[blit_slides].pic,false) 
end

for move_slides = 1, 2 do
if move == true then
slide[move_slides].x = slide[move_slides].x + 10
end
end

for repeat_slides = 1, 2 do
if slide[repeat_slides].x > 480 then
slide[repeat_slides].x = -480
end
end

if pad:start() and move == true then
move = false
elseif pad:start() and move == false then
move = true
end

movePlayer()
screen:blit(Player.x,Player.y,player)
bulletFire()
collisioncheck()
collisioncheck2()
collisioncheck3()
collisioncheck4()
collisioncheck5()
collisioncheck6()
collisioncheck7()
gameover()


Enemy.x = Enemy.x + 6

Enemy2.x = Enemy2.x + 3

Enemy3.x = Enemy3.x + 4.5


screen:print(1,1,"Progress:",green)
screen:fillRect(70,1,87,7,green)
screen:print(156,1,"I",red)
screen:print(1,10,"Health:",green)
screen:fillRect(60,10,Player.health,healthbar.y,green)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen:blit(Enemy2.x,Enemy2.y,Enemy2.img)
screen:blit(Enemy3.x,Enemy3.y,Enemy3.img)

screen:print(280,1,"Press START to Exit",white)
screen:print(280,15,"Press SELECT to Restart",white)

if Enemy.x>480 and Enemy2.x>480 and Enemy3.x>480 then
enemy4animation()
screen:blit(Enemy4.x,Enemy4.y,Enemy4.img)
collisioncheck7()
end



screen.waitVblankStart()
screen.flip()
oldpad = pad
end
end



function game()
while true do
screen:clear()
pad = Controls.read()
screen:clear()
counter2:start()


if pad:cross() and pad:cross() ~= oldpad:cross() then
bulletSetup()
end

for blit_slides = 1, 2 do
screen:blit(slide[blit_slides].x, slide[blit_slides].y, slide[blit_slides].pic,false) 
end

for move_slides = 1, 2 do
if move == true then
slide[move_slides].x = slide[move_slides].x + 10
end
end

for repeat_slides = 1, 2 do
if slide[repeat_slides].x > 480 then
slide[repeat_slides].x = -480
end
end

enemyanimation()
enemy2animation()
enemy3animation()

bulletFire()
movePlayer()
collisioncheck()
collisioncheck2()
collisioncheck3()
collisioncheck4()
collisioncheck5()
collisioncheck6()
collisioncheck7()
gameover()


current2Time = counter2:time()/1000

screen:blit(Player.x,Player.y,player)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen:blit(Enemy2.x,Enemy2.y,Enemy2.img)
screen:blit(Enemy3.x,Enemy3.y,Enemy3.img)
screen:print(1,1,"Progress:",green)
screen:fillRect(70,1,current2Time,7,green)
screen:print(156,1,"I",red)
screen:print(1,10,"Health:",green)
screen:fillRect(60,10,Player.health,healthbar.y,green)

screen:print(280,1,"Press START to Exit",white)
screen:print(280,15,"Press SELECT to Restart",white)

screen:print(190,7,"-Stage 2-",white)


if current2Time > 90 then
stageclear()
end

if pad:start() then
Music.stop()
dofile("ztype-menu.lua")
end

if pad:select() then
Music.stop()
dofile("ztype-stage2.lua")
end

oldpad = pad
screen.waitVblankStart()
screen.flip()
end
end






---Collisions
function collisioncheck()
if (Enemy.x + 32 > Player.x) and (Enemy.x < Player.x + 32) and (Enemy.y + 32 > Player.y) and (Enemy.y < Player.y + 32) then
Player.health = Player.health - 30
end
end

function collisioncheck2()
if (Enemy2.x + 32 > Player.x) and (Enemy2.x < Player.x + 32) and (Enemy2.y + 32 > Player.y) and (Enemy2.y < Player.y + 32) then
Player.health = Player.health - 30
end
end

function collisioncheck3()
if (Enemy3.x + 32 > Player.x) and (Enemy3.x < Player.x + 32) and (Enemy3.y + 32 > Player.y) and (Enemy3.y < Player.y + 32) then
Player.health = Player.health - 30
end
end

function collisioncheck4()
for i = 1,5 do
if (Enemy.x + 32 > BulletInfo[i].x) and (Enemy.x < BulletInfo[i].x + 32) and (Enemy.y + 32 > BulletInfo[i].y) and (Enemy.y < BulletInfo[i].y + 32) then
math.randomseed(os.time())
BulletInfo[i].y = -300
game()
end
end
end

function collisioncheck5()
for i = 1,5 do
if (Enemy2.x + 32 > BulletInfo[i].x) and (Enemy2.x < BulletInfo[i].x + 32) and (Enemy2.y + 32 > BulletInfo[i].y) and (Enemy2.y < BulletInfo[i].y + 32) then
math.randomseed(os.time())
BulletInfo[i].y = -300
game()
end
end
end

function collisioncheck6()
for i = 1,5 do
if (Enemy3.x + 32 > BulletInfo[i].x) and (Enemy3.x < BulletInfo[i].x + 32) and (Enemy3.y + 32 > BulletInfo[i].y) and (Enemy3.y < BulletInfo[i].y + 32) then
math.randomseed(os.time())
BulletInfo[i].y = -300
game()
end
end
end


function collisioncheck7()
if (Player.x + 32 > Enemy4.x) and (Player.x < Enemy4.x + 82) and (Player.y + 32 > Enemy4.y) and (Player.y < Enemy4.y + 154) then
Player.health = Player.health - 10
end
end
----


screen:clear()

screen:blit(0,0,getready)
screen:print(190,56,"-STAGE 2-",white)
screen:print(120,76,"Fly through the astroid belt!",white)

screen:print(2,130,"Player=",white)
screen:blit(60,120,player)


screen:print(400,130,"=Astroids",white)
screen:blit(400-35,120,Enemy.img)
screen:blit(400-35-35,120,Enemy2.img)
screen:blit(400-35-35-35,120,Enemy3.img)



screen.flip()
screen.waitVblankStart(400)

Music.stop()
Music.playFile("music/voiders.mod")

move = true
while true do

screen:clear()
pad = Controls.read()
screen:clear()
 counter:start()
if pad:cross() and pad:cross() ~= oldpad:cross() then
bulletSetup()
end

for blit_slides = 1, 2 do
screen:blit(slide[blit_slides].x, slide[blit_slides].y, slide[blit_slides].pic,false) 
end

for move_slides = 1, 2 do
if move == true then
slide[move_slides].x = slide[move_slides].x + 10
end
end

for repeat_slides = 1, 2 do
if slide[repeat_slides].x > 480 then
slide[repeat_slides].x = -480
end
end

currentTime = counter:time()/1000

enemyanimation()
enemy2animation()
enemy3animation()


bulletFire()
movePlayer()
collisioncheck()
collisioncheck2()
collisioncheck3()
collisioncheck4()
collisioncheck5()
collisioncheck6()
collisioncheck7()
gameover()



screen:blit(Player.x,Player.y,player)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen:blit(Enemy2.x,Enemy2.y,Enemy2.img)
screen:blit(Enemy3.x,Enemy3.y,Enemy3.img)
screen:print(1,1,"Progress:",green)
screen:fillRect(70,1,currentTime,7,green)
screen:print(156,1,"I",red)
screen:print(1,10,"Health:",green)
screen:fillRect(60,10,Player.health,healthbar.y,green)


screen:print(280,1,"Press START to Exit",white)
screen:print(280,15,"Press SELECT to Restart",white)

screen:print(190,7,"-Stage 2-",white)


if pad:start() then
dofile("ztype-menu.lua")
end

if pad:select() then
dofile("ztype-stage2.lua")
end


if currentTime > 90 then
stageclear()
end

oldpad = pad
screen.waitVblankStart()
screen.flip()
end