
-- MENU --

Select = 1
maxSelect = 3
gamestate = "Menu"
IntroFrame = 1

-- ##### Loading Sprites ##### --



white = Color.new(255,255,255)
black = Color.new(0,0,0)
grau = Color.new(100,100,100)
blue = Color.new(72,61,139)
Auto = Image.load("Games/FallBall/Pictures/Sprites/Auto1.png")
Auto2 = Image.load("Games/FallBall/Pictures/Sprites/Auto2.png")
wand = Image.load ("Games/FallBall/Pictures/Sprites/wand.jpg")
loch = Image.load ("Games/FallBall/Pictures/Sprites/loch.jpg")
n0 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/0.PNG")
n1 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/1.PNG")
n2 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/2.PNG")
n3 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/3.PNG")
n4 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/4.PNG")
n5 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/5.PNG")
n6 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/6.PNG")
n7 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/7.PNG")
n8 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/8.PNG")
n9 = Image.load ("Games/FallBall/Pictures/Sprites/numbers/9.PNG")
lvl = Image.load ("Games/FallBall/Pictures/Sprites/numbers/lvl.PNG")
p1 = Image.load ("Games/FallBall/Pictures/P1.PNG")
p2 = Image.load ("Games/FallBall/Pictures/P2.PNG")
p1w = Image.load ("Games/FallBall/Pictures/P1w.PNG")
p2w = Image.load ("Games/FallBall/Pictures/P2w.PNG")

-- ##### Posistioning Sprites ##### --

MPstate = "P1i"
punkte = 0
punkteMP1 = 0
punkteMP2 = 0
speed = 1
kollision = 0
wand1x = 175
wand2x = 350
wand3x = 525
AutoX = 228
AutoY = 100
AutoHeight = 40
AutoWidth = 25
speed1 = 5
speed2 = 4
SpeedAuto = 2
SaveData = 0
strasse1X = 0
strasse2X = 160
strasse3X = 320
strasse4X = 480
dx = 0
dy = 0
Pause = 0
Select = 0
maxSelect = 3
maxSelectHm = 4
time = 0
loch1y = math.random(0, 232) 
loch2y = math.random(0, 232) 
loch3y = math.random(0, 232) 



-- ##### Screen ##### --

function movewand()
     speed = speed + 0.001
     
     wand1x = wand1x - speed
     wand2x = wand2x - speed
     wand3x = wand3x - speed
     if kollision == 1 then AutoX = AutoX - speed end
     
     if wand1x < -45 then loch1y = nil wand1x = 480 loch1y = math.random(0, 232) end
     if wand2x < -45 then wand2x = 480 loch2y = math.random(0, 232) end
     if wand3x < -45 then wand3x = 480 loch3y = math.random(0, 232) end
end

function moveBack()
     if strasse1X <= -160 then
          strasse1X = 480
     end
     if strasse2X <= -160 then
          strasse2X = 480
     end
     if strasse3X <= -160 then
          strasse3X = 480
     end
     if strasse4X <= -160 then
          strasse4X = 480
     end

     strasse1X = strasse1X - speed
     strasse2X = strasse2X - speed
     strasse3X = strasse3X - speed
     strasse4X = strasse4X - speed
end





-- ##### Controls ##### --

function moveAuto()

if pad:square() and AutoX > 0 and kollision == 0 then
     AutoX = AutoX - 3
end
if pad:circle() and AutoX < 455 and kollision == 0 then
     AutoX = AutoX + 3
end
if pad:triangle() and AutoY > 0 then
     AutoY = AutoY - 3
end
if pad:cross() and AutoY < 247 then
     AutoY = AutoY + 3
end
if pad:select() then
     Image:save("ms0:/PSP/PHOTO/screen.png")
end
if pad:start() then
     gamestate = "Pause"
end
end     

-- ##### printRotated #####

function rotate(image)
	local w = image:width()
	local h = image:height()
	local result = Image.createEmpty(h, w)
	for x=0,w-1 do
		for y=0,h-1 do
			result:pixel(h-y-1, x, image:pixel(x, y))
		end
	end
	return result
end

function printRotated(x, y, text, color, image, rotateIndex)
	rotateIndex = math.mod(rotateIndex, 4)
	local w = string.len(text)
	local result = Image.createEmpty(w * 8, 8)
	result:print(0, 0, text, color)
	if rotateIndex > 0 then
		rotateIndex = rotateIndex - 1
		for i=0,rotateIndex do
			result = rotate(result)
		end
	end
	image:blit(x, y, result)
end

-- ##### Load Background #####

BG = Image.load("Games/FallBall/Pictures/BG.png")
Start = Image.load("Games/FallBall/Pictures/StartGame.png")
Options = Image.load("Games/FallBall/Pictures/Options.png")
QuitGame = Image.load("Games/FallBall/Pictures/QuitGame.png")
MenuSelect = Image.load("Games/FallBall/Pictures/MenuSelection.png")
Paused = Image.load("Games/FallBall/Pictures/Pause.png")
Optionen = Image.load("Games/FallBall/Pictures/optionen.png")
Back = Image.load("Games/FallBall/Pictures/Back.png")
Resume = Image.load("Games/FallBall/Pictures/Resume.png")
Menu = Image.load("Games/FallBall/Pictures/Menu.png")
Quit = Image.load("Games/FallBall/Pictures/Quit.png")
usb = Image.load("Games/FallBall/Pictures/usb.png")
ton = Image.load("Games/FallBall/Pictures/ton.png")
Multiplayer = Image.load("Games/FallBall/Pictures/Multi.png")

-- ##### Load Sounds #####
Move = Sound.load("Games/FallBall/Sounds/Move.wav",false)
SelectSound = Sound.load("Games/FallBall/Sounds/Select.wav",false)
Music.playFile("Games/FallBall/Sounds/Title.xm", true)

-- ##### Main Loop #####
gamestate = "Menu"
oldpad = Controls.read()
while true do
pad = Controls.read()
screen:clear()

if gamestate == "Menu" then    
     screen:blit(0, 0, BG)

     if pad:circle() and oldpad:circle() ~= pad:circle() and Select <= 4 then
          Select = Select + 1
          Move:play()
     end
     if pad:square() and oldpad:square() ~= pad:square() and Select >= 1 then
          Select = Select - 1
          Move:play()
     end
	if Select > maxSelectHm then
		Select = 1
	elseif Select < 1  then
		Select = 4
	end

     if Select == 1 then
          screen:blit(246+65,115,MenuSelect)
          screen:blit(250+65,100,Start)
          screen:blit(280+65,100,Options)
          screen:blit(310+65,100,Multiplayer)
          screen:blit(340+65,100,QuitGame)
     end
     if Select == 3 then
          screen:blit(306+65,115,MenuSelect)
          screen:blit(250+65,100,Start)
          screen:blit(280+65,100,Options)
          screen:blit(310+65,100,Multiplayer)
          screen:blit(340+65,100,QuitGame)
     end
     if Select == 2 then
          screen:blit(276+65,115,MenuSelect)
          screen:blit(250+65,100,Start)
          screen:blit(280+65,100,Options)
          screen:blit(310+65,100,Multiplayer)
          screen:blit(340+65,100,QuitGame)
     end
     if Select == 4 then
          screen:blit(336+65,115,MenuSelect)
          screen:blit(250+65,100,Start)
          screen:blit(280+65,100,Options)
          screen:blit(310+65,100,Multiplayer)
          screen:blit(340+65,100,QuitGame)
     end
     if Select == 1 and pad:cross() and oldpad:cross() ~= pad:cross() then          
               SelectSound:play()
               Music.stop()


Credits = Image.load("Games/FallBall/Pictures/Sprites/Credits.png")
               Music.playFile("Sounds/LevelOne.xm", true)
               gamestate = "Game"
     end

     if Select == 3 and pad:cross() and oldpad:cross() ~= pad:cross() then               SelectSound:play()
               Music.stop()


Credits = Image.load("Games/FallBall/Pictures/Sprites/Credits.png")
               Music.playFile("Games/FallBall/Sounds/LevelOne.xm", true)
               gamestate = "GameMP"
     end
     if Select == 2 and pad:cross() and oldpad:cross() ~= pad:cross() then     gamestate = "options"   SelectSound:play() end
     if Select == 4 and pad:cross() and oldpad:cross() ~= pad:cross() then
          System.Quit()
     end
end




   


if gamestate == "Game" then
screen:clear(blue)

moveAuto()

movewand()

screen:blit(wand1x, 0, wand)
screen:blit(wand2x,0, wand)
screen:blit(wand3x, 0, wand)
screen:blit(wand1x, loch1y, loch)
screen:blit(wand2x,loch2y, loch)
screen:blit(wand3x, loch3y, loch)
screen:blit(AutoX, AutoY, Auto)
screen:blit(10, 177, lvl)



punkte = punkte + 1
if punkte <= 100 then screen:blit (10, 162, n1) end
if punkte > 100 and punkte <= 200 then screen:blit (10, 162, n2) end
if punkte > 200 and punkte <= 300 then screen:blit (10, 162, n3) end
if punkte > 300 and punkte <= 400 then screen:blit (10, 162, n4) end
if punkte > 400 and punkte <= 500 then screen:blit (10, 162, n5) end
if punkte > 500 and punkte <= 600 then screen:blit (10, 162, n6) end
if punkte > 600 and punkte <= 700 then screen:blit (10, 162, n7) end
if punkte > 700 and punkte <= 800 then screen:blit (10, 162, n8) end
if punkte > 800 and punkte <= 900 then screen:blit (10, 162, n9) end
if punkte > 900 and punkte <= 1000 then screen:blit (10, 162, n1) screen:blit (10, 147, n0) end
if punkte > 1000 and punkte <= 1100 then screen:blit (10, 162, n1) screen:blit (10, 147, n1) end
if punkte > 1100 and punkte <= 1200 then screen:blit (10, 162, n1) screen:blit (10, 147, n2) end
if punkte > 1200 and punkte <= 1300 then screen:blit (10, 162, n1) screen:blit (10, 147, n3) end
if punkte > 1300 and punkte <= 1400 then screen:blit (10, 162, n1) screen:blit (10, 147, n4) end
if punkte > 1400 and punkte <= 1500 then screen:blit (10, 162, n1) screen:blit (10, 147, n5) end
if punkte > 1500 and punkte <= 1600 then screen:blit (10, 162, n1) screen:blit (10, 147, n6) end
if punkte > 1600 and punkte <= 1700 then screen:blit (10, 162, n1) screen:blit (10, 147, n7) end
if punkte > 1700 and punkte <= 1800 then screen:blit (10, 162, n1) screen:blit (10, 147, n8) end
if punkte > 1800 and punkte <= 1900 then screen:blit (10, 162, n1) screen:blit (10, 147, n9) end
if punkte > 1900 and punkte <= 2000 then screen:blit (10, 162, n2) screen:blit (10, 147, n0) end
if punkte > 2000 and punkte <= 2100 then screen:blit (10, 162, n2) screen:blit (10, 147, n1) end
if punkte > 2100 and punkte <= 2200 then screen:blit (10, 162, n2) screen:blit (10, 147, n2) end
if punkte > 2200 and punkte <= 2300 then screen:blit (10, 162, n2) screen:blit (10, 147, n3) end
if punkte > 2300 and punkte <= 2400 then screen:blit (10, 162, n2) screen:blit (10, 147, n4) end
if punkte > 2400 and punkte <= 2500 then screen:blit (10, 162, n2) screen:blit (10, 147, n5) end
if punkte > 2500 and punkte <= 2600 then screen:blit (10, 162, n2) screen:blit (10, 147, n6) end
if punkte > 2600 and punkte <= 2700 then screen:blit (10, 162, n2) screen:blit (10, 147, n7) end
if punkte > 2700 and punkte <= 2800 then screen:blit (10, 162, n2) screen:blit (10, 147, n8) end
if punkte > 2800 and punkte <= 2900 then screen:blit (10, 162, n2) screen:blit (10, 147, n9) end
if punkte > 2900 and punkte <= 3000 then screen:blit (10, 162, n3) screen:blit (10, 147, n0) end
if punkte > 3000 and punkte <= 3100 then screen:blit (10, 162, n3) screen:blit (10, 147, n1) end
if punkte > 3100 and punkte <= 3200 then screen:blit (10, 162, n3) screen:blit (10, 147, n2) end
if punkte > 3200 and punkte <= 3300 then screen:blit (10, 162, n3) screen:blit (10, 147, n3) end
if punkte > 3300 and punkte <= 3400 then screen:blit (10, 162, n3) screen:blit (10, 147, n4) end
if punkte > 3400 and punkte <= 3500 then screen:blit (10, 162, n3) screen:blit (10, 147, n5) end
if punkte > 3500 and punkte <= 3600 then screen:blit (10, 162, n3) screen:blit (10, 147, n6) end
if punkte > 3600 and punkte <= 3700 then screen:blit (10, 162, n3) screen:blit (10, 147, n7) end
if punkte > 3700 and punkte <= 3800 then screen:blit (10, 162, n3) screen:blit (10, 147, n8) end
if punkte > 3800 and punkte <= 3900 then screen:blit (10, 162, n3) screen:blit (10, 147, n9) end
if punkte > 3900 and punkte <= 4000 then screen:blit (10, 162, n4) screen:blit (10, 147, n0) end
if punkte > 4000 and punkte <= 4100 then screen:blit (10, 162, n4) screen:blit (10, 147, n1) end
if punkte > 4100 and punkte <= 4200 then screen:blit (10, 162, n4) screen:blit (10, 147, n2) end
if punkte > 4200 and punkte <= 4300 then screen:blit (10, 162, n4) screen:blit (10, 147, n3) end
if punkte > 4300 and punkte <= 4400 then screen:blit (10, 162, n4) screen:blit (10, 147, n4) end
if punkte > 4400 and punkte <= 4500 then screen:blit (10, 162, n4) screen:blit (10, 147, n5) end
if punkte > 4500 and punkte <= 4600 then screen:blit (10, 162, n4) screen:blit (10, 147, n6) end
if punkte > 4600 and punkte <= 4700 then screen:blit (10, 162, n4) screen:blit (10, 147, n7) end
if punkte > 4700 and punkte <= 4800 then screen:blit (10, 162, n4) screen:blit (10, 147, n8) end
if punkte > 4800 and punkte <= 4900 then screen:blit (10, 162, n4) screen:blit (10, 147, n9) end
if punkte > 4900 and punkte <= 5000 then screen:blit (10, 162, n5) screen:blit (10, 147, n0) end
if punkte > 5000 and punkte <= 5100 then screen:blit (10, 162, n5) screen:blit (10, 147, n1) end
if punkte > 5100 and punkte <= 5200 then screen:blit (10, 162, n5) screen:blit (10, 147, n2) end
if punkte > 5200 and punkte <= 5300 then screen:blit (10, 162, n5) screen:blit (10, 147, n3) end
if punkte > 5300 and punkte <= 5400 then screen:blit (10, 162, n5) screen:blit (10, 147, n4) end
if punkte > 5400 and punkte <= 5500 then screen:blit (10, 162, n5) screen:blit (10, 147, n5) end
if punkte > 5500 and punkte <= 5600 then screen:blit (10, 162, n5) screen:blit (10, 147, n6) end
if punkte > 5600 and punkte <= 5700 then screen:blit (10, 162, n5) screen:blit (10, 147, n7) end
if punkte > 5700 and punkte <= 5800 then screen:blit (10, 162, n5) screen:blit (10, 147, n8) end
if punkte > 5800 and punkte <= 5900 then screen:blit (10, 162, n5) screen:blit (10, 147, n9) end
if punkte > 5900 and punkte <= 6000 then screen:blit (10, 162, n6) screen:blit (10, 147, n0) end
if punkte > 6000 and punkte <= 6100 then screen:blit (10, 162, n6) screen:blit (10, 147, n1) end
if punkte > 6100 and punkte <= 6200 then screen:blit (10, 162, n6) screen:blit (10, 147, n2) end
if punkte > 6200 and punkte <= 6300 then screen:blit (10, 162, n6) screen:blit (10, 147, n3) end
if punkte > 6300 and punkte <= 6400 then screen:blit (10, 162, n6) screen:blit (10, 147, n4) end
if punkte > 6400 and punkte <= 6500 then screen:blit (10, 162, n6) screen:blit (10, 147, n5) end
if punkte > 6500 and punkte <= 6600 then screen:blit (10, 162, n6) screen:blit (10, 147, n6) end
if punkte > 6600 and punkte <= 6700 then screen:blit (10, 162, n6) screen:blit (10, 147, n7) end
if punkte > 6700 and punkte <= 6800 then screen:blit (10, 162, n6) screen:blit (10, 147, n8) end
if punkte > 6800 and punkte <= 6900 then screen:blit (10, 162, n6) screen:blit (10, 147, n9) end
if punkte > 6900 and punkte <= 7000 then screen:blit (10, 162, n7) screen:blit (10, 147, n0) end





if kollision == 1 then
if AutoX < -10 then
   punkte = 0
   speed = 1
   kollision = 0
   AutoX = 228
   AutoY = 100
   Music.stop()
   Music.playFile("Games/FallBall/Sounds/Title.xm", true)
   gamestate = "Menu"
end
end


if AutoX + 25 > wand1x and AutoX + 25 < wand1x + 10 then
   if AutoY > loch1y and AutoY < loch1y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end


if AutoX + 25 > wand2x and AutoX + 25 < wand2x + 10 then
   if AutoY > loch2y and AutoY < loch2y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end


if AutoX + 25 > wand3x and AutoX + 25 < wand3x + 10 then
   if AutoY > loch3y and AutoY < loch3y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end
end


if gamestate == "GameMP" then

if MPstate == "P1i" then
   screen:clear()
   screen:blit (100, 30, p1)
   MPstate = "P1"
   screen.flip()
   screen.waitVblankStart(180)
end

if MPstate == "P1" then

screen:clear(blue)

moveAuto()

movewand()


screen:blit(wand1x, 0, wand)
screen:blit(wand2x,0, wand)
screen:blit(wand3x, 0, wand)
screen:blit(wand1x, loch1y, loch)
screen:blit(wand2x,loch2y, loch)
screen:blit(wand3x, loch3y, loch)
screen:blit(AutoX, AutoY, Auto)
screen:blit(10, 177, lvl)

punkteMP1 = punkteMP1 + 1

if kollision == 0 then punkte = punkte + 1 end
if punkte <= 1000 then screen:blit (10, 162, n1) end
if punkte > 1000 and punkte <= 2000 then screen:blit (10, 162, n2) end
if punkte > 2000 and punkte <= 3000 then screen:blit (10, 162, n3) end
if punkte > 3000 and punkte <= 4000 then screen:blit (10, 162, n4) end
if punkte > 4000 and punkte <= 5000 then screen:blit (10, 162, n5) end
if punkte > 5000 and punkte <= 6000 then screen:blit (10, 162, n6) end
if punkte > 6000 and punkte <= 7000 then screen:blit (10, 162, n7) end
if punkte > 7000 and punkte <= 8000 then screen:blit (10, 162, n8) end
if punkte > 8000 and punkte <= 9000 then screen:blit (10, 162, n9) end



if kollision == 1 then
if AutoX < -10 then
   punkte = 0
   speed = 1
   kollision = 0
   AutoX = 228
   AutoY = 100
   MPstate = "P2i"
end
end


if AutoX + 25 > wand1x and AutoX + 25 < wand1x + 10 then
   if AutoY > loch1y and AutoY < loch1y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end


if AutoX + 25 > wand2x and AutoX + 25 < wand2x + 10 then
   if AutoY > loch2y and AutoY < loch2y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end


if AutoX + 25 > wand3x and AutoX + 25 < wand3x + 10 then
   if AutoY > loch3y and AutoY < loch3y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end
end
if MPstate == "P2i" then
   screen:clear()
   screen:blit (100, 30, p2)
   MPstate = "P2"
   screen.flip()
   screen.waitVblankStart(180)
end

if MPstate == "P2" then

screen:clear(blue)

moveAuto()

movewand()


screen:blit(wand1x, 0, wand)
screen:blit(wand2x,0, wand)
screen:blit(wand3x, 0, wand)
screen:blit(wand1x, loch1y, loch)
screen:blit(wand2x,loch2y, loch)
screen:blit(wand3x, loch3y, loch)
screen:blit(AutoX, AutoY, Auto2)
screen:blit(10, 177, lvl)

punkteMP2 = punkteMP2 + 1

if kollision == 0 then punkte = punkte + 1 end
if punkte <= 1000 then screen:blit (10, 162, n1) end
if punkte > 1000 and punkte <= 2000 then screen:blit (10, 162, n2) end
if punkte > 2000 and punkte <= 3000 then screen:blit (10, 162, n3) end
if punkte > 3000 and punkte <= 4000 then screen:blit (10, 162, n4) end
if punkte > 4000 and punkte <= 5000 then screen:blit (10, 162, n5) end
if punkte > 5000 and punkte <= 6000 then screen:blit (10, 162, n6) end
if punkte > 6000 and punkte <= 7000 then screen:blit (10, 162, n7) end
if punkte > 7000 and punkte <= 8000 then screen:blit (10, 162, n8) end
if punkte > 8000 and punkte <= 9000 then screen:blit (10, 162, n9) end



if kollision == 1 then
if AutoX < -10 then
   punkte = 0
   speed = 1
   kollision = 0
   AutoX = 228
   AutoY = 100
   MPstate = "MPend"
end
end


if AutoX + 25 > wand1x and AutoX + 25 < wand1x + 10 then
   if AutoY > loch1y and AutoY < loch1y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end


if AutoX + 25 > wand2x and AutoX + 25 < wand2x + 10 then
   if AutoY > loch2y and AutoY < loch2y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end


if AutoX + 25 > wand3x and AutoX + 25 < wand3x + 10 then
   if AutoY > loch3y and AutoY < loch3y + 30 then kollision = 0
   else
   kollision = 1
   end
   
end
end

if MPstate == "MPend" then
   screen:clear()
   if punkteMP2 > punkteMP1 then screen:blit(100, 30, p2w) end
   if punkteMP2 < punkteMP1 then screen:blit(100, 30, p1w) end
   MPstate = "P1i"
   gamestate = "Menu"
   Music.stop()
   Music.playFile("Sounds/Title.xm", true)
   screen.flip()
   screen.waitVblankStart(180)
end
end

if gamestate == "options" then    
   screen:blit(0, 0, Optionen)

     if pad:circle() and oldpad:circle() ~= pad:circle() and Select <= 3 then
          Select = Select + 1
          Move:play()
     end
     if pad:square() and oldpad:square() ~= pad:square() and Select >= 1 then
          Select = Select - 1
          Move:play()
     end
	if Select > maxSelect then
		Select = 1
	elseif Select <= 0 then
		Select = maxSelect
	end

     if Select == 1 then
          screen:blit(276+65,115,MenuSelect)
          screen:blit(280+65,100,usb)
          screen:blit(310+65,100,ton)
          screen:blit(340+65,100,Back)
     end
     if Select == 2 then
          screen:blit(306+65,115,MenuSelect)
          screen:blit(280+65,100,usb)
          screen:blit(310+65,100,ton)
          screen:blit(340+65,100,Back)
     end
     if Select == 3 then
          screen:blit(336+65,115,MenuSelect)
          screen:blit(280+65,100,usb)
          screen:blit(310+65,100,ton)
          screen:blit(340+65,100,Back)
     end

     if Select == 1 and pad:cross() and oldpad:cross() ~= pad:cross() then
                System.usbDiskModeActivate()
     end

     if Select == 1 and pad:circle() and oldpad:circle() ~= pad:circle() then
                System.usbDiskModeDeactivate()
     end

     if Select == 2 and pad:cross() and oldpad:cross() ~= pad:cross() then
                Music.volume(128)
     end


     if Select == 2 and pad:circle() and oldpad:circle() ~= pad:circle() then
                Music.volume(0)
     end

     if Select == 3 and pad:cross() and oldpad:cross() ~= pad:cross() then
          gamestate = "Menu"
     end
end



if gamestate == "Pause" then
screen:clear()    
     screen:blit(0, 0, Paused, true)


     if pad:circle() and oldpad:circle() ~= pad:circle() and Select <= 3 then
          Select = Select + 1
          Move:play()
     end
     if pad:square() and oldpad:square() ~= pad:square() and Select >= 1 then
          Select = Select - 1
          Move:play()
     end
	if Select > maxSelect then
		Select = 1
	elseif Select <= 0 then
		Select = maxSelect
	end

     if Select == 1 then
          screen:blit(276+65,115,MenuSelect)
          screen:blit(280+65,100,Resume)
          screen:blit(310+65,100,Menu)
          screen:blit(340+65,100,Quit)
     end
     if Select == 2 then
          screen:blit(306+65,115,MenuSelect)
          screen:blit(280+65,100,Resume)
          screen:blit(310+65,100,Menu)
          screen:blit(340+65,100,Quit)
     end
     if Select == 3 then
          screen:blit(336+65,115,MenuSelect)
          screen:blit(280+65,100,Resume)
          screen:blit(310+65,100,Menu)
          screen:blit(340+65,100,Quit)
     end
     if Select == 1 and pad:cross() and oldpad:cross() ~= pad:cross() then
          SelectSound:play()
          gamestate = "Game"
     end
     if Select == 2 and pad:cross() and oldpad:cross() ~= pad:cross() then
          SelectSound:play()
          Music.playFile("Sounds/Title.xm", true)
          punkte = 0
          speed = 1
          kollision = 0
          AutoX = 228
          AutoY = 100
          MPstate = "P1i"
          gamestate = "Menu"
     end
     if Select == 3 and pad:cross() and oldpad:cross() ~= pad:cross() then
          System.Quit()
     end
end


if gamestate == "Credits" then
     screen:blit(0, 0, Black)
     screen:blit(113, 10, DemoComplete)
     screen:blit(90, 72, Credits)
     screen:blit(129, 250, PressX)
          if pad:cross() and oldpad:cross() ~= pad:cross() then
          wolkencroll = 0
          Polizei1Y = -40
          Polizei2Y = -40
          Polizei3Y = -40
          Polizei4Y = -40
          AutoX = 150
          AutoY = 100
               SelectSound:play()
               Music.playFile("Sounds/Title.xm", true)
               gamestate = "Menu"
          end
end

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