The same block puzzle β but every line of it is Python, running in your browser
β Back to Fun TimeThere 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.