white = Color.new(255,255,255)
green = Color.new(0,255,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 = 1, img = Image.load("images/ztype/enemyship.png"),health = 100 }
Enemy2 = { x = 5, y = 235, img = Image.load("images/ztype/left1.png"),health = 100 }

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

enemybullet = { x = Enemy.x, y = Enemy.y, img = Image.load("images/ztype/enemybullet.png") }


score = 0

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}

Warning = {}
Warning = {x = -200, y = 30, img = warning}

slide = {}

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

healthbar = {}
healthbar.y = 10

Music.stop()


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 enemyshoot()

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


if enemybullet.x > Player.x then
enemybullet.x = enemybullet.x - 1
elseif enemybullet.x < Player.x then
enemybullet.x = enemybullet.x + 1
end
		if enemybullet.y > Player.y then
		enemybullet.y = enemybullet.y - 1
		elseif enemybullet.y < Player.y then
		enemybullet.y = enemybullet.y + 1
		end
end

function enemyanimation1()
Enemy.y = Enemy.y
Enemy.x = Enemy.x + 3

if Enemy.y < Player.y then
Enemy.y = Enemy.y + 2
end
 
if Enemy.y > Player.y then
Enemy.y = Enemy.y - 2
end

 if Enemy.x > 440 then
Enemy.x = -32
 end
 
end

function enemyanimation2()

Enemy.x = Enemy.x + 3


if Enemy.y < 220 then
 Enemy.y = Enemy.y + 3
 end
 
 if Enemy.x > 448 then
Enemy.y = 1
Enemy.x = -40
 end
 end

function enemyanimation3()

Enemy.x = Enemy.x + 3

if Enemy.y < Player.y then
Enemy.y = Enemy.y-2
end
 
 if Enemy.x > 448 then
 Enemy.x = -32
 Enemy.y = 1
 end
end

function enemy2animation1()

Enemy2.x = Enemy2.x + 3

if Enemy2.y > Player.y then
Enemy2.y = Enemy2.y - 2
end

if Enemy2.y < Player.y then
Enemy2.y = Enemy2.y + 2
end
 
 if Enemy2.x > 448 then
 Enemy2.x = -32
 Enemy2.y = 1
 end
end

function enemy2animation2()

Enemy2.x = Enemy2.x + 3

if Enemy2.y > 110 then
 Enemy2.y = Enemy2.y - 3
 end
 
 if Enemy2.x > 448 then
 Enemy2.x = -32
 Enemy2.y = 240
 end
end

function enemy2animation3()

Enemy2.x = Enemy2.x + 3

 
 if Enemy2.x > 448 then
 Enemy2.x = -32
 Enemy2.y = Enemy2.y
 end
end


function astroid1ani()
Enemy3.y = Enemy3.y
Enemy3.x = Enemy3.x + 6

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

function astroid2ani()
Enemy4.y = Enemy4.y
Enemy4.x = Enemy4.x + 3

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

function astroid3ani()
Enemy5.y = Enemy5.y
Enemy5.x = Enemy5.x + 4.5

 if Enemy5.x > 440 then
Enemy5.x = -32
Enemy5.y = math.random(30,250)
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
dofile("ztype-menu.lua")
end

if pad:select() then
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()

collisioncheck4()
collisioncheck5()
collisioncheck6()
collisioncheck7()
collisioncheck8()
collisioncheck9()
collisioncheck10()
collisioncheck11()
collisioncheck12()
gameover()


Enemy.x = Enemy.x + 3

Enemy2.x = Enemy2.x + 3

Enemy3.x = Enemy3.x + 6

Enemy4.x = Enemy4.x + 3

Enemy5.x = Enemy5.x + 4.5



screen:print(1,1,"Score:",green)
screen:print(50,1," " .. score,green)
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:blit(Enemy4.x,Enemy4.y,Enemy4.img)
screen:blit(Enemy5.x,Enemy5.y,Enemy5.img)


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

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

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

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


if Enemy.x>480 and Enemy2.x>480 and Enemy3.x>480 and Enemy4.x>450 and Enemy5.x>450 then
dofile("ztype-boss2.lua")
end

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



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

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

enemyanimation1()
enemyanimation3()
enemyanimation2()
enemy2animation2()
enemy2animation3()
enemy2animation1()
astroid1ani()
astroid2ani()
astroid3ani()

bulletFire()
movePlayer()
collisioncheck()
collisioncheck2()
collisioncheck3()
collisioncheck4()
collisioncheck5()
collisioncheck6()
collisioncheck7()
collisioncheck8()
collisioncheck9()
collisioncheck10()
collisioncheck11()
collisioncheck12()
enemyshoot()
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:blit(Enemy4.x,Enemy4.y,Enemy4.img)
screen:blit(Enemy5.x,Enemy5.y,Enemy5.img)
screen:print(1,1,"Score:",green)
screen:print(50,1," " .. score,green)
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 3-",white)

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

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

if score>29 then
stageclear()
end

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




-----------------------------------------COLLISIONS
function collisioncheck()
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())
Enemy = { x = -32, y = math.random(1,240) , img = Image.load("images/ztype/enemyship.png"),health = 100 }
BulletInfo[i].y = -300
score = score + 1
game()
end
end
end

function collisioncheck2()
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())
Enemy2 = { x = -32, y = math.random(1,240) , img = Image.load("images/ztype/left1.png"),health = 100 }
BulletInfo[i].y = -300
score = score + 1
game()
end
end
end

function collisioncheck3()
for i = 1,5 do
if (enemybullet.x + 10 > BulletInfo[i].x) and (enemybullet.x < BulletInfo[i].x + 10) and (enemybullet.y + 10 > BulletInfo[i].y) and (enemybullet.y < BulletInfo[i].y + 10) then
BulletInfo[i].y = -10
enemybullet = { x = 1, y = Enemy.y, img = Image.load("images/ztype/enemybullet.png") }
score = score + 1
game()
end
end
end

function collisioncheck4()
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 - 5
end
end

function collisioncheck5()
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 - 5
end
end

function collisioncheck6()
if (enemybullet.x + 5 > Player.x) and (enemybullet.x < Player.x + 5) and (enemybullet.y + 5 > Player.y) and (enemybullet.y < Player.y + 5) then
Player.health = Player.health - 100
end
end

function collisioncheck7()
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 collisioncheck8()
if (Enemy4.x + 32 > Player.x) and (Enemy4.x < Player.x + 32) and (Enemy4.y + 32 > Player.y) and (Enemy4.y < Player.y + 32) then
Player.health = Player.health - 30
end
end

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

function collisioncheck10()
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 collisioncheck11()
for i = 1,5 do
if (Enemy4.x + 32 > BulletInfo[i].x) and (Enemy4.x < BulletInfo[i].x + 32) and (Enemy4.y + 32 > BulletInfo[i].y) and (Enemy4.y < BulletInfo[i].y + 32) then
math.randomseed(os.time())
BulletInfo[i].y = -300
game()
end
end
end

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


screen:clear()


screen:blit(0,0,getready)
screen:print(190,56,"-STAGE 3-",white)

screen:print(100,76,"Kill the enemies, or they'll kill you!",white)
screen:print(150,86,"Avoid the asteroids",white)

screen:print(2,130,"Player=",white)
screen:blit(60,120,player)
screen:print(400,130,"=Enemies",white)
screen:blit(300,115,Enemy.img)
screen:blit(360,120,Enemy2.img)

screen:print(160,190,"= Instant Death!",white)
screen:blit(140,190,enemybullet.img)

screen.flip()
screen.waitVblankStart(400)

Music.stop()
Music.playFile("music/unknown-aryx.s3m")

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

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


enemyanimation1()
enemyanimation3()
enemyanimation2()
enemy2animation2()
enemy2animation3()
enemy2animation1()
astroid1ani()
astroid2ani()
astroid3ani()

bulletFire()
movePlayer()
collisioncheck()
collisioncheck2()
collisioncheck3()
collisioncheck4()
collisioncheck5()
collisioncheck6()
collisioncheck7()
collisioncheck8()
collisioncheck9()
collisioncheck10()
collisioncheck11()
collisioncheck12()
enemyshoot()
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:blit(Enemy4.x,Enemy4.y,Enemy4.img)
screen:blit(Enemy5.x,Enemy5.y,Enemy5.img)
screen:print(1,1,"Score:",green)
screen:print(50,1," " .. score,green)
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 3-",white)

pad = Controls.read()

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

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

if Enemy.x == 10 then
enemybulletfunc()
end


if score>29 then
stageclear()
end

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