Object is to trap the blue circle with red squares. Just tap the screen around the circle. There are 9 levels.
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
function setup()
rectMode(CENTER)
fontSize(30)
size=30
pos={vec2(12,15),vec2(12,16),vec2(12,17),vec2(11,16),vec2(13,16),
vec2(12,14),vec2(12,18),vec2(10,16),vec2(14,16)}
count=0
redSquares={0,0,0,0,0,0,0,0,0}
setup1()
end
function setup1()
count=count+1
if count==10 then
restart()
return
end
allTrapped=false
allow=false
tab={}
for x=1,25 do
tab[x]={}
for y=1,32 do
tab[x][y]=0
end
end
dots={}
for z=1,count do
table.insert(dots,dot(pos[z]))
end
end
function draw()
background(40, 40, 50)
for x=1,25 do
for y=1,32 do
fill(255)
if tab[x][y]==1 then
fill(255,0,0)
end
rect(x*size-5,y*size+25,size,size)
end
end
for a,b in pairs(dots) do
b:drawDot()
b:moveDot()
end
trapped()
allow=false
for z=1,9 do
text(redSquares[z],z*80,HEIGHT-50)
end
end
function trapped()
allTrapped=true
for a,b in pairs(dots) do
if not b.trapped then
allTrapped=false
end
end
if allTrapped then
fill(0,0,255)
text("Trapped",WIDTH/2,HEIGHT-100)
text("Double tap for next level.",WIDTH/2,HEIGHT-150)
end
end
function touched(t)
if t.state==BEGAN then
if allTrapped then
if t.tapCount==2 then
setup1()
end
return
end
local tx=math.ceil((t.x-5)/size)
local ty=math.ceil((t.y-35)/size)
if tx<1 or tx>25 or ty<1 or ty>32 then
return
end
local valid=true
for a,b in pairs(dots) do
if tx==b.x and ty==b.y then
valid=false
end
end
if valid then
redSquares[count]=redSquares[count]+1
tab[tx][ty]=1
end
allow=true
end
end
dot=class()
function dot:init(d)
self.x=d.x
self.y=d.y
self.trapped=false
tab[d.x][d.y]=2
end
function dot:drawDot()
fill(0,0,255)
ellipse(self.x*size-5,self.y*size+25,20)
end
function dot:moveDot()
if not allow or self.trapped then
return
end
local dir={0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1}
local used={0,0,0,0,0,0,0,0}
while true do
self.trapped=true
for z=1,8 do
if used[z]==0 then
self.trapped=false
end
end
if self.trapped then
return
end
off=math.random(1,8)
used[off]=1
off=(off-1)*2+1
hx=self.x
hy=self.y
self.x=self.x+dir[off]
self.y=self.y+dir[off+1]
if self.x>25 then self.x=1 end
if self.x<1 then self.x=25 end
if self.y>32 then self.y=1 end
if self.y<1 then self.y=32 end
if tab[self.x][self.y] > 0 then
self.x=hx
self.y=hy
else
tab[hx][hy]=0
tab[self.x][self.y]=2
return
end
end
end