Here’s I suppose what you could call the foundation to starting a falling sand game, not too hard for beginners either:
-- cells
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
p = {}
filled = {}
tn = 30
time = 0
m = {}
rect = {}
for x = 1,tn do
p[x] = {}
rect[x] = {}
m[x] = {}
filled[x] = {}
for y = 1,tn do
m[x][y] = mesh()
filled[x][y] = 0
p[x][y] = vec2(-(WIDTH/tn)/2+(WIDTH/tn)*x,-(HEIGHT/tn)/2+(HEIGHT/tn)*y)
rect[x][y] = m[x][y]:addRect(p[x][y].x,p[x][y].y,WIDTH/tn,HEIGHT/tn)
end
end
end
function touched(t)
if t.state == BEGAN then time = 5 else time = time + 1 end
tpos = vec2(t.x,t.y)
for x = 1,tn do
for y = 1,tn do
if t.x < x*WIDTH/tn and t.y < y*HEIGHT/tn and t.x > x*WIDTH/tn - WIDTH/tn and t.y > y*HEIGHT/tn - HEIGHT/tn then
if vec2(t.deltaX,t.deltaY):len() > 1.5 and filled[x][y] == 0 then
filled[x][y] = 1
elseif t.state == BEGAN and filled[x][y] == 1 then
filled[x][y] = 0
end
end
end
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(0)
-- Do your drawing here
for x = 1,tn do
for y = 1,tn do
if filled[x][y] == 0 then
m[x][y]:setColors(100,100,100,255)
elseif filled[x][y] == 1 then
if filled[x][y-1] == 0 and y > 1 then
filled[x][y] = 0
filled[x][y-1] = 1
end
m[x][y]:setColors(200,200,200,255)
end
m[x][y]:draw()
end
end
end