๐Ÿš€ ASTEROIDS in Python

The same drifting ship โ€” but the trigonometry is Python

โ† Back to Fun Time
๐Ÿ Starting Pythonโ€ฆ
Score
0
Lives
3
Wave
1
Best
0

โŒจ๏ธ Controls

  • โ† โ†’ turn the ship  โ€ข  โ†‘ fire the engine
  • SPACE shoot โ€” up to four shots at once
  • Tapping the field shoots too
  • P pause  โ€ข  R new game
  • ๐ŸŽฎ Xbox controller? Just plug it in or pair it โ€” the stick and D-pad are the arrow keys, A is SPACE, Menu pauses and View starts again

๐ŸŽฏ How to play

  • Turning points the ship. Thrust pushes it โ€” and it keeps going.
  • To stop, turn all the way round and thrust against your own drift.
  • Fly off any edge and you come back on the other side.
  • The dashed ring means you cannot be hit for a moment. Use it to get clear.

๐Ÿ† Scoring

  • Big rock 20  โ€ข  medium 50  โ€ข  small 100.
  • Every rock you break makes two smaller, faster ones.
  • Clearing a whole wave is another 100.
  • Tip: break the big ones one at a time. Break them all at once and there will be sixteen little rocks after you.

๐Ÿง‘โ€๐Ÿ’ป This whole game is Python

There is almost no JavaScript on this page. The browser downloaded Pyodide โ€” a complete Python interpreter compiled to WebAssembly โ€” and then ran the .py files in funtime/pylib/. Python does the rules, the maths and the drawing.

File What it does Good functions to try re-writing
pylib/asteroids_rules.py The rules: angles, drifting, wrapping, breaking point_from, wrap_position, thrust_ship, hit_rocks
pylib/asteroids_draw.py Painting space โ€” Python calling the canvas draw_ship, draw_rock, render_game
pylib/asteroids_web.py The glue: the held keys, the taps, the loop do_action, on_key_down, frame

๐Ÿ” Compare the two versions. Open lib/asteroids-rules.js and pylib/asteroids_rules.py side by side โ€” same ideas, two languages.

๐Ÿ› ๏ธ Build it yourself: Build Asteroids in Python walks you through the functions one at a time.

โš ๏ธ If the game never starts: the page has to read its .py files, which a browser will not do for a page opened straight off your disk. Use the live site, or run python3 -m http.server in the project folder.