The same puzzle โ but every slide and merge is Python
โ Back to Fun TimeThere 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/twenty48_rules.py | The rules: sliding, joining, turning | slide_row, merge_row, rotate_board, move_board |
| pylib/twenty48_draw.py | Painting the tiles โ Python calling the canvas | tile_look, draw_tile, render_game |
| pylib/twenty48_web.py | The glue: the arrow keys and finger swipes | do_action, on_touch_end, start_game |
๐ Compare the two versions. Open lib/twenty48-rules.js and pylib/twenty48_rules.py side by side โ same ideas, two languages.
๐ ๏ธ Build it yourself: Build 2048 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.