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.
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.
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:
Share your code
Show a friend, family member, or teacher what you've done!
Here is a share link to your code:
Need a hint?
💡 What now?
The code has ???? for the 3rd piece's movement. Replace ???? with 4 (so the 2-block square slides right to columns 8 and 9), then click Run Code!
1. Observe how Piece 1 lands on columns 0-3 and Piece 2 lands on columns 4-7.
2. With right(4), Piece 3 fills columns 8-9, completing all 10 squares on row 19 ($4 + 4 + 2 = 10$).
3. Watch row 19 flash and clear, earning your first 100 points!
4. Click Step (⏭) or Reset (↺) to replay and inspect the moves step-by-step.