๐Ÿ”ข 2048 in Python

The same puzzle โ€” but every slide and merge is Python

โ† Back to Fun Time
๐Ÿ Starting Pythonโ€ฆ
Score
0
Biggest
2
Moves
0
Best
0

โŒจ๏ธ Controls

  • โ†‘ โ†“ โ† โ†’ or W A S D slide everything
  • On a phone: swipe the board
  • SPACE keep playing after you reach 2048
  • 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

  • Every slide moves all the tiles as far as they can go.
  • Two tiles with the same number join into one worth double.
  • A new tile appears after every move that changed something.
  • A tile can only join once per move โ€” four 2s become two 4s, not one 8.

๐Ÿ† Getting good

  • Pick a corner and keep your biggest tile there. Never move it out.
  • Use three directions and avoid the fourth โ€” that keeps the big tile put.
  • Build a row in order along one edge: 2, 4, 8, 16โ€ฆ
  • The game is over only when there are no gaps and no equal neighbours.

๐Ÿง‘โ€๐Ÿ’ป 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/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.