--!name Brick Breaker
--!icon game
--!category Games
local r=snail.touchscreen==true local M,N=14,14 local q=12 local E,A,X=2,3,6 local F,Y=13,3 local ag=1 local af={{10,2,"up",true}}local ap={"..^!..","..++..","<++++>","=++++?","..++..","..~|.."}local h,L="menu",1 local G={}local e,n,b,f,j=6,13,1,-1,5 local t,P,v,x=true,0,0,0 local z,J,u,Q=0,3,1,0 local w,o,R,D,ac=false,1,nil,0,false local m={0,0}local W=1 local function aB(ax)W=(W*1664525+1013904223)&0xFFFFFFFF return(W>>16)%ax end local function aa()if o==1 then return math.max(450,700-(u-1)*50)end return math.max(450,550-(u-1)*50)end local function aC()snail.save(m[1]..","..m[2])end local function an()t,P=true,0 e,n,f=j+1,F-1,-1 b=aB(2)==0 and-1 or 1 end local function ao()for a=1,A do G[a]={}for c=1,X do G[a][c]=true end end Q=A*X an()end local function ay()if os and os.time then W=os.time()%100000+11 end z,J,u,j=0,3,1,5 ao()v,x=0,1 w,R,D=false,nil,0 h="play"snail.tick(aa())end local function az()h="over"snail.tick(0)ac=z>m[o]if ac then m[o]=z aC()end end local function ar(p,ae)local a=ae-E+1 if a<1 or a>A or p<1 or p>q then return nil end local c=(p-1)//2+1 if G[a][c]then return a,c end end local function Z(p,ae)local a,c=ar(p,ae)if not a then return false end G[a][c]=false Q=Q-1 z=math.min(9999,z+(A+1-a)*10)return true end local function ak()if e+b<1 or e+b>q then b=-b end if n+f<E then f=1 end local I=Z(e,n+f)if I then f=-f end if Z(e+b,n)then b,I=-b,true end if not I and Z(e+b,n+f)then b,f,I=-b,-f,true end return I end local function T(p)return p>=j and p<j+Y end local function aE()if ak()and ak()then return end local S,ad=e+b,n+f if ad==F then local _=T(S)and S or T(e)and e if _ then if _==j then b=-1 elseif _==j+Y-1 then b=1 end if e+b<1 or e+b>q then b=-b end f=-1 S,ad=e+b,n+f else J=J-1 v=2 end end e,n=S,ad end function tick()if h~="play"or w then snail.tick(0)return end if x>0 then x=x-1 return end if D>0 then D=D-1 if D==0 then R=nil end end if v>0 then v=v-1 if v==0 then if J==0 then az()else an()end end return end if Q==0 then u=u+1 ao()R,D="LEVEL "..u,2 snail.tick(aa())return end if t then P=P+1 if P>=5 then t=false end return end aE()end local function al()return h=="play"and not w and x==0 and v==0 end local function ab(aA)j=math.max(1,math.min(q-Y+1,aA))if t then e=j+1 end end function key(c)if h=="menu"then if c=="up"or c=="down"then L=3-L elseif c=="ok"then o=L ay()end return end if h=="over"then if c=="ok"then h="menu"end return end if c=="ok"then w=not w snail.tick(w and 0 or aa())return end if not al()then return end if c=="left"then ab(j-1)return true elseif c=="right"then ab(j+1)return true elseif c=="up"or c=="down"then t=false end end local function at(l,C)local a,d=C-N-1,l-1 if a<0 then return nil end local b,f=2*(d-ag)-5,2*a-5 if b>=-7 and b<=7 then local ai,aj=math.abs(b),math.abs(f)if ai>aj then return b<0 and"left"or"right"end if aj>ai then return f<0 and"up"or"down"end return nil end for ah,s in ipairs(af)do if d>=s[1]and d<=s[1]+1 then return s[3]end end end function touch(aw,l,C)if aw~="board"then return end if r then local c=at(l,C)if c then return key(c)end else l=l-1 end if C<=N and al()then if t and T(l-1)then t=false else ab(l-2)end end end local function au()local k={}for a=1,6 do k[a]={}for d=1,M do k[a][d]="."end for i=1,6 do k[a][ag+i]=ap[a]:sub(i,i)end end for ah,s in ipairs(af)do local U,a,d=s[4]and"()[]"or"$&;:",s[2]+1,s[1]+1 k[a][d],k[a][d+1]=U:sub(1,1),U:sub(2,2)k[a+1][d],k[a+1][d+1]=U:sub(3,3),U:sub(4,4)end for a=1,6 do k[a]=table.concat(k[a])end return k end local function aD(H)local K={}for a=0,N-1 do local y=table.concat(H[a])K[#K+1]=r and y or"."..y.."."end if r then for ah,V in ipairs(au())do K[#K+1]=V end end return table.concat(K,"/")end local function aq()local B=x>0 local H={}for a=0,N-1 do local V={}for d=1,M do V[d]="."end H[a]=V end local function g(d,a,as)H[a][d+1]=as end local y=B and"8888"or string.format("%4d",z):gsub(" ","_")for i=1,4 do g(i-1,0,y:sub(i,i))end for i=1,3 do g(10+i,0,(B or J>=i)and"b"or"B")end g(0,1,"y")g(M-1,1,"z")for d=1,q do g(d,1,"t")end for a=2,F do g(0,a,"l")g(M-1,a,"r")end for a=1,A do for c=1,X do local am=B or G[a][c]g(2*c-1,E+a-1,am and"e"or"E")g(2*c,E+a-1,am and"f"or"F")end end for a=E+A,F-1 do for d=1,q do g(d,a,B and"b"or"B")end end for d=1,q do g(d,F,(B or T(d))and"p"or"P")end if not B then g(e,n,"b")end return aD(H)end function draw()local av=string.format("GAME %s  BEST %d",o==1 and"A"or"B",m[o])snail.title((r and h~="menu")and"Brick Breaker  "..av or"Brick Breaker")if h=="menu"then snail.row("Game A",L==1)snail.row("Game B (faster)",L==2)snail.gap()snail.text(string.format("Best: A %d   B %d",m[1],m[2]))snail.gap()snail.small("Bounce the ball off the paddle to knock out every brick.")snail.small("Clear the wall and the next one comes faster.")if r then snail.small("Move with the d-pad or tap where the paddle should go;")snail.small("the round button, or a tap on the paddle, serves.")else snail.small("LEFT and RIGHT move the paddle (hold to slide);")snail.small("UP or DOWN serves the ball.")end snail.small("An end of the paddle sends the ball that way.")snail.small("Three balls.")snail.hint("OK start BACK leave")return end if not r then snail.small(string.format("GAME %s   LEVEL %d   BEST %d",o==1 and"A"or"B",u,m[o]))end local O=R if x>0 then O="GAME "..(o==1 and"A"or"B")end if w then O="PAUSED"end if h=="over"then O=string.format("GAME OVER  %d%s",z,ac and"  BEST!"or"")end snail.board(aq(),nil,O)if h=="over"then snail.hint("OK menu BACK leave")elseif r then snail.hint("OK pause BACK leave")else snail.hint("L left R right OK pause BACK leave")end end function start()snail.ink("fast")local y=snail.load()if y then local l,C=y:match("(%d+),(%d+)")m={tonumber(l)or 0,tonumber(C)or 0}end end
