# Falling sand game start-off

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)
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)
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
``````

``````-- cells

-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
p = {}
filled = {}
tn = 30
time = 0
typ = {}
brush = 1
m = {}
Orect = rect
rect = {}
for x = 1,tn do
p[x] = {}
typ[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)
typ[x][y] = 0
end
end
end

function touched(t)
tpos = vec2(t.x,t.y)
if t.state == BEGAN and tpos:dist(vec2(100,HEIGHT-100)) < 75 then
brush = 1
elseif t.state == BEGAN and tpos:dist(vec2(200,HEIGHT-100)) < 75 then
brush = 2
elseif t.state == BEGAN and tpos:dist(vec2(300,HEIGHT-100)) < 75 then
brush = 3
elseif tpos:dist(vec2(150,HEIGHT-100)) > 125 then
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
typ[x][y] = brush
filled[x][y] = 1
end
if brush == 3 then
filled[x][y] = 0
end
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)
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
local dir = math.random(-1,1)
if x+dir == 0 then dir = 1 elseif x+dir == 31 then dir = -1 end
if typ[x][y] == 2 and filled[x+dir][y] == 0 and filled[x][y-1] == 1 then
filled[x][y] = 0
typ[x][y] = 0
filled[x+dir][y] = 1
typ[x+dir][y] = 2
end
if filled[x][y-1] == 0 and y > 1 then
filled[x][y] = 0
filled[x][y-1] = 1
typ[x][y-1] = typ[x][y]
--typ[x][y+1] = 0
end
if typ[x][y] == 1 then
m[x][y]:setColors(200,200,200,255)
elseif typ[x][y] == 2 then
m[x][y]:setColors(0,0,200,255)
end
end
m[x][y]:draw()
end
end

fill(200,200,200,200)
ellipse(100,HEIGHT-100,70)
fill(255, 255, 255, 255)
text("Stone",100,HEIGHT-50)

fill(0,0,200,200)
ellipse(200,HEIGHT-100,70)
fill(255, 255, 255, 255)
text("Water",200,HEIGHT-50)

fill(255,255,255,200)
ellipse(300,HEIGHT-100,70)
fill(255,255,255,255)
text("Eraser",300,HEIGHT-50)
end
``````

Very cool, thanks for posting that up. I like how the water moves.