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


scroll1 = Image.load("images/ztype/scroll1.png")
scroll2 = Image.load("images/ztype/scroll2.png")

gameover = Image.load("images/menu/gameover.png")

bullet = Image.load("images/ztype/bullet.png")
enemybulletpic = Image.load("images/ztype/enemybullet.png")

player = Image.load("images/ztype/ship.png")

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

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}

Enemy = { x = 5, y = 100, img = Image.load("images/ztype/boss1.png"),health = 100 }


enemybullet = { x = Enemy.x, y = Enemy.y, img = enemybulletpic }


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 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 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
Enemy.health = Enemy.health - 0.7

if Enemy.health < 0 then
screen:clear()
while true do
screen:clear()
screen:print(130,10,"Level Complete!",white)
screen:print(100,20,"Press X for -Level 2-",white)


pad = Controls.read()
if pad:cross() then
Music.stop()
dofile("ztype-stage2.lua")
end


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


function collisioncheck2()
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
enemybullet = { x = Enemy.x, y = Enemy.y, img = enemybulletpic }

while true do
screen:clear()
if pad:select() then
    oldpad = pad
    screen:clear()
    
    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 + 2
end
end

for repeat_slides = 1, 2 do
if slide[repeat_slides].x > 480 then
slide[repeat_slides].x = -480
end
end
    screen:print(150,10,"Press CR0SS to Continue",Color.new(255,255,255))
    screen:print(180,1,"-Paused-",Color.new(255,255,255))
    screen.flip()
    while true do
       pad = Controls.read()
   if pad:cross() and pad:cross() ~= oldpad:cross() then 
           break
       end
       screen.waitVblankStart()
       oldpad = pad
    end
    oldpad = pad
    pad = Controls.read()
end


oldpad = Controls.read()


degree = degree + speed
      radian = (degree/180)*math.pi
      Enemy.x = xcenter+math.cos(radian)*radius
      Enemy.y = ycenter-math.sin(radian)*radius



if pad:cross() and oldpad:cross() ~= pad: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



movePlayer()
screen:blit(Player.x,Player.y,player)
bulletFire()
screen:blit(Enemy.x,Enemy.y,Enemy.img)
collisioncheck()
collisioncheck2()
collisioncheck3()
screen:fillRect(110,1,Enemy.health,healthbar.y,green)
screen:print(1,1,"Enemy health:",green)

screen:print(250,1,"Press START to Exit",white)
screen:print(250,10,"Press SELECT to Pause",white)
screen:print(1,15,"-Boss Stage 1-",white)

if Enemy.y > 3 then
enemybulletfunc()
end

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


function collisioncheck3()
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
Music.stop()
Music.playFile("music/elise.mod",true)
while true do
screen:clear()
screen:blit(0,0,gameover)

pad = Controls.read()

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

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

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






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


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





radius = 100
speed = 3

xcenter = 100

ycenter = 135

degree = 0
radian = 0




Music.playFile("music/tz-boss.mod")

move = true
--Main Loop
while true do
pad = Controls.read()
if pad:select() then
    oldpad = pad
    screen:clear()
    
    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 + 2
end
end

for repeat_slides = 1, 2 do
if slide[repeat_slides].x > 480 then
slide[repeat_slides].x = -480
end
end
    screen:print(150,10,"Press Cross to Continue",Color.new(255,255,255))
    screen:print(180,1,"-Paused-",Color.new(255,255,255))
    screen.flip()
    while true do
       pad = Controls.read()
   if pad:cross() and pad:cross() ~= oldpad:cross() then 
           break
       end
       screen.waitVblankStart()
       oldpad = pad
    end
    oldpad = pad
    pad = Controls.read()
end




pad = Controls.read()
screen:clear()

degree = degree + speed
      radian = (degree/180)*math.pi
      Enemy.x = xcenter+math.cos(radian)*radius
      Enemy.y = ycenter-math.sin(radian)*radius




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

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()
screen:blit(Enemy.x,Enemy.y,Enemy.img)
collisioncheck()
collisioncheck2()
collisioncheck3()
screen:fillRect(110,1,Enemy.health,healthbar.y,green)
screen:print(1,1,"Enemy health:",green)

screen:print(250,1,"Press START to Exit",white)
screen:print(250,10,"Press SELECT to Pause",white)

screen:print(1,15,"-Boss Stage 1-",white)


if Enemy.y > 3 then
enemybulletfunc()
end


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