๐Ÿ’ก LIGHTS OUT in Python

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

โ† Back to Fun Time
๐Ÿ Starting Pythonโ€ฆ
Presses
0
Still lit
0
Best
โ€”

โŒจ๏ธ Controls

  • โ† โ†’ โ†‘ โ†“ move the dashed cursor
  • Space or Enter press the square
  • Clicking or tapping a square works too
  • P pause  โ€ข  R new puzzle
  • ๐ŸŽฎ 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

  • Pressing a square flips it and its four neighbours โ€” a plus shape.
  • At the edges the cross is smaller, because some neighbours do not exist.
  • Turn every light off to win.
  • Every puzzle can be solved โ€” the game builds them by pressing, so they can always be un-pressed.

๐Ÿ’ก Two tricks

  • Pressing the same square twice puts everything back. Every move is its own undo.
  • So the order you press in does not matter at all โ€” only which squares you press.
  • That means every puzzle has a shortest answer, and no square ever needs pressing twice.
  • Try solving the top row first, then work down. It really helps.

๐Ÿง‘โ€๐Ÿ’ป 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/lights_rules.py The rules: the cross, the flips, the win light_index, neighbours, press_light, create_puzzle
pylib/lights_draw.py Painting the lights โ€” Python calling the canvas draw_light, square_at_pixel, render_game
pylib/lights_web.py The glue: clicks, keyboard, the scoreboard do_action, on_board_click, start_game

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

๐Ÿ› ๏ธ Build it yourself: Build Lights Out 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.