Lesson goal: Tetris: Stacking Blocks & Line Clears

Previous: Asteroids: Velocity & Angles | Home | Next: Pac-Man

In 1984, Soviet software engineer Alexey Pajitnov created one of the most popular video games in history: Tetris.

Tetris is built entirely on simple geometry and coordinate arithmetic: falling shapes must be rotated and positioned on a 2D grid to form complete solid lines without gaps.

The Tetris Matrix & Coordinates

The playing board is a vertical grid called the matrix:
  • Columns ($x$): $0$ to $9$ (from left to right across $10$ columns). Notice the column numbers labeled along the bottom of the board!
  • Rows ($y$): $0$ to $19$ (from top to bottom across $20$ rows). Row $0$ is at the top where pieces appear, and Row $19$ is the floor at the bottom.


The 7 Geometric Tetrominoes

Every piece in Tetris is called a tetromino because it is made of exactly 4 connected squares (from the Greek prefix tetra- meaning four):
  • I-Piece (Cyan): A straight line of 4 blocks. Width = $4$ when horizontal, width = $1$ when vertical.
  • O-Piece (Yellow): A $2 \times 2$ square block. Width = $2$, height = $2$.
  • T-Piece (Purple): A 3-block row with one center block projecting out.
  • L-Piece (Orange) & J-Piece (Blue): 3-block rows with one corner block.
  • S-Piece (Green) & Z-Piece (Red): 2-step zig-zag shapes.


Controlling the Game with Code

Instead of pressing keyboard buttons in real time, you write code that commands how each piece moves and drops:
  • tetris_game(n) sets up the interactive 10x20 matrix for Game #$n$ (default is $n = 1$). Each number $n$ provides a deterministic set of pieces unique to that game!
  • left(n): Shifts the current falling piece left by $n$ columns.
  • right(n): Shifts the current falling piece right by $n$ columns.
  • rotate(n) (or rot(n)): Spins the piece clockwise by 90° ($n$ times).
  • drop(speed): Animates the piece dropping down to the floor at the chosen fall rate (optional: e.g. drop(2) is 2× speed, drop(0.5) is slow-motion, or omit for smooth default falling animation). Once it lands, it locks into the grid, checks for line clears, and spawns the next piece!
  • place(col, rot): A helpful shortcut that rotates and moves the piece directly to column col before dropping.


The Math of Line Clears: $4 + 4 + 2 = 10$

The bottom row is $10$ columns wide. If you fill all $10$ squares in a row:
  • Row Clears: That completed horizontal row flashes and vanishes!
  • Gravity: All blocks above that row drop down by one row.
  • Scoring: 1 line = $100$ pts, 2 lines = $300$ pts, 3 lines = $500$ pts, 4 lines (a TETRIS!) = $800$ pts.
Notice how the math works: if you place an I-piece ($4$ blocks wide) at columns $0$ to $3$, another I-piece ($4$ blocks wide) at columns $4$ to $7$, and an O-piece ($2$ blocks wide) at columns $8$ to $9$: $$4 + 4 + 2 = 10 \text{ filled columns!}$$ The entire bottom row is completely full and clears automatically!
tetris_game()
left(3)

drop()

right(1)

drop()
Move the mouse over a dotted box for more information.

  • Ghost Guide: The faint translucent outline at the bottom shows you exactly where your piece will land before you drop it.
  • Interactive Playback: Click Run Code to watch your plan animate. Use Play/Pause, Step (⏭), and Reset (↺) to analyze each move piece by piece.
  • Next Piece Preview: Check the Next Piece box on the right sidebar to see which shape is coming next!

Now you try. Can you use rotate(1) to turn an I-piece vertically, or use place(col, rot) to place pieces directly by column number?

Type your code here:


See your results here: