System.usbDiskModeActivate()

back = Image.load("img1/07.png")
player = Image.load("img1/b.PNG")
Sora = Image.load("img1/sora.png")
Soldat = Image.load("img1/s.png")
Soldat2 = Image.load("img1/so.png")
n1 = Image.load("Gewonnen.png")
h = Image.load("h/h.PNG")
h2 = Image.load("h/h2.PNG")
h3 = Image.load("h/h3.PNG")
h4 = Image.load("h/h4.PNG")
h5 = Image.load("h/h5.PNG")
h6 = Image.load("h/h6.PNG")
h7 = Image.load("h/h7.PNG")
h8 = Image.load("h/h8.PNG")
h9 = Image.load("h/h9.PNG")
h10 = Image.load("h/h10.PNG")
cloud = Image.load("img1/cloud.PNG")

s1f = Image.load("w2/w.PNG")
s2f = Image.load("w2/w2.PNG")
s3f = Image.load("w2/w3.PNG")
s4f = Image.load("w2/w4.PNG")
s1d = Image.load("w2/w5.PNG")
s2d = Image.load("w2/w6.PNG")
s3d = Image.load("w2/w7.PNG")
s4d = Image.load("w2/w8.PNG")

m = Image.load("img1/Sora1.png")
t = 0
l = 20

p  = Image.load("a.png")
p2 = Image.load("a2.png")

pause = 0
Wahl = 1
green = Color.new(0,255,0)  --Grün
orange = Color.new(235,115,0)  --Orange
red = Color.new(255,0,0)
light_blue = Color.new(0,180,255)
weiss = Color.new(255,255,255)

pasosd=0
pasosi=0

player = {
x = 236;
y = 180;
score = 0;
img = player;
height = 67;
width = 54;
health = 100;}

Sora = {
x = math.random (0,480);
y = -20;
height = 50;
width = 25;
img = Sora;
}

Sora2 = {
x = math.random (0,480);
y = -20;
height = 50;
width = 25;
img = Sora;
}

Soldat = {
x = math.random (0,480);
y = -20;
height = 57;
width = 56;
img = Soldat;
}

Soldat2 = {
x = math.random (0,480);
y = -20;
height = 63;
width = 50;
img = Soldat2;
}

function collideEngine(flame)
if (player.x + player.width >flame.x) and (player.x < flame.x + flame.width) and (player.y + player.height > flame.y) and (player.y < flame.y + flame.height) and pause == 0 then 	
	player.score = player.score +5
	t = t + 1
	Sora.x = math.random(0,480)
	Sora.y = -50	
	Sora2.x = math.random(0,480)
	Sora2.y = -50		
	end

end

function collideBomb(flame)
if (player.x + player.width >flame.x) and (player.x < flame.x + flame.width) and (player.y + player.height > flame.y) and (player.y < flame.y + flame.height) and pause == 0 then 	
	player.health = player.health - 5
	Soldat.x = math.random(0,480)
	Soldat.y = -30	
			
	end

end


function collideRock(flame)
if (player.x + player.width >flame.x) and (player.x < flame.x + flame.width) and (player.y + player.height > flame.y) and (player.y < flame.y + flame.height) and pause == 0 then 	
	player.health = player.health - 7
	Soldat2.x = math.random(0,480)
	Soldat2.y = -30			
	end

end

function sprd()
if pasosd >= 0 and pasosd < 10 and pad:right() then
player.img = s1d
end
if pasosd >= 10 and pasosd < 20 and pad:right() then
player.img = s2d
end
if pasosd >= 20 and pasosd < 30 and pad:right() then
player.img = s3d
end
if pasosd >= 30 and pasosd < 40 and pad:right() then
player.img = s4d
end
if pasosd >= 40 then
pasosd=0
end
end

function spri()
if pasosi >= 0 and pasosi < 10 and pad:left() then
player.img = s1f
end
if pasosi >= 10 and pasosi < 20 and pad:left() then
player.img = s2f
end
if pasosi >= 20 and pasosi < 30 and pad:left() then
player.img = s3f
end
if pasosi >= 30 and pasosi < 40 and pad:left() then
player.img = s4f
end
if pasosi >= 40 then
pasosi=0
end
end

System.message("Level 3!",0)
while true do
Ogg.load("k.ogg",false)
Ogg.play()
screen:clear()
screen:blit(0,0,back,true)
screen:blit(player.x,player.y,player.img,true)


pad = Controls.read()
if pad:l() and pad:r() and pad:triangle() then
dofile("lvl4.lua")
end

if pad:left() and pause == 0 then
player.x = player.x - 3
pasosi = pasosi + 2
end

if pad:right() and pause == 0 then 
player.x = player.x + 3
pasosd = pasosd + 2
end

sprd()
spri()


if player.x <= 0 or player.x >=480-54 then player.x = oldx end
oldx = player.x

if pause == 0 then
Sora.y = Sora.y + 2
Sora2.y = Sora2.y + 2
Soldat.y = Soldat.y + 3
Soldat2.y = Soldat2.y + 3.5
end

if Sora.y >= 290 then
Sora.x = math.random(0,480) 
Sora.y = -50 end
if Sora2.y >= 290 then 
Sora2.x = math.random(0,480)
Sora2.y = -50 end
if Soldat.y >= 285 then 
Soldat.x = math.random(0,480)
Soldat.y = - 30 end
if Soldat2.y >= 285 then 
Soldat2.x = math.random(0,480)
Soldat2.y = - 30 end


collideEngine(Sora)
collideEngine(Sora2)
collideBomb(Soldat)
collideRock(Soldat2)



if player.score >= 100 then 
back = nil
player = nil
Sora = nil
Soldat = nil
Soldat2 = nil
h = nil
h2 = nil
h3 = nil
h4 = nil
h5 = nil
h6 = nil
h7 = nil
h8 = nil
h9 = nil
h10 = nil
cloud = nil
s1f = nil
s2f = nil
p = nil
p2 = nil
s3f = nil
s4f = nil
s1d = nil
s2d = nil
s3d = nil
s4d = nil
m = nil
screen:blit(0,0,n1)
screen.flip()
screen.waitVblankStart(180)
dofile("lvl4.lua") 
end
if player.health <= 0 then 
dofile("Verloren.lua")
end

if player.health <= 100 and player.health > 90 then
screen:blit(380,10,h,true)
end
if player.health <= 90 and player.health > 80 then
screen:blit(380,10,h2,true)
end
if player.health <= 80 and player.health > 70 then
screen:blit(380,10,h3,true)
end
if player.health <= 70 and player.health > 60 then
screen:blit(380,10,h4,true)
end
if player.health <= 60 and player.health > 50 then
screen:blit(380,10,h5,true)
end
if player.health <= 50 and player.health > 40 then
screen:blit(380,10,h6,true)
end
if player.health <= 40 and player.health > 30 then
screen:blit(380,10,h7,true)
end
if player.health <= 30 and player.health > 20 then
screen:blit(380,10,h8,true)
end
if player.health <= 20 and player.health > 10 then
screen:blit(380,10,h9,true)
end
if player.health <= 10 and player.health > 0 then
screen:blit(380,10,h10,true)
end

screen:blit(Sora.x,Sora.y, Sora.img,true)
screen:blit(Sora2.x,Sora2.y, Sora.img,true)
screen:blit(Soldat.x,Soldat.y, Soldat.img,true)
screen:blit(Soldat2.x,Soldat2.y, Soldat2.img,true)
screen:print(10,10,"Punkte :" ..player.score,Color.new(0,0,0))
screen:blit(0,240,m,true)
screen:print(39,250,t.. "/" ..l,Color.new(0,0,0))
screen:blit(445,24,cloud,true)




if pad:start() and oldpad:start() ~= pad:start() and pause == 0 then
pause = 1
end

if pause == 1 then
if Wahl == 1 then
screen:blit(0,0,p2,true)
end
if Wahl == 2 and pause == 1 then
screen:blit(0,0,p,true)
end
if pad:up() and oldpad:up() ~= pad:up() and Wahl == 1 then
screen.waitVblankStart(20)
Wahl = 2
elseif pad:up() and oldpad:up() ~= pad:up() and Wahl == 2 then
screen.waitVblankStart(20)
Wahl = 1
end

if pad:down() and oldpad:down() ~= pad:down() and Wahl == 1 then
screen.waitVblankStart(20)
Wahl = 2
elseif pad:down() and oldpad:down() ~= pad:down() and Wahl == 2 then
screen.waitVblankStart(20)
Wahl = 1
end

if pad:circle() and oldpad:circle() ~= pad:circle() or pad:cross() and oldpad:cross() ~= pad:cross() and Wahl == 1 then
pause = 0
end
if pad:cross() and oldpad:cross() ~= pad:cross() and Wahl == 2 then
dofile("script2.lua")
end
end


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