Maze scene, originally retrieved from dave1707:
MazeScene = class()
function MazeScene:init(x)
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
c1=physics.body(CIRCLE, 15)
c1.x=200
c1.y=20
c1.gravityScale=0
c1.sleepingAllowed=false
tab1={}
tab2={}
EAST = 0
NORTH = 1
WEST = 2
SOUTH = 3
EWALL = 1
NWALL = 2
WWALL = 4
SWALL = 8
XOFF = 30
YOFF = 70
d=0
ALLWALLS = NWALL + EWALL + SWALL + WWALL
VISITED = 16
NMAX = 15
N = 10
pN = 10
M = WIDTH
XYOFF = XOFF
if HEIGHT < WIDTH then
M = HEIGHT
XYOFF = YOFF
end
stack = {}
startx = 1; starty = 1
endx = N; endy = N
createmaze(N, startx, starty, endx, endy)
--solvemaze()
end
-- This function gets called once every frame
function MazeScene:draw()
-- This sets a dark background color
background(0,0,0)
translate(0,100)
strokeWidth(2)
fill(255, 0, 0, 255)
ellipse(c1.x, c1.y, 30)
c1.x = c1.x+Gravity.x*15
c1.y = c1.y+Gravity.y*15
fill(255, 255, 255, 255)
stroke(255, 255, 255, 255)
for i=1, N do
x = XOFF + (i-1)*S
for j=1, N do
y = YOFF + (j-1)*S
c = maze[i][j]
w = c%VISITED
we = w%2
wn = math.floor(w/NWALL)%2
ww = math.floor(w/WWALL)%2
ws = math.floor(w/SWALL)
if we == 1 then
line(x+S+150, y-50, x+S+150, y-S-50)
pb(x+S+150,y-50,x+S+150,y-S-50)
end
if wn == 1 then
line(x+150, y-50, x+S+150, y-50)
pb(x+150, y-50, x+S+150, y-50)
end
if ww == 1 then
line(x+150, y-50, x+150, y-S-50)
pb(x+150, y-50, x+150, y-S-50)
end
if ws == 1 then
line(x+150, y-S-50, x+S+150, y-S-50)
pb(x+150, y-S-50, x+S+150, y-S-50)
end
end
end
done=true
if c1.x > 800 then
Scene.Change("Screen")
end
end
--print("x: "..CurrentTouch.x.." y: "..CurrentTouch.y)
-- print("x: "..c1.x.." y: "..c1.y)
function MazeScene:pb(x,y,x1,y1)
local z
if not done then
for z=1,#tab1 do
if tab1[z].x==x and tab1[z].y==y and
tab1[z].z==x1 and tab1[z].w==y1 then
return
end
end
table.insert(tab1,vec4(x,y,x1,y1))
table.insert(tab2,physics.body(EDGE,vec2(x,y),vec2(x1,y1)))
tab2[#tab2].sleepingAllowed=false
end
end
function MazeScene:createmaze(n, sx, sy, ex, ey)
if ex > N then
ex = N; ey = N
end
S = math.floor((M - 2*XYOFF)/N)
maze = {}
for i=0, NMAX+1 do
maze[i] = {}
for j = 0, NMAX+1 do
maze[i][j] = ALLWALLS
if i == 0 or i == N+1 then
maze[i][j] = maze[i][j] + VISITED
elseif j == 0 or j == N+1 then
maze[i][j] = maze[i][j] + VISITED
end
end
end
x = ex; y = ey
sp = 1
setvisited(ex, ey)
erasewalls(sx-1, sy, sx, sy)
erasewalls(ex, ey, ex+1, ey)
while true do
nx, ny = getunvisitedneighbor(x, y)
if nx < 0 then
x, y = pop()
if sp == 1 then
break
end
else
setvisited(nx, ny)
push(x, y)
erasewalls(x, y, nx, ny)
x = nx; y = ny
end
end
end
function MazeScene:push(a, b)
stack[sp] = a
stack[sp+1] = b
sp = sp + 2
end
function pop()
sp = sp - 1
b = stack[sp]
a = stack[sp-1]
sp = sp - 1
return a, b
end
function getneighbor(x, y, d)
--print("x ", x, "y ", y, "d ", d)
if d == EAST then
x = x + 1
elseif d == NORTH then
y = y + 1
elseif d == WEST then
x = x -1
else
y = y - 1
end
return x, y
end
function notvisited(x, y)
if x < 1 or x > N then
return false
end
if y < 1 or y > N then
return false
end
v = math.floor(maze[x][y] / VISITED)
if v >= 1 then
return false
else
return true
end
end
function MazeScene:setvisited(x, y)
maze[x][y] = maze[x][y] + VISITED
end
function getunvisitedneighbor(x, y)
nu = 0
uds = {}
for i=EAST, SOUTH do
nx, ny = getneighbor(x, y, i)
if notvisited(nx, ny)then
nu = nu + 1
uds[nu] = i
end
end
if nu == 0 then
return -1, -1
end
td = math.random(1, nu)
nx, ny = getneighbor(x, y, uds[td])
return nx, ny
end
function getdirection(x, y, nx, ny)
if nx > x then
d = EAST
elseif ny > y then
d = NORTH
elseif nx < x then
d = WEST
else
d = SOUTH
end
return d
end
function MazeScene:erasewalls(x, y, nx, ny)
d = getdirection(x, y, nx, ny)
if d == EAST then
maze[x][y] = maze[x][y] - EWALL
maze[nx][ny] = maze[nx][ny] - WWALL
elseif d == NORTH then
maze[x][y] = maze[x][y] - NWALL
maze[nx][ny] = maze[nx][ny] - SWALL
elseif d == WEST then
maze[x][y] = maze[x][y] - WWALL
maze[nx][ny] = maze[nx][ny] - EWALL
else
maze[x][y] = maze[x][y] - SWALL
maze[nx][ny] = maze[nx][ny] - NWALL
end
end