System.usbDiskModeActivate()

back = Image.load("img1/background.png")
player = Image.load("img1/p.png")
Sora = Image.load("img1/sora.png")
n1 = Image.load("Gewonnen.png")
h = Image.load("h/h.PNG")
m = Image.load("img1/Sora1.png")
t = 0
l = 10
riku = Image.load("img1/riku.png")

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

pause = 0
Wahl = 1
red = Color.new(255,0,0)
light_blue = Color.new(0,180,255)
weiss = Color.new(255,255,255)

pasosd=0
pasosi=0

p  = Image.load("a.png")
p2 = Image.load("a2.png")
player = {
x = 236;
y = 180;
score = 0;
img = player;
height = 67;
width = 54;}

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;
}

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

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 + 10
	t = t + 1
	Sora.x = math.random(0,480)
	Sora.y = -50	
	Sora2.x = math.random(0,480)
	Sora2.y = -50
	Sora3.x = math.random(0,480)
	Sora3.y = -50	
	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

oldpad = Controls.read()
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("lvl2.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 = Sora.y + 2
Sora3.y = Sora.y + 2
end

if Sora.y >= 290
 then Sora.y = -50 end
if Sora2.y >= 290
 then Sora2.y = -50 end
if Sora3.y >= 290
 then Sora3.y = -50 end
 
collideEngine(Sora)
collideEngine(Sora2)
collideEngine(Sora3)



if player.score >= 100 then 
back = nil
player = nil
Sora = nil
h = nil
m = nil
riku = nil
s1f = nil
s2f = nil
s3f = nil
s4f = nil
s1d = nil
s2d = nil
s3d = nil
s4d = nil
p = nil
p2 = nil
screen:blit(0,0,n1)
screen.flip()
screen.waitVblankStart(180)
dofile("lvl2.lua") 
end


screen:blit(Sora.x,Sora.y, Sora.img,true)
screen:blit(Sora2.x,Sora2.y, Sora.img,true)
screen:blit(Sora3.x,Sora3.y, Sora.img,true)
screen:print(10,10,"Punkte :" ..player.score,Color.new(0,0,0))
screen:blit(380,10,h,true)
screen:blit(0,240,m,true)
screen:print(39,250,t.. "/" ..l,Color.new(0,0,0))
screen:blit(443,20,riku,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