๐Ÿ“ฆ SOKOBAN in Python

The same warehouse โ€” but every push is worked out in Python

โ† Back to Fun Time
๐Ÿ Starting Pythonโ€ฆ
Level
1 / 6
Moves
0
Boxes home
0 / 1
Solved
0

โŒจ๏ธ Controls

  • โ†‘ โ†“ โ† โ†’ or W A S D walk
  • Tapping the board walks one step towards where you tapped
  • U, Z or BACKSPACE undo  โ€ข  R start the level again
  • N or ENTER next level  โ€ข  P pause
  • ๐ŸŽฎ 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

  • Push every crate onto a ring. A crate standing on a ring is filled in solid.
  • You can only push. There is no way to pull a crate back.
  • Push a crate into a corner and it is stuck there for good โ€” undo is your friend.
  • Six levels, getting harder. The last one needs sixteen moves.

๐Ÿง  Thinking ahead

  • Look at where each crate has to end up before you touch it.
  • A crate against a wall can only slide along that wall โ€” never away from it.
  • Work out which side of a crate you need to stand on, and go there first.
  • Stuck? U as many times as you like. It costs nothing.

๐Ÿง‘โ€๐Ÿ’ป 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/sokoban_rules.py The rules: the levels, the pushing, the undo is_wall, box_at, move_player, undo_move
pylib/sokoban_draw.py Painting the warehouse โ€” Python calling the canvas draw_wall, draw_box, render_game
pylib/sokoban_web.py The glue: the keyboard, the taps, the buttons do_action, on_board_click, start_game

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

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