🐍 SNAKE in Python

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

← Back to Fun Time
🐍 Starting Python…
Score
0
Length
3
Level
1
Best
0

⌨️ Controls

  • ↑ ↓ ← β†’ steer the snake
  • W A S D work too
  • P or Space pause
  • R new game
  • Any arrow key starts a paused game.

🎯 How to play

  • The snake never stops moving β€” you only choose which way it turns.
  • Eat an apple and the snake grows one square longer.
  • Hit a wall or your own body and the game ends.
  • You cannot turn straight back on yourself β€” no U-turns!
  • Fill the entire board to win. (Nobody has ever done it. Probably.)

πŸ† Scoring

  • Each apple is worth 10 Γ— your level.
  • Every 5 apples you go up a level.
  • Each level makes the snake 15 ms faster, down to a very scary 70 ms per step.
  • So the long game is worth far more than the short one. πŸ€”

πŸ§‘β€πŸ’» 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/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.