player = Image.load("Astroid_Escape/Images/Ship.png")
shieldPlayer = Image.load("Astroid_Escape/Images/ShipShield.png")
rightPlayer = Image.load("Astroid_Escape/Images/ShipRight.png")
leftPlayer = Image.load("Astroid_Escape/Images/ShipLeft.png")
upPlayer = Image.load("Astroid_Escape/Images/ShipUp.png")
astroid = Image.load("Astroid_Escape/Images/astroid.png")
astroidGone = Image.load("Astroid_Escape/Images/astroidgone.png")
background = Image.load("Astroid_Escape/Images/astroidbelt.png")
pauseback = Image.load("Astroid_Escape/Images/menu.png")
barFull = Image.createEmpty(50,10)
bar1 = Image.createEmpty(45,10)
bar2 = Image.createEmpty(40,10)
bar3 = Image.createEmpty(35,10)
bar4 = Image.createEmpty(30,10)
bar5 = Image.createEmpty(25,10)
bar6 = Image.createEmpty(20,10)
bar7 = Image.createEmpty(15,10)
bar8 = Image.createEmpty(10,10)
bar9 = Image.createEmpty(5,10)
bar10 = Image.createEmpty(1,10)

barFull:clear(red)
bar1:clear(red)
bar2:clear(red)
bar3:clear(red)
bar4:clear(red)
bar5:clear(red)
bar6:clear(red)
bar7:clear(red)
bar8:clear(red)
bar9:clear(red)
bar10:clear(red)

oldpad = Controls.read()

gamestate = "Start"

direction = "up" 

font_color = 0
current = 1

Player = {}
Player = {x = 240, y = 240, health = 50, shield = 200, image = player}

Astroid = {}
Astroid[1] = {x = 240, y = 0, health = 10, speed = 3, current = astroid}
Astroid[2] = {x = 160, y = -20, health = 10, speed = 5, current = astroid}
Astroid[3] = {x = 360, y = -25, health = 10, speed = 4, current = astroid}
Astroid[4] = {x = 50, y = -40, health = 10, speed = 5, current = astroid}
Astroid[5] = {x = 420, y = -50, health = 10, speed = 3, current = astroid}
Astroid[6] = {x = 270, y = -60, health = 10, speed = 2, current = astroid}
Astroid[7] = {x = 180, y = -80, health = 10, speed = 5, current = astroid}
Astroid[8] = {x = 90, y = -85, health = 10, speed = 2, current = astroid}
Astroid[9] = {x = 390, y = -95, health = 10, speed = 4, current = astroid}
Astroid[10] = {x = 200, y = -100, health = 10, speed = 3, current = astroid}
Astroid[11] = {x = 130, y = -110, health = 10, speed = 5, current = astroid}
Astroid[12] = {x = 460, y = -120, health = 10, speed = 2, current = astroid}
Astroid[13] = {x = 110, y = -140, health = 10, speed = 4, current = astroid}
Astroid[14] = {x = 240, y = -150, health = 10, speed = 3, current = astroid}
Astroid[15] = {x = 350, y = -165, health = 10, speed = 5, current = astroid}
Astroid[16] = {x = 120, y = -170, health = 10, speed = 3, current = astroid}
Astroid[17] = {x = 80, y = -185, health = 10, speed = 4, current = astroid}
Astroid[18] = {x = 260, y = -199, health = 10, speed = 3, current = astroid}
Astroid[19] = {x = 300, y = -220, health = 10, speed = 5, current = astroid}
Astroid[20] = {x = 100, y = -225, health = 10, speed = 2, current = astroid}
Astroid[21] = {x = 180, y = -250, health = 10, speed = 3, current = astroid}
Astroid[22] = {x = 20, y = -255, health = 10, speed = 4, current = astroid}
Astroid[23] = {x = 340, y = -270, health = 10, speed = 5, current = astroid}
Astroid[24] = {x = 200, y = -280, health = 10, speed = 3, current = astroid}
Astroid[25] = {x = 400, y = -300, health = 10, speed = 4, current = astroid}
Astroid[26] = {x = 70, y = -310, health = 10, speed = 4, current = astroid}
Astroid[27] = {x = 450, y = -325, health = 10, speed = 4, current = astroid}
Astroid[28] = {x = 240, y = -345, health = 10, speed = 4, current = astroid}
Astroid[29] = {x = 320, y = -350, health = 10, speed = 4, current = astroid}
Astroid[30] = {x = 30, y = -360, health = 10, speed = 4, current = astroid}
Astroid[31] = {x = 460, y = -370, health = 10, speed = 4, current = astroid}
Astroid[32] = {x = 270, y = -385, health = 10, speed = 4, current = astroid}
Astroid[33] = {x = 130, y = -390, health = 10, speed = 4, current = astroid}
Astroid[34] = {x = 380, y = -405, health = 10, speed = 4, current = astroid}
Astroid[35] = {x = 220, y = -415, health = 10, speed = 4, current = astroid}
Astroid[36] = {x = 400, y = -420, health = 10, speed = 4, current = astroid}
Astroid[37] = {x = 200, y = -430, health = 10, speed = 4, current = astroid}
Astroid[38] = {x = 50, y = -445, health = 10, speed = 4, current = astroid}
Astroid[39] = {x = 160, y = -450, health = 10, speed = 4, current = astroid}
Astroid[40] = {x = 450, y = -455, health = 10, speed = 4, current = astroid}
Astroid[41] = {x = 120, y = -470, health = 10, speed = 4, current = astroid}
Astroid[42] = {x = 320, y = -480, health = 10, speed = 4, current = astroid}
Astroid[43] = {x = 250, y = -485, health = 10, speed = 4, current = astroid}
Astroid[44] = {x = 470, y = -495, health = 10, speed = 4, current = astroid}
Astroid[45] = {x = 10, y = -500, health = 10, speed = 4, current = astroid}
Astroid[46] = {x = 200, y = -510, health = 10, speed = 4, current = astroid}
Astroid[47] = {x = 150, y = -525, health = 10, speed = 4, current = astroid}
Astroid[48] = {x = 350, y = -530, health = 10, speed = 4, current = astroid}
Astroid[49] = {x = 290, y = -535, health = 10, speed = 4, current = astroid}
Astroid[50] = {x = 420, y = -550, health = 10, speed = 4, current = astroid}
Astroid[51] = {x = 100, y = -560, health = 10, speed = 4, current = astroid}
Astroid[52] = {x = 250, y = -565, health = 10, speed = 4, current = astroid}
Astroid[53] = {x = 20, y = -575, health = 10, speed = 4, current = astroid}
Astroid[54] = {x = 390, y = -580, health = 10, speed = 4, current = astroid}
Astroid[55] = {x = 300, y = -595, health = 10, speed = 4, current = astroid}
Astroid[56] = {x = 200, y = -605, health = 10, speed = 4, current = astroid}
Astroid[57] = {x = 140, y = -610, health = 10, speed = 4, current = astroid}
Astroid[58] = {x = 60, y = -620, health = 10, speed = 4, current = astroid}
Astroid[59] = {x = 450, y = -635, health = 10, speed = 4, current = astroid}
Astroid[60] = {x = 250, y = -640, health = 10, speed = 4, current = astroid}

function start()
screen:blit(0,0,background)
printCentered(120,"Through the Astroid Belt:",white)
printCentered(140,"Level 2",white)
printCentered(160,"Press X to Begin",white)
if pad:cross() and oldpad:cross() ~= pad:cross() then
gamestate = "Playing"
end
end


function controls()
pad = Controls.read()
if pad:analogX() > 50 and Player.x <= 480 - player:width() then
Player.x = Player.x + 5
Player.image = rightPlayer
end

if pad:analogX() > 50 and Player.x <= 480 - player:width() and pad:square() and Player.shield > 0 then
Player.image = rightShieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogX() > 0 and pad:analogX() <= 50 then
Player.image = player
end

if pad:analogX() > 0 and pad:analogX() <= 50 and pad:square() and Player.shield > 0 then
Player.image = shieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogX() < -50 and Player.x >= 0 then
Player.x = Player.x - 5
Player.image = leftPlayer
end

if pad:analogX() < -50 and Player.x >= 0 and pad:square() and Player.shield > 0 then
Player.image = leftShieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogX() >= -50 and pad:analogX() <= 0 then
Player.image = player
end

if pad:analogX() >= -50 and pad:analogX() <= 0 and pad:square() and Player.shield > 0 then
Player.image = shieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogY() > 50 and Player.y <= 272 - player:height() then
Player.y = Player.y + 5
Player.image = player
end

if pad:analogY() > 50 and Player.y <= 272 - player:height() and pad:square() and Player.shield > 0 then
Player.image = shieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogY() <= 50 and pad:analogY() >= 0 then
Player.image = player
end

if pad:analogY() <= 50 and pad:analogY() >= 0 and pad:square() and Player.shield > 0 then
Player.image = shieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogY() < -50 and Player.y > 0 then
Player.y = Player.y - 5
Player.image = upPlayer
end

if pad:analogY() < -50 and Player.y > 0 and pad:square() and Player.shield > 0 then
Player.image = upShieldPlayer
Player.shield = Player.shield - 1
end

if pad:analogY() < -50 and Player.y <= 0 then
Player.image = player
end

if pad:analogY() < -50 and Player.y <= 0 and pad:square() and Player.shield > 0 then
Player.image = shieldPlayer
end

if pad:analogY() >= -50 and pad:analogY() <= 0 then
Player.image = player
end

if pad:analogY() >= -50 and pad:analogY() <= 0 and pad:square() and Player.shield > 0 then
Player.image = shieldPlayer
Player.shield = Player.shield - 1
end
end

function blitObjects()
screen:blit(0,0,background)
for a = 1,60 do
screen:blit(Astroid[a].x, Astroid[a].y, Astroid[a].current)
end
screen:print(5,5,"Health:",red)
screen:print(5,25,"Shield:",red)
healthAndShieldBar()

if Player.image == player then
screen:blit(Player.x, Player.y, player)
end

if Player.image == shieldPlayer then
screen:blit(Player.x, Player.y, player)
screen:blit(Player.x - 10, Player.y - 10, shieldPlayer)
end

if Player.image == upPlayer then
screen:blit(Player.x, Player.y, upPlayer)
end

if Player.image == upShieldPlayer then
screen:blit(Player.x, Player.y, upPlayer)
screen:blit(Player.x - 10, Player.y - 10, shieldPlayer)
end
end

function healthAndShieldBar()
if Player.health == 50 then
screen:blit(60,4,barFull)
end

if Player.health >= 45 and Player.health <= 49 then
screen:blit(60,4,bar1)
end

if Player.health >= 40 and Player.health <= 44 then
screen:blit(60,4,bar2)
end

if Player.health >= 35 and Player.health <= 39 then
screen:blit(60,4,bar3)
end

if Player.health >= 30 and Player.health <= 34 then
screen:blit(60,4,bar4)
end

if Player.health >= 25 and Player.health <= 29 then
screen:blit(60,4,bar5)
end

if Player.health >= 20 and Player.health <= 24 then
screen:blit(60,4,bar6)
end

if Player.health >= 15 and Player.health <= 19 then
screen:blit(60,4,bar7)
end

if Player.health >= 10 and Player.health <= 14 then
screen:blit(60,4,bar8)
end

if Player.health >= 5 and Player.health <= 9 then
screen:blit(60,4,bar9)
end

if Player.health >= 1 and Player.health <= 4 then
screen:blit(60,4,bar10)
end

if Player.shield == 200 then
screen:blit(60,24,barFull)
end

if Player.shield >= 180 and Player.shield <= 199 then
screen:blit(60,24,bar1)
end

if Player.shield >= 160 and Player.shield <= 179 then
screen:blit(60,24,bar2)
end

if Player.shield >= 140 and Player.shield <= 159 then
screen:blit(60,24,bar3)
end

if Player.shield >= 120 and Player.shield <= 139 then
screen:blit(60,24,bar4)
end

if Player.shield >= 100 and Player.shield <= 119 then
screen:blit(60,24,bar5)
end

if Player.shield >= 80 and Player.shield <= 99 then
screen:blit(60,24,bar6)
end

if Player.shield >= 60 and Player.shield <= 79 then
screen:blit(60,24,bar7)
end

if Player.shield >= 40 and Player.shield <= 59 then
screen:blit(60,24,bar8)
end

if Player.shield >= 20 and Player.shield <= 39 then
screen:blit(60,24,bar9)
end

if Player.shield >= 1 and Player.shield <= 19 then
screen:blit(60,24,bar10)
end
end

function screenMove()
for a = 1,60 do
Astroid[a].y = Astroid[a].y + 1.5
end
end

function playerCheck(object)
if (Player.x + player:width() > object.x) and (Player.x < object.x + astroid:width()) and (Player.y + player:height() > object.y) and (Player.y < object.y + astroid:height()) and object.current == astroid and Player.image == player then
object.current = astroidGone
looseLife()
end
if (Player.x + player:width() > object.x) and (Player.x < object.x + astroid:width()) and (Player.y + player:height() > object.y) and (Player.y < object.y + astroid:height()) and object.current == astroid and Player.image == upPlayer then
object.current = astroidGone
looseLife()
end
if (Player.x - 10 + shieldPlayer:width() > object.x) and (Player.x - 10 < object.x + astroid:width()) and (Player.y - 10 + shieldPlayer:height() > object.y) and (Player.y - 10 < object.y + astroid:height()) and object.current == astroid and Player.image == shieldPlayer then
object.current = astroidGone
end
if (Player.x - 10 + shieldPlayer:width() > object.x) and (Player.x - 10 < object.x + astroid:width()) and (Player.y - 10 + shieldPlayer:height() > object.y) and (Player.y - 10 < object.y + astroid:height()) and object.current == astroid and Player.image == upShieldPlayer then
object.current = astroidGone
end
end

function looseLife()
Player.health = Player.health - 10
if Player.health <= 0 then
gamestate = "Die"
end
end

function paused()
screen:blit(0,0,pauseback)
font_color = font_color + 1
printCentered(135, "Resume", red)
printCentered(150, "Quit", red)

if current == 1 and font_color <= 4 then
printCentered(135, "Resume", blue)
end

if current == 1 and font_color > 4 then
printCentered(135, "Resume", white)
end

if current == 2 and font_color <= 4 then
printCentered(150, "Quit", blue)
end

if current == 2 and font_color > 4 then
printCentered(150, "Quit", white)
end

if font_color > 8 then
font_color = 0
end
end

function dead()
screen:clear()
font_color = font_color + 1
screen:blit(0,0,pauseback)
printCentered(150,"You Lose!!!",red)
printCentered(170,"Try Again",red)
printCentered(190,"Quit",red)

if font_color <= 4 and current == 1 then
printCentered(170,"Try Again", blue)
	if pad:cross() then
	dofile("Astroid_Escape/1_2.lua")
	end
end

if font_color > 4 and current == 1 then
printCentered(170,"Try Again", white)
	if pad:cross() then
	dofile("Astroid_Escape/1_2.lua")
	end
end

if font_color <= 4 and current == 2 then
printCentered(190,"Quit", blue)
	if pad:cross() then
	dofile("Astroid_Escape/mainMenu.lua")
	end
end

if font_color > 4 and current == 2 then
printCentered(190,"Quit", white)
	if pad:cross() then
	dofile("Astroid_Escape/mainMenu.lua")
	end
end

if font_color > 8 then
font_color = 0
end

if current == 0 then
current = 2
end

if current == 3 then
current = 1
end
end

function finish()
if Astroid[60].y >= 272 then
gamestate = "Win"
end
end

function win()
font_color = font_color + 1
screen:clear()
screen:blit(0,0,pauseback)
printCentered(150,"You Beat the Level!!!",red)
printCentered(170,"Continue",red)
printCentered(190,"Quit",red)

if font_color <= 4 and current == 1 then
printCentered(170,"Continue", blue)
	if pad:cross() and oldpad:cross() ~= pad:cross() then
	dofile("Astroid_Escape/1_3.lua")
	end
end

if font_color > 4 and current == 1 then
printCentered(170,"Continue", white)
	if pad:cross() and oldpad:cross() ~= pad:cross() then
	dofile("Astroid_Escape/1_3.lua")
	end
end

if font_color <= 4 and current == 2 then
printCentered(190,"Quit", blue)
	if pad:cross() then
	dofile("Astroid_Escape/mainMenu.lua")
	end
end

if font_color > 4 and current == 2 then
printCentered(190,"Quit", white)
	if pad:cross() then
	dofile("Astroid_Escape/mainMenu.lua")
	end
end

if font_color > 8 then
font_color = 0
end

if current == 0 then
current = 2
end

if current == 3 then
current = 1
end
end

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

if gamestate == "Start" then
start()
end

if gamestate == "Playing" then
screen:clear()
controls()
blitObjects()
screenMove()
finish()
for a = 1,60 do
playerCheck(Astroid[a])
end
	if pad:start() then
	gamestate = "Paused"
	end
end

if gamestate == "Die" then
dead()
	if pad:up() and oldpad:up() ~= pad:up() then
	current = current - 1
	end

	if pad:down() and oldpad:down() ~= pad:down() then
	current = current + 1
	end
end

if gamestate == "Win" then
win()
	if pad:up() and oldpad:up() ~= pad:up() then
	current = current - 1
	end

	if pad:down() and oldpad:down() ~= pad:down() then
	current = current + 1
	end
end

if gamestate == "Paused" then
screen:clear()
paused()
	if pad:down() and oldpad:down() ~= pad:down() then
	current = current + 1
	end

	if pad:up() and oldpad:up() ~= pad:up() then
	current = current - 1
	end

	if current == 0 then
	current = 2
	end

	if pad:cross() and current == 1 then
	gamestate = "Playing"
	end

	if pad:cross() and current == 2 then
	dofile("Astroid_Escape/mainMenu.lua")
	end

	if current == 3 then
	current = 1
	end
end

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