Skip to content

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.

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
  1. Save a new file as falling-blocks.blend.

  2. In the Text Editor, make data/pieces.json with one row per piece: its name, its color (a number from 1 to 7) and rots, its four rotations as lists of cells.

  3. Make three more tables: data/kicks.json (the sideways nudges a blocked rotation tries, in a field dx: 0, −1, +1, −2, +2), data/scoring.json (points by lines cleared at once: 0, 40, 100, 300, 1200) and data/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.

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.

  1. Add an Empty named Board and give it Add Component ▸ Arcade ▸ Grid View (or drag Grid View from the Simulacrum asset library).

  2. Set grid.var to view, grid.cell to 0.3, grid.colors to seven colours (value 1 takes the first) and grid.frame to #1b2030 for a backing panel. A grid stands upright by default (grid.plane = xy), row 0 at the top.

  3. Add a second grid view, NextPiece, beside it with grid.var set to nextView and grid.cell set to 0.24.

  4. 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.

  1. 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.

  2. Add an Empty with Add Component ▸ HUD & Menus ▸ HUD Choice Menu (or drag HUD Pad from the asset library). Set hud.layout to pad, hud.when to playing, and hud.menu to left=◀|rotate=⟳|right=▶|down=▼|drop=⤓.

  3. Add a HUD Text line for Score {score} · Lines {lines} · Level {level} at the top, and a HUD Choice Menu with again=Play again shown while over.

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.

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

  1. The piece. A Data Table on data/pieces.json, field rots, row piece, into an Array Get with rot as its index and wrap on, gives the piece’s cells. Its color is the value it writes.

  2. Gravity. On Tick every 0.05 s adds to fall. When fall reaches the level’s seconds from the speeds table (the last row for every level after it), emit graph:step. On graph:step, the piece moves down one row if Grid Fits allows it; otherwise emit graph:lock.

  3. 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 add points × (level + 1) to score, add the cleared rows to lines, set level to lines divided by 10 (whole number, with Integer), and emit graph:spawn.

  4. Spawn. Take next as the new piece, roll a new next with Random Range and Integer (floor, 0 to 6), set rot to 0 and px to column 3. If the new piece does not fit, the game is over and the best score is kept.

  5. 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. again starts over, but only after a game ends.

  6. Draw. After every change, emit graph:draw: stamp the piece onto a copy of board and store it in view.

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.

  1. Press Build Cartridge. The four tables go into the cartridge.

  2. Publish it (Publish and share), open it and hold ←: the piece slides until it meets the wall. Fill a row and it clears.

  • 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.