--Level 4--

screen:blit(0,0,bg4)

screen:print(100,20,"Level 4")

if gem14 == "on" then
   screen:blit(gem1_1.x, gem1_1.y, gem1_1.img)
end

if gem15 == "on" then
   screen:blit(gem1_4.x, gem1_4.y, gem1_4.img)
end

if gem16 == "on" then
   screen:blit(gem2_2.x, gem2_2.y, gem2_2.img)
end

if gem17 == "on" then
   screen:blit(gem2_4.x, gem2_4.y, gem2_4.img)
end

if gem18 == "on" then
   screen:blit(gem3_1.x, gem3_1.y, gem3_1.img)
end

if gem19 == "on" then
   screen:blit(gem3_2.x, gem3_2.y, gem3_2.img)
end

if gem20 == "on" then
   screen:blit(gem3_3.x, gem3_3.y, gem3_3.img)
end

if gem21 == "on" then
   screen:blit(gem3_4.x, gem3_4.y, gem3_4.img)
end

if gem22 == "on" then
   screen:blit(gem2_3.x, gem2_3.y, gem2_3.img)
end

if gem23 == "on" then
   screen:blit(gem1_2.x, gem1_2.y, gem1_2.img)
end

if enemy4_1 == "on" then
   screen:blit(enemy1.x, enemy1.y, enemy1.img)
end

if enemy5_1 == "on" then
   screen:blit(enemy2.x, enemy2.y, enemy2.img)
end

if enemy6_1 == "on" then
   screen:blit(enemy3.x, enemy3.y, enemy3.img)
end

--

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

function collisionCheck4()
--Check for collision with gems--

if (player.x + 32 > gem1_1.x) and (player.x < gem1_1.x + 32) and (player.y + 32 > gem1_1.y) and (player.y < gem1_1.y + 32) then
   gem14 = "off"
elseif (player.x + 32 > gem1_4.x) and (player.x < gem1_4.x + 32) and (player.y + 32 > gem1_4.y) and (player.y < gem1_4.y + 32) then
   gem15 = "off"
elseif (player.x + 32 > gem2_2.x) and (player.x < gem2_2.x + 32) and (player.y + 32 > gem2_2.y) and (player.y < gem2_2.y + 32) then
   gem16 = "off"
elseif (player.x + 32 > gem2_4.x) and (player.x < gem2_4.x + 32) and (player.y + 32 > gem2_4.y) and (player.y < gem2_4.y + 32) then
   gem17 = "off"
elseif (player.x + 32 > gem3_1.x) and (player.x < gem3_1.x + 32) and (player.y + 32 > gem3_1.y) and (player.y < gem3_1.y + 32) then
   gem18 = "off"
elseif (player.x + 32 > gem3_2.x) and (player.x < gem3_2.x + 32) and (player.y + 32 > gem3_2.y) and (player.y < gem3_2.y + 32) then
   gem19 = "off"
elseif (player.x + 32 > gem3_3.x) and (player.x < gem3_3.x + 32) and (player.y + 32 > gem3_3.y) and (player.y < gem3_3.y + 32) then
   gem20 = "off"
elseif (player.x + 32 > gem3_4.x) and (player.x < gem3_4.x + 32) and (player.y + 32 > gem3_4.y) and (player.y < gem3_4.y + 32) then
   gem21 = "off"
elseif (player.x + 32 > gem2_3.x) and (player.x < gem2_3.x + 32) and (player.y + 32 > gem2_3.y) and (player.y < gem2_3.y + 32) then
   gem22 = "off"
elseif (player.x + 32 > gem1_2.x) and (player.x < gem1_2.x + 32) and (player.y + 32 > gem1_2.y) and (player.y < gem1_2.y + 32) then
   gem23 = "off"
end

--Check for collision with enemy4--

if (player.x + 32 > enemy1.x) and (player.x < enemy1.x + 32) and (player.y + 32 > enemy1.y) and (player.y < enemy1.y + 32) then
   enemy4_1 = "off"
end

--Check for collision with enemy5--

if (player.x + 32 > enemy2.x) and (player.x < enemy2.x + 32) and (player.y + 32 > enemy2.y) and (player.y < enemy2.y + 32) then
   enemy5_1 = "off"
end

--Check for collision with enemy6--

if (player.x + 32 > enemy3.x) and (player.x < enemy3.x + 32) and (player.y + 32 > enemy3.y) and (player.y < enemy3.y + 32) then
   enemy6_1 = "off"
end

end

function endCheck4()
--Check for end of level--

pad = Controls.read()

if gem14 == "off" and gem15 == "off" and gem16 == "off" and gem17 == "off" and gem18 == "off" and gem19 == "off" and gem20 == "off" and gem21 == "off" and gem22 == "off" and gem23 == "off" and over == "false" then
   level = 5
   enemy4_1 = "on"
   enemy5_1 = "on"
   enemy6_1 = "on"
   gem14 = "on"
   gem15 = "on"
   gem16 = "on"
   gem17 = "on"
   gem18 = "on"
   gem19 = "on"
   gem20 = "on"
   gem21 = "on"
   gem22 = "on"
   gem23 = "on"
end

if enemy4_1 == "off" or enemy5_1 == "off" or enemy6_1 == "off" then
   screen:blit(0,0,busted)
   screen:print(150,200,"PRESS START TO RESTART")
   over = "true"
end

if over == "true" and pad:start() then
   dofile("script.lua")
end

end

collisionCheck4()
endCheck4()