← back to projects

Tetris

Python Pygame early project

One of my first projects while learning to code: Tetris in Python with Pygame. Falling blocks you can move and rotate, full lines that clear with a little animation, a next-piece preview and a score.

My role: a solo project.

Play it: browser port of my Pygame code playable

← → move  ·  ↑ rotate  ·  ↓ drop faster

Same rules and numbers as my Python code: field size, fall speed (300 ms, or 15 ms while dropping), rotation, scoring and the floating line-clear effect. The blocks are drawn in code using my sprites' colours, and the title uses a free font, because the original font may not be redistributed.

How it works

The board is a grid. A 10 × 20 list of lists remembers which cells are filled. A move is only allowed if every block of the piece would land inside the walls on an empty cell.

Rotation. Every block is turned 90° around the piece's first block. If that would hit something, the rotation is simply skipped.

Clearing lines. Going up from the bottom, each row is copied down; a full row is skipped instead, so everything above it drops. More lines at once score much more: 100, 300, 700 or 1500 points.

Two versions

The first version (May 2025) was the whole game in one file. The current one, played above, is split into modules for settings, pieces, the board and the main loop, which made it much easier to add the next-piece preview, sprites and effects.

What I'd do differently now

A hidden bug. If a tall piece lands while part of it is still above the board, its row number is -1, and in Python list[-1] means the last row, so a block silently appears at the bottom. The browser version treats that as game over instead.

No wall kicks. Rotating next to a wall just fails, and the square piece wobbles when rotated because it turns around a corner block.

It never gets harder. The speed stays at 300 ms per row, and game over restarts instantly without showing your score.