β–© TETRIS in Python

The same block puzzle β€” but every line of it is Python, running in your browser

← Back to Fun Time
🐍 Starting Python…
Next
Score
0
Lines
0
Level
1

⌨️ Controls

  • ← β†’ move left and right
  • ↑ rotate the piece
  • Z rotate the other way
  • ↓ soft drop (1 point a row)
  • Space hard drop (2 points a row)
  • P pause  β€’  R new game

🎯 How to play

  • Pieces fall from the top. Turn and slide them so they fit together.
  • Fill a whole row across and it vanishes β€” everything above drops down.
  • The grey outline shows where the piece will land.
  • Every 10 lines you go up a level and the pieces fall faster.
  • If a new piece cannot fit at the top, the game is over.

πŸ† Scoring

  • 1 row = 100 Γ— level
  • 2 rows = 300 Γ— level
  • 3 rows = 500 Γ— level
  • 4 rows at once = 800 Γ— level β€” a real "Tetris"!
  • Clearing four rows in one go is worth more than four single rows. πŸ€”

πŸ§‘β€πŸ’» This whole game is Python

There is almost no JavaScript on this page. When you opened it, the browser downloaded Pyodide β€” a complete Python interpreter compiled to WebAssembly β€” and then ran three ordinary .py files from the funtime/pylib/ folder. Python is doing everything: the rules, the maths, and the drawing.

File What it does Functions to try re-writing
pylib/tetris_rules.py The rules: falling, rotating, clearing, scoring rotate_matrix_clockwise, is_row_full, remove_rows, can_place_piece
pylib/tetris_draw.py Painting the picture β€” Python calling the canvas draw_block, render_game, render_next_piece
pylib/tetris_web.py The glue: keyboard, buttons, the 60-times-a-second loop do_action, update_held_key, frame

πŸ” Compare the two versions. Open lib/tetris-board.js and pylib/tetris_rules.py side by side. The ideas are identical, but Python is shorter in places: all(value == 1 for value in row) replaces a whole loop, and a list comprehension rotates the whole grid in one line.

πŸ› οΈ Want to build it yourself in Python? Build Tetris in Python walks you through the twelve functions, tests each one with real Python, and shows the game growing as you go.

⚠️ If the game never starts: this page has to read its .py files, and a browser will not do that for a page opened straight off your disk. Open it from the live site, or run python3 -m http.server in the project folder and visit the address it prints.