๐Ÿ”จ WHACK-A-MOLE in Python

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

โ† Back to Fun Time
๐Ÿ Starting Pythonโ€ฆ
Score
0
Hits
0
Time
45
Level
1
Best
0

โŒจ๏ธ Controls

  • โ† โ†’ โ†‘ โ†“ aim the hammer
  • Space or Enter swing
  • Clicking or tapping a hole works too โ€” much faster!
  • 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

  • A mole pops out of one hole at a time. Hit it before it ducks back down.
  • You have 45 seconds.
  • Every 5 hits you go up a level โ€” and the moles get quicker.
  • A miss does not move the mole, so hitting every hole in turn will not help you.

๐Ÿ† Scoring

  • Each mole is worth 10 ร— your level.
  • Level 1 moles wait 1100 ms; level 10 moles wait only 350.
  • So the points really pile up once you get going โ€” the last ten seconds are worth more than the first thirty.

๐Ÿง‘โ€๐Ÿ’ป 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/whack_rules.py The rules: the mole, the clock, the scoring random_hole, mole_interval, whack, update_game
pylib/whack_draw.py Painting the mole โ€” Python calling the canvas draw_hole, draw_mole, hole_at_pixel
pylib/whack_web.py The glue: clicks, keyboard, the loop do_action, on_board_click, frame

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

๐Ÿ› ๏ธ Build it yourself: Build Whack-a-Mole 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.