unloadbuff()
white = Color.new(255, 255, 255)
black = Color.new(0, 0, 0)
Triangle = Image.load("Games/BBR/IMG/A.png")
Circle = Image.load("Games/BBR/IMG/O.png")
Square = Image.load("Games/BBR/IMG/V.png")
Kreuz = Image.load("Games/BBR/IMG/X.png")
Up = Image.load("Games/BBR/IMG/up.png")
Down = Image.load("Games/BBR/IMG/down.png")
Left = Image.load("Games/BBR/IMG/left.png")
Right = Image.load("Games/BBR/IMG/right.png")
menusel = Image.load("Games/BBR/Pictures/rocket.png")
menupic = Image.load("Games/BBR/Pictures/menu.jpg")
Balken1 = Image.load("Games/BBR/Balken/Balken1.png")
Balken2 = Image.load("Games/BBR/Balken/Balken2.png")
Balken3 = Image.load("Games/BBR/Balken/Balken3.png")
Balken4 = Image.load("Games/BBR/Balken/Balken4.png")
Balken5 = Image.load("Games/BBR/Balken/Balken5.png")
Balken6 = Image.load("Games/BBR/Balken/Balken6.png")
Balken7 = Image.load("Games/BBR/Balken/Balken7.png")
Balken8 = Image.load("Games/BBR/Balken/Balken8.png")
n0 = Image.load("Games/BBR/ani/n000.jpg")
n1 = Image.load("Games/BBR/ani/n001.jpg")
n2 = Image.load("Games/BBR/ani/n002.jpg")
n3 = Image.load("Games/BBR/ani/n003.jpg")
n4 = Image.load("Games/BBR/ani/n004.jpg")
n5 = Image.load("Games/BBR/ani/n005.jpg")
n6 = Image.load("Games/BBR/ani/n006.jpg")
n7 = Image.load("Games/BBR/ani/n007.jpg")
n8 = Image.load("Games/BBR/ani/n008.jpg")
n9 = Image.load("Games/BBR/ani/n009.jpg")
n10 = Image.load("Games/BBR/ani/n010.jpg")
n11 = Image.load("Games/BBR/ani/n011.jpg")
n12 = Image.load("Games/BBR/ani/n012.jpg")
n13 = Image.load("Games/BBR/ani/n013.jpg")
n14 = Image.load("Games/BBR/ani/n014.jpg")
n15 = Image.load("Games/BBR/ani/n015.jpg")
anistate = 0
balkenpic = Balken1
anidirection = 1
balkenani = 0
xPad1 = 0
xPad2 = 0
xPad3 = 0
xPad4 = 0
yPad1 = 272
yPad2 = 272
yPad3 = 272
yPad4 = 272
waitPad1 = 262
waitPad2 = 200
waitPad3 = 140
waitPad4 = 80
kindPad1 = 0
kindPad2 = 0
kindPad3 = 0
kindPad4 = 0
picPad1 = Cross
picPad2 = Cross
picPad3 = Cross
picPad4 = Cross
onPad1 = 0
onPad2 = 0
onPad3 = 0
onPad4 = 0
entriegeltPad1 = 1
entriegeltPad2 = 1
entriegeltPad3 = 1
entriegeltPad4 = 1
bild = 1
bild2 = 1
right = Sound.load("Games/BBR/Sound/right.wav")
bgs = Sound.load("Games/BBR/Sound/bg.wav", true)
punkte = 0
leben = 5
speed = 2
gamestate = "menue"
Select = 1
sperre = 1
bgsplay = 1

function anibg()
bild2 = bild2 + 0.5
if bild >= 1 then screen:blit(0, 0, n0) end
if bild >= 2 and bild < 3 then screen:blit(0, 0, n1) end
if bild >= 3 and bild < 4 then screen:blit(0, 0, n2) end
if bild >= 4 and bild < 5 then screen:blit(0, 0, n3) end
if bild >= 5 and bild < 6 then screen:blit(0, 0, n4) end
if bild >= 6 and bild < 7 then screen:blit(0, 0, n5) end
if bild >= 7 and bild < 8 then screen:blit(0, 0, n6) end
if bild >= 8 and bild < 9 then screen:blit(0, 0, n7) end
if bild >= 9  and bild < 10 then screen:blit(0, 0, n8) end
if bild >= 10 and bild < 11 then screen:blit(0, 0, n9) end
if bild >= 11 and bild < 12 then screen:blit(0, 0, n10) end
if bild >= 12 and bild < 13 then screen:blit(0, 0, n11) end
if bild >= 13 and bild < 14 then screen:blit(0, 0, n12) end
if bild >= 14 and bild < 15 then screen:blit(0, 0, n13) end
if bild >= 15 and bild < 16 then screen:blit(0, 0, n14) end
if bild >= 16 and bild <= 17 then screen:blit(0, 0, n15) bild2 = 1 end
bild = bild2
end

function ani()
if balkenani == 1 then
screen:blit (0, 50, balkenpic, white)
if anidirection == 1 then anistate = anistate + 1 end
if anidirection == 2 then anistate = anistate - 1 end
if anistate == 0 then balkenpic = Balken1 anidirection = 1 balkenani = 0 end
if anistate == 1 and anidirection == 1 then right:play() punkte = punkte + 1 end
if anistate == 1 then balkenpic = Balken1 end
if anistate == 2 then balkenpic = Balken2 end
if anistate == 3 then balkenpic = Balken3 end
if anistate == 4 then balkenpic = Balken4 end
if anistate == 5 then balkenpic = Balken5 end
if anistate == 6 then balkenpic = Balken6 end
if anistate == 7 then balkenpic = Balken7 end
if anistate == 8 then anidirection = 2 balkenpic = Balken8 end
end
end

function wait4pad()
if waitPad1 >= 272 then xPad1 = math.random(1, 447) kindPad1 = math.random(0.5, 8.5) waitPad1 = 0 onPad1 = 1 yPad1 = 272 if entriegeltPad1 == 0 then leben = leben -1 end entriegeltPad1 = 0
    if  kindPad1 < 1.5 then picPad1 = Kreuz end
    if  kindPad1 < 2.5 and kindPad1 > 1.5 then picPad1 = Triangle end
    if  kindPad1 < 3.5 and kindPad1 > 2.5 then picPad1 = Square end
    if  kindPad1 < 4.5 and kindPad1 > 3.5 then picPad1 = Circle end
    if  kindPad1 < 5.5 and kindPad1 > 4.5 then picPad1 = Up end
    if  kindPad1 < 6.5 and kindPad1 > 5.5 then picPad1 = Down end
    if  kindPad1 < 7.5 and kindPad1 > 6.5 then picPad1 = Left end
    if  kindPad1 < 8.5 and kindPad1 > 7.5 then picPad1 = Right end
end
if waitPad2 >= 272 then xPad2 = math.random(0, 447) kindPad2 = math.random(0.5, 4.5) waitPad2 = 0 onPad2 = 1 yPad2 = 272 if entriegeltPad2 == 0 then leben = leben -1 end entriegeltPad2 = 0
    if  kindPad2 < 1.5 then picPad2 = Kreuz end
    if  kindPad2 < 2.5 and kindPad2 > 1.5 then picPad2 = Triangle end
    if  kindPad2 < 3.5 and kindPad2 > 2.5 then picPad2 = Square end
    if  kindPad2 < 4.5 and kindPad2 > 3.5 then picPad2 = Circle end
    if  kindPad1 < 5.5 and kindPad1 > 4.5 then picPad1 = Up end
    if  kindPad1 < 6.5 and kindPad1 > 5.5 then picPad1 = Down end
    if  kindPad1 < 7.5 and kindPad1 > 6.5 then picPad1 = Left end
    if  kindPad1 < 8.5 and kindPad1 > 7.5 then picPad1 = Right end
end
if waitPad3 >= 272 then xPad3 = math.random(0, 447) kindPad3 = math.random(0.5, 4.5) waitPad3 = 0 onPad3 = 1 yPad3 = 272 if entriegeltPad3 == 0 then leben = leben -1 end entriegeltPad3 = 0
    if  kindPad3 < 1.5 then picPad3 = Kreuz end
    if  kindPad3 < 2.5 and kindPad3 > 1.5 then picPad3 = Triangle end
    if  kindPad3 < 3.5 and kindPad3 > 2.5 then picPad3 = Square end
    if  kindPad3 < 4.5 and kindPad3 > 3.5 then picPad3 = Circle end
    if  kindPad1 < 5.5 and kindPad1 > 4.5 then picPad1 = Up end
    if  kindPad1 < 6.5 and kindPad1 > 5.5 then picPad1 = Down end
    if  kindPad1 < 7.5 and kindPad1 > 6.5 then picPad1 = Left end
    if  kindPad1 < 8.5 and kindPad1 > 7.5 then picPad1 = Right end
end
if waitPad4 >= 272 then xPad4 = math.random(0, 447) kindPad4 = math.random(0.5, 4.5) waitPad4 = 0 onPad4 = 1 yPad4 = 272 if entriegeltPad4 == 0 then leben = leben -1 end entriegeltPad4 = 0
    if  kindPad4 < 1.5 then picPad4 = Kreuz end
    if  kindPad4 < 2.5 and kindPad4 > 1.5 then picPad4 = Triangle end
    if  kindPad4 < 3.5 and kindPad4 > 2.5 then picPad4 = Square end
    if  kindPad4 < 4.5 and kindPad4 > 3.5 then picPad4 = Circle end
    if  kindPad1 < 5.5 and kindPad1 > 4.5 then picPad1 = Up end
    if  kindPad1 < 6.5 and kindPad1 > 5.5 then picPad1 = Down end
    if  kindPad1 < 7.5 and kindPad1 > 6.5 then picPad1 = Left end
    if  kindPad1 < 8.5 and kindPad1 > 7.5 then picPad1 = Right end
end
end
 
oldpad = Controls.read()

while true do

if gamestate == "menue" then 
screen:clear()
screen:blit(0, 0, menupic)
pad = Controls.read()
     if pad:down() and sperre == 0 then sperre = 1
          Select = Select + 1
     end
     if pad:up() and sperre == 0 then sperre = 1
          Select = Select - 1
     end
     if not pad:up() and not pad:down() then sperre = 0 end
	if Select > 2 then
		Select = 1
	elseif Select < 1 then
		Select = 2
	end
     if Select == 1 then
        screen:blit(180, 173, menusel)
     end
     if Select == 2 then
        screen:blit(180, 214, menusel)
     end
     if Select == 1 and pad:cross() then
        gamestate = "game"
     end
     if Select == 2 and pad:cross() then
Triangle = nil
Circle = nil
Square = nil
Kreuz = nil
Up = nil
Down = nil
Left = nil
Right = nil
menusel = nil
menupic = nil
Balken1 = nil
Balken2 = nil
Balken3 = nil
Balken4 = nil
Balken5 = nil
Balken6 = nil
Balken7 = nil
Balken8 = nil
n0 = nil
n1 = nil
n2 = nil
n3 = nil
n4 = nil
n5 = nil
n6 = nil
n7 = nil
n8 = nil
n9 = nil
n10 = nil
n11 = nil
n12 = nil
n13 = nil
n14 = nil
n15 = nil
collectgarbage()
bgs:stop()
          dofile("main.lua")
     end
end

if gamestate == "game" then
screen:clear()
pad = Controls.read()
anibg()
screen:blit (0, 50, balkenpic, white)
ani()
if bgsplay == 1 then bgs:play() bgsplay = 2 end
speed = speed + 0.001
waitPad1 = waitPad1 + speed
waitPad2 = waitPad2 + speed
waitPad3 = waitPad3 + speed
waitPad4 = waitPad4 + speed

wait4pad()

if onPad1 == 1 then screen:blit(xPad1, yPad1, picPad1) yPad1 = yPad1 - speed end
if onPad2 == 1 then screen:blit(xPad2, yPad2, picPad2) yPad2 = yPad2 - speed end
if onPad3 == 1 then screen:blit(xPad3, yPad3, picPad3) yPad3 = yPad3 - speed end
if onPad4 == 1 then screen:blit(xPad4, yPad4, picPad4) yPad4 = yPad4 - speed end




if yPad1 < 50 and yPad1 > 35 then
if pad:cross() and kindPad1 < 1.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:triangle() and kindPad1 < 2.5 and kindPad1 > 1.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:square() and kindPad1 < 3.5 and kindPad1 > 2.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:circle() and kindPad1 < 4.5 and kindPad1 > 3.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:up() and kindPad1 < 5.5 and kindPad1 > 4.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:down() and kindPad1 < 6.5 and kindPad1 > 5.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:left() and kindPad1 < 7.5 and kindPad1 > 6.5 then balkenani = 1 entriegeltPad1 = 1
elseif pad:right() and kindPad1 < 8.5 and kindPad1 > 7.5 then balkenani = 1 entriegeltPad1 = 1 end
end

if yPad2 < 50 and yPad2 > 35 then
if pad:cross() and kindPad2 < 1.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:triangle() and kindPad2 < 2.5 and kindPad2 > 1.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:square() and kindPad2 < 3.5 and kindPad2 > 2.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:circle() and kindPad2 < 4.5 and kindPad2 > 3.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:up() and kindPad1 < 5.5 and kindPad1 > 4.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:down() and kindPad1 < 6.5 and kindPad1 > 5.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:left() and kindPad1 < 7.5 and kindPad1 > 6.5 then balkenani = 1 entriegeltPad2 = 1 end
if pad:right() and kindPad1 < 8.5 and kindPad1 > 7.5 then balkenani = 1 entriegeltPad2 = 1 end
end

if yPad3 < 50 and yPad3 > 35 then
if pad:cross() and kindPad3 < 1.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:triangle() and kindPad3 < 2.5 and kindPad3 > 1.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:square() and kindPad3 < 3.5 and kindPad3 > 2.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:circle() and kindPad3 < 4.5 and kindPad3 > 3.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:up() and kindPad1 < 5.5 and kindPad1 > 4.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:down() and kindPad1 < 6.5 and kindPad1 > 5.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:left() and kindPad1 < 7.5 and kindPad1 > 6.5 then balkenani = 1 entriegeltPad3 = 1 end
if pad:right() and kindPad1 < 8.5 and kindPad1 > 7.5 then balkenani = 1 entriegeltPad3 = 1 end
end

if yPad4 < 50 and yPad4 > 35 then
if pad:cross() and kindPad4 < 1.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:triangle() and kindPad4 < 2.5 and kindPad4 > 1.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:square() and kindPad4 < 3.5 and kindPad4 > 2.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:circle() and kindPad4 < 4.5 and kindPad4 > 3.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:up() and kindPad1 < 5.5 and kindPad1 > 4.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:down() and kindPad1 < 6.5 and kindPad1 > 5.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:left() and kindPad1 < 7.5 and kindPad1 > 6.5 then balkenani = 1 entriegeltPad4 = 1 end
if pad:right() and kindPad1 < 8.5 and kindPad1 > 7.5 then balkenani = 1 entriegeltPad4 = 1 end
end

if leben < 0 then gamestate = "menue" punkte = 0 leben = 5 speed = 2 end

screen:print(210, 240, punkte.." Punkte", black) 
screen:print(210, 255, leben.." Leben", black) 

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