screen:blit(0,0,Image.load("img/splash.png"))
screen.flip()
System.sioInit(510416)
bgimg = Image.load("img/bg.png")
m = Image.load("img/m.png")
p = Image.load("img/p.png")
ball = Image.load("img/ball.png")
s1 = Image.load("img/s1.png")
b1 = Image.load("img/b1.png")
b2 = Image.load("img/b2.png")
pfnt = Font.load("font.ttf")
pfnt:setPixelSizes(0, 24)
gld = {"0000000000011100000121000001110000011100000111000001110000000000",
"0000000000000000000020000000100000011100000111000001110000000000",
"0000000000000000000020000000100000001000000010000000000000000000",
"0000000000000000000000000021100000001000000010000000000000000000",
"0000000000000000000200000001100000001000000010000000000000000000",
"0000000000000000000000000011200000100000001110000000000000000000",
"0000000000000000000000000011120000100000001010000011100000000000",
"0000000000000000000000000120111001000010011010100011111000000000",
"0000000000000000000000000011111000100010001010100020111000000000",
"0000000000000000000021100011001000100010001111100000100000000000",
"0000000000002110000000100000011000001100000010000000000000000000",
"0000000000201110001000100011011000011100000010000000000000000000",
"0000000000000200000001000111110001000000011010000011100000000000",
"0000000000011100000101000002011000000010000010100000111000000000",
"0000000001110000010111100100001001120010000011100000000000000000",
"0000000000000000000111000011010011101102000010000000000000000000",
"0000100000111000001002000011110000000000000010000000100000001000",
"0010001111100001000000000000000000000000000010000020110000100110",
"0000000100000011000001100000020000000000000010001111100100000001",
"0000000000000000200111010011011100100000001110000000000000000000",
"0000010000211000000010000000100000001000011111110000100000001000",
"0000000000000000020100000101000001011000010010000111110000000000",
"0000010000000100001111000110000011001111000010000000000012000111",
"0002000000000000000000001110111010101010101010110010000000111000",
"1011111100000000200000001011111010100010101010101010111010100000",
"0011110001100110001011000110011011001102000010000000000000000000",
"1100000001011100011101000000010000000200100010111000111010000000",
"0010000000111100000101000111011001000010020110100011000000100000",
"0101110011010001000111111110000010101110101010110010020001100100",
"0100102001111010000001100111110101000110110110010001000111011001"}
function get_xy()
	mox,moy = 0,0
	for i = 1, 10 do 
		System.sioWrite("11111111")
		mox = mox + string.byte(System.sioRead())
		System.sioWrite("22222222")
		moy = moy + string.byte(System.sioRead())
	end
	mox,moy = mox/10,moy/10
end
function bg()
	screen:blit((math.max(math.min(moy-128,32),-32)/2)-16,(math.max(math.min(mox-128,32),-32)/2)-16,bgimg)
end
function main()
	sel = 0
	in2 = Controls.read()
	while true do
		inp = Controls.read()
		get_xy()
		bg()
		if inp:up() and not in2:up() then
			sel=sel-1
			if sel == -1 then sel=2 end
		end
		if inp:down() and not in2:down() then
			sel=sel+1
			if sel == 3 then sel=0 end
		end
		if inp:cross() and not in2:cross() then
			if sel == 0 then bg() play() end
			if sel == 1 then how() end
			if sel == 2 then break end
		end
		screen:blit(140,93,m,0,sel*86,199,86)
		in2=inp
		screen.waitVblankStart()
		screen.flip()
	end
end
function how()
	pg = -1
	timer = 0
	pg2 = true
	while true do
		inp = Controls.read()
		get_xy()
		bg()
		if inp:cross() and not in2:cross() then
			pg = pg + 1
		end
		if pg == 0 then
			timer = timer + 1
			if timer == 40 then
				pg2 = not pg2
				timer = 0
			end
			if pg2 then
				screen:blit(0,45,p,0,0,480,183)
			else
				screen:blit(0,45,p,0,183,480,167)
			end
		elseif pg == 1 then
			screen:blit(0,45,p,0,350,480,149)
		else break end
		in2=inp
		screen.waitVblankStart()
		screen.flip()
	end
end
function level(lev)
	levimg = Image.createEmpty(512,512)
	levsha = Image.createEmpty(512,512)
	for y = 1,8 do
		for x = 1,8 do
			type = string.byte(gld[lev],8*y-8+x)
			if type == 49 then
				levimg:blit(64*x-64,64*y-64,b1)
				levsha:fillRect(64*x-64,64*y-64,64,64,Color.new(76,69,109))
			elseif type == 50 then
				levimg:blit(64*x-64,64*y-64,b2)
				levsha:fillRect(64*x-64,64*y-64,64,64,Color.new(76,69,109))
			end
		end
	end
end
function msg(txt,t1)
	temp = {{Color.new(0,0,0,130),480,272,0},{Color.new(0,0,0,130),0,272,0},{Color.new(0,0,0,130),480,0,0},{Color.new(0,0,0,130),0,0,0}}
	Gu.start3d()
	Gu.disable(Gu.TEXTURE_2D)
	Gum.drawArray(Gu.TRIANGLE_STRIP, Gu.COLOR_8888+Gu.VERTEX_32BITF+Gu.TRANSFORM_2D, temp)
	Gu.end3d()
	wpt = pfnt:getTextSize(txt)["width"]
	screen:fontPrint(pfnt, (480-pfnt:getTextSize(txt)["width"])/2, 120, txt, Color.new(255,255,255))
	screen:fontPrint(pfnt, (480-pfnt:getTextSize(t1)["width"])/2, 160, t1, Color.new(210,210,210))
	screen.flip()
	while true do if not Controls.read():cross() then break end screen.waitVblankStart() end
	while true do
		inp = Controls.read()
		if inp:cross() and not in2:cross() then return false end
		if inp:circle() and not in2:circle() then return true end
		in2=inp
	end
end
function play()
	lev = 1
	br=false
	while true do
		if lev==31 then
			if msg("Congratulations! You beat the game!","O Main     X Restart") then break else bg() lev = 1 end
		end
		level(lev)
		xr,yr = 5,6
		xv,yv = 0,0
		if msg("Level " .. lev,"O Quit     X Start") then br = true break else
			while true do
				if Controls.read():start() then
					if msg("PAUSED","O Quit     X Continue") then br=true break end
				end
				get_xy()
				bg()
				xv,yv = math.max(math.min(xv+(mox-128),32),-32)*((lev+10)/10)/5,math.max(math.min(yv-(moy-128),32),-32)*((lev+10)/10)/5
				xr,yr = xr+(yv/100),yr+(xv/100)
				if xr<.5 then xr=xr+8 end
				if yr<.5 then yr=yr+8 end
				if xr>8.5 then xr=xr-8 end
				if yr>8.5 then yr=yr-8 end
				xl,yl = xr*64+240,yr*64-168
				if xl<0 then xl=xl+512 end
				if yl<0 then yl=yl+512 end
				xs,ys = xl+yv*1.5,yl+xv*1.5
				if xs<0 then xs=xs+512 end
				if ys<0 then ys=ys+512 end
				screen:blit(0,0,levsha,xs,ys,480,272)
				screen:blit(0,0,levimg,xl,yl,480,272)
				screen:blit(208+yv*1.5,104+xv*1.5,s1)
				screen:blit(208,104,ball)
				screen.waitVblankStart()
				screen.flip()
				type = string.byte(gld[lev],math.floor(yr+.5)*8-8+math.floor(xr+.5))
				if type == 48 then
					if msg("FAIL","O Quit     X Retry") then
						br=true break
					else break end
				elseif type == 50 then
					if msg("Level Complete!","O Quit     X Next") then
						br=true break
					else lev=lev+1 break end
				end
			end
		if br then break end
		end
	end
end
main() 