Falling blocks
For creatorsAdd-on 2.2.2+
In this tutorial you build a falling-blocks puzzle: pieces drop onto a board, you slide and turn them, and full rows clear. The board is a grid, a value held in one variable that pieces are tested against, written into and cleared from. The same grid nodes serve mazes, dungeons and board games later. This builds on tutorial 3, The trail.
▶ Play the finished game: ← → slide (hold to keep sliding), ↑ rotates, ↓ drops one row, Space drops all the way, Enter plays again. On a phone, use the pad under the board.
1. Learn the grid nodes
Section titled “1. Learn the grid nodes”A grid is { w, h, cells }: the cells row by row, row 0 at the top, 0 meaning empty. It lives in an ordinary graph variable of type json, so it saves and replays like a number. The grid nodes never change a grid in place; each one returns a new grid that you store with Set Variable.
| Node | What it gives |
|---|---|
| Grid | A new grid of w × h cells, every cell set to fill |
| Grid Cell | The value at (x, y), or −1 outside the grid, plus the width and height |
| Grid Set Cell | A copy with one cell set |
| Grid Fits | Whether a shape, a list of cells [[dx, dy], …], fits at (x, y): every cell inside and empty. openTop lets cells stand above the top row |
| Grid Stamp | A copy with a shape written in at (x, y) |
| Grid Clear Full Rows | A copy with every full row removed and the rows above dropped down, plus how many rows were cleared |
| Array Get | One element of a list, and the list’s length. wrap counts around |
| Integer | Floor, round, ceil, whole-number divide, and a mod that is never negative |
2. Put the pieces in data
Section titled “2. Put the pieces in data”-
Save a new file as
falling-blocks.blend. -
In the Text Editor, make
data/pieces.jsonwith one row per piece: itsname, itscolor(a number from 1 to 7) androts, its four rotations as lists of cells. -
Make three more tables:
data/kicks.json(the sideways nudges a blocked rotation tries, in a fielddx: 0, −1, +1, −2, +2),data/scoring.json(points by lines cleared at once: 0, 40, 100, 300, 1200) anddata/speeds.json(seconds per row by level, from 0.8 at level 0 down to 0.1 at level 9).
Changing how the game feels means editing these tables, not the graph.
3. Draw the board with a component
Section titled “3. Draw the board with a component”The rules never touch geometry. They compute view (the board with the falling piece drawn on it) and nextView (the next piece on a 4 × 4 grid), and two objects draw them.
-
Add an Empty named
Boardand give it Add Component ▸ Arcade ▸ Grid View (or drag Grid View from the Simulacrum asset library). -
Set
grid.vartoview,grid.cellto0.3,grid.colorsto seven colours (value 1 takes the first) andgrid.frameto#1b2030for a backing panel. A grid stands upright by default (grid.plane = xy), row 0 at the top. -
Add a second grid view,
NextPiece, beside it withgrid.varset tonextViewandgrid.cellset to0.24. -
Point an orthographic camera straight at the board:
camera.mode=fixed,camera.projection=ortho,camera.at=0.35,-0.35,10,camera.target=0.35,-0.35,0,camera.orthoHeight=3.9,camera.orthoMinWidth=4.8(Properties ▸ Scene ▸ Custom Properties).
The grid view draws a block for every non-empty cell and redraws whenever the graph stores a new grid.
4. Add the controls and the pad
Section titled “4. Add the controls and the pad”-
In World ▸ Simulacrum ▸ Input, add the actions with Add Action:
left=keys:ArrowLeft/KeyA ; repeat:170,50,right=keys:ArrowRight/KeyD ; repeat:170,50,rotate=keys:ArrowUp/KeyW/KeyX,down=keys:ArrowDown/KeyS ; repeat:60,50,drop=keys:Space,again=keys:Enter/KeyR. -
Add an Empty with Add Component ▸ HUD & Menus ▸ HUD Choice Menu (or drag HUD Pad from the asset library). Set
hud.layouttopad,hud.whentoplaying, andhud.menutoleft=◀|rotate=⟳|right=▶|down=▼|drop=⤓. -
Add a HUD Text line for
Score {score} · Lines {lines} · Level {level}at the top, and a HUD Choice Menu withagain=Play againshown whileover.
repeat:170,50 makes a held button press again after 170 ms, then every 50 ms. A pad menu stands its buttons in a row and holds them like keys while a finger is down, so the same repeat works under a thumb.
5. Write the rules
Section titled “5. Write the rules”Make a Scene-scope graph named blocks.rules and set it as the scene’s Rules. Declare board, view and nextView as json variables, the numbers piece, next, rot, px (3), py, fall, score, lines, level and cleared, the flags playing and over, and best (Persistent, Player).
-
The piece. A Data Table on
data/pieces.json, fieldrots, rowpiece, into an Array Get withrotas its index andwrapon, gives the piece’s cells. Itscoloris the value it writes. -
Gravity. On Tick every 0.05 s adds to
fall. Whenfallreaches the level’ssecondsfrom the speeds table (the last row for every level after it), emitgraph:step. Ongraph:step, the piece moves down one row if Grid Fits allows it; otherwise emitgraph:lock. -
Lock. Grid Stamp writes the piece into
board, then Grid Clear Full Rows. Store the cleared count before you replace the board, because each step re-reads its inputs. Then addpoints × (level + 1)toscore, add the cleared rows tolines, setleveltolinesdivided by 10 (whole number, with Integer), and emitgraph:spawn. -
Spawn. Take
nextas the newpiece, roll a newnextwith Random Range and Integer (floor, 0 to 6), setrotto 0 andpxto column 3. If the new piece does not fit, the game is over and the best score is kept. -
The player. On Input, only while
playing: left and right move the piece if it fits there. Rotate takes the next rotation (Integer mod 4) and tries each kick in turn with a While loop, keeping the first that fits. The soft drop is one step. The hard drop loops down while the piece fits, then locks.againstarts over, but only after a game ends. -
Draw. After every change, emit
graph:draw: stamp the piece onto a copy ofboardand store it inview.
Messages on graph: channels stay inside this graph and run at once, inside the same step, so you can name the parts of the rules (graph:step, graph:lock, graph:spawn, graph:draw) and call them from anywhere.
A new deal every game. The Random Range node’s seed is mixed with a seed the viewer picks fresh on every page load, so no two games deal alike. Add &seed= and a number to the play link to replay one exact deal.
6. Build and play
Section titled “6. Build and play”-
Press Build Cartridge. The four tables go into the cartridge.
-
Publish it (Publish and share), open it and hold ←: the piece slides until it meets the wall. Fill a row and it clears.
What you learned
Section titled “What you learned”- A whole board can be one value in a variable, changed only by nodes that return a new copy.
- The Grid View component draws a grid variable and keeps it up to date.
repeat:and a pad menu make held controls work the same on keys and under a finger.
Next: The dungeon generates levels from a seed and draws them with a tile set made in Blender.