groen = Color.new(0,255,0)
green = Color.new(205,38,38)
white = Color.new(255,255,255)
red = Color.new(255,0,0)
yellow = Color.new(255,255,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")
Enemybullet2 = Image.load("images/ztype/enemybullet2.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 }
Enemy2 = { x = 255, y = 100, img = Image.load("images/ztype/boss2.png"),health = 100 }

enemybullet = { x = Enemy.x, y = Enemy.y, img = enemybulletpic }
enemybullet2 = { x = Enemy2.x, y = 300, img = Enemybullet2 }

slide = {}

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



healthbar = {}
healthbar.y = 10


healthbar2 = {}
healthbar2.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
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 }
game()
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
gameover()
end
end

function collisioncheck4()
for i = 1,5 do
if (enemybullet2.x + 10 > BulletInfo[i].x) and (enemybullet2.x < BulletInfo[i].x + 10) and (enemybullet2.y + 10 > BulletInfo[i].y) and (enemybullet2.y < BulletInfo[i].y + 10) then
enemybullet2 = { x = Enemy2.x, y = Enemy2.y, img = Enemybullet2 }
game()
end
end
end


function collisioncheck5()
if (enemybullet2.x + 65 > Player.x) and (enemybullet2.x < Player.x + 20) and (enemybullet2.y + 24 > Player.y) and (enemybullet2.y < Player.y + 5) then
Player.health = Player.health - 10
end
end

function collisioncheck6()
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
Enemy2.health = Enemy2.health - 0.7
end
end
end


function stageclear()
screen:clear()
while true do
screen:clear()
screen:print(130,10,"YOU ARE A SUPER PLAYER!",white)
screen:print(100,20,"Press Start to Restart",white)


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


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



function game()
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

      degree = degree + speed
      radian = (degree/180)*math.pi
      Enemy2.y = ycenter-math.sin(radian)*100


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()
collisioncheck4()
collisioncheck5()
collisioncheck6()
screen:fillRect(110,1,Enemy.health,healthbar.y,green)
screen:print(1,1,"Boss1 health:",green)
screen:print(1,12,"Boss2 health:",yellow)
screen:print(250,1,"Press SELECT to Pause",white)
screen:fillRect(110,12,Enemy2.health,healthbar.y,yellow)

screen:print(250,10,"Player health:",groen)
screen:fillRect(360,10,Player.health,healthbar2.y,groen)

screen:blit(Enemy2.x,Enemy2.y,Enemy2.img)
if Enemy.y > 3 then
enemybulletfunc()
end
screen:blit(enemybullet2.x,enemybullet2.y,enemybullet2.img)

if Enemy2.y > 40 then
enemybullet2.y = Enemy2.y
enemybullet2func()
end

if enemybullet2.x > 480 then
enemybullet2.x = Enemy2.x
end

if Enemy.health < 0 then
stageclear()
end

if Enemy2.health < 0 then
stageclear()
end

if Player.health < 0 then
gameover()
end


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


function gameover()
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
dofile("ztype-menu.lua")
end

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

screen.waitVblankStart()
screen.flip()
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

function enemybullet2func()
enemybullet2.x = enemybullet2.x + 5
end




radius = 100
speed = 3

xcenter = 100

ycenter = 135

degree = 0
radian = 0




Music.playFile("music/rtypemix.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
      
      degree = degree + speed
      radian = (degree/180)*math.pi
      Enemy2.y = ycenter-math.sin(radian)*100




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)
screen:blit(Enemy2.x,Enemy2.y,Enemy2.img)
collisioncheck()
collisioncheck2()
collisioncheck3()
collisioncheck4()
collisioncheck5()
collisioncheck6()
screen:fillRect(110,1,Enemy.health,healthbar.y,green)
screen:print(1,1,"Boss1 health:",green)
screen:print(1,12,"Boss2 health:",yellow)
screen:print(250,1,"Press SELECT to Pause",white)
screen:fillRect(110,12,Enemy2.health,healthbar.y,yellow)

screen:print(250,10,"Player health:",groen)
screen:fillRect(360,10,Player.health,healthbar2.y,groen)



if Enemy.y > 3 then
enemybulletfunc()
end


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

if Enemy2.y > 40 then
enemybullet2.y = Enemy2.y
enemybullet2func()
end

if enemybullet2.x > 480 then
enemybullet2.x = Enemy2.x
end

if Enemy.health < 0 then
stageclear()
end

if Enemy2.health < 0 then
stageclear()
end

if Player.health < 0 then
gameover()
end

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