The same game β 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/snake_rules.py | The rules: crawling, eating, growing, dying | same_position, add_direction, move_snake, step_game |
| pylib/snake_draw.py | Painting the picture β Python calling the canvas | draw_segment, draw_head, draw_food |
| pylib/snake_web.py | The glue: keyboard, buttons, the 60-times-a-second loop | do_action, frame, start_game |
π Compare the two versions. Open the JavaScript rules and pylib/snake_rules.py side by side. The ideas are identical β but Python says some of them in far fewer words: position in items replaces a whole loop, and a == b compares two squares that JavaScript had to compare number by number.
π οΈ Want to build it yourself in Python? Build Snake 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.