What you build
One circuit carries the whole course. Every lesson opens on the same wiring and changes only the sketch:
- The field: two MAX7219 8x8 matrices, stood one on the other for a field 8 wide and 16 tall. The top one is on pins 12, 11 and 10 (DIN, CS, CLK), the bottom one on 9, 8 and 7.
- The controls: a thumb joystick. Left and right move, up turns, down falls faster, and pressing it in drops the piece. Its press is on A0, up and down on A1, left and right on A2.
- The sound: an active buzzer on pin 3. It has its own oscillator, so the sketch only switches it on and off.
- The brain: an Arduino Uno.
How it works
The field is sixteen bytes. One byte per row, one bit per LED, bit 7 on the left. A full row is 255 (0xFF), so finding completed lines is one comparison per row. Row y of the field is row y % 8 of matrix y / 8, and changing a row means sending its chip 16 bits: the register number, then the byte.
Seven pieces. Four squares joined edge to edge make exactly seven shapes, counting mirror images as different. The course calls them Stick, Block, Tee, Zig, Zag, Hook and Boot. Each is four cells in a small box: 4 by 4 for the Stick, 2 by 2 for the Block, 3 by 3 for the rest.
One test for everything. fits() checks a piece's four cells against the walls, the floor and the blocks already there. Falling, moving and turning are all the same question: does it fit one step over?
A quarter turn maps each cell (x, y) to (n - 1 - y, x) inside its box. No angles and no trigonometry. When a wall is in the way, the turned piece is tried one column right, one left, then two each way: a wall kick.
Line clears and score. Every full row is removed by moving everything above it down one, and the same row is checked again in case another full row fell into it. A clear scores lines x lines x 100 x level:
| Rows at once | Points on level 1 |
|---|---|
| 1 | 100 |
| 2 | 400 |
| 3 | 900 |
| 4 | 1600 |
Levels. Every 4 lines is a level, up to 10, and each is 15 percent faster: from 800 ms a row on level 1 down to 185 ms on level 10. The timing uses millis() and never delay(), so the joystick is read all the time.
Fair dealing. Pieces come in shuffled bags of seven (a Fisher-Yates shuffle seeded from the microsecond you press start), so no shape is ever more than twelve pieces away.
The course
The course takes six lessons:
- The board grid: sixteen bytes on two matrices, and drawing with the joystick.
- Pieces: the seven shapes, falling, steering and landing.
- Rotation: the quarter turn, the piece that never changes, and wall kicks.
- Line clears: two puzzles, full rows, and scoring the hard ones.
- Speed levels: faster every 4 lines, soft drop, timing without delay(), and game over.
- Project: a pocket falling-blocks game: a title screen, the bag, a lock click, pause and a best score.
Every step but the last is checked against the running circuit.