🏎️ CAR RACING in Python

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

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

⌨️ Controls

  • ← β†’ steer between the lanes
  • ↑ accelerate   ↓ brake
  • W A S D work too
  • P or Space pause
  • R new race
  • Hold the keys β€” steering is not a tap!

🎯 How to play

  • Your car is the one with the racing stripe, at the bottom.
  • The road never stops. Steer between the three lanes to miss the traffic.
  • Every car you overtake scores points β€” and touching one ends the race.
  • Accelerating covers more road (and scores faster), but leaves you less time to react.
  • Every 5 cars passed is a new level, and every level is quicker.

πŸ† Scoring

  • Each car overtaken is worth 10 Γ— your level.
  • Metres counts how far you have driven β€” 10 pixels of road is 1 metre.
  • Level 1 rolls along at 180 pixels a second; level 11 screams past at 520.
  • However fast it gets, there is always 0.8 seconds between cars β€” just enough to swerve. πŸ˜…

πŸ§‘β€πŸ’» 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/race_rules.py The rules: steering, traffic, crashing, scoring clamp, overlaps, move_cars, update_race
pylib/race_draw.py Painting the picture β€” Python calling the canvas draw_dashed_line, draw_road, draw_car
pylib/race_web.py The glue: held keys, buttons, the 60-times-a-second loop set_held, frame, connect_hold_button

πŸ” Compare the two versions. Open lib/race-game.js and pylib/race_rules.py side by side. The ideas are identical, but Python is shorter in places: clamp becomes one line with min and max, and any(...) finds a crash without writing a loop.

πŸ› οΈ Want to build it yourself in Python? Build Car Racing in Python walks you through the twelve functions, tests each one with real Python, and shows the race 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.