Coders Packet

2048 Game in Python

By Aritra Banerjee

A GUI based implementation of the well known game 2048, in Python. Tkinter module is used for graphics.

2048 Game in Python

As shown in the grid above, we get numbers in powers of 2. The goal of the game is to get 2048 in one of the cells.

We can rearrange the numbers by moving all the numbers to any one of the 4 edges. While moving, empty cells in the direction of movement are ignored. The game automatically generates a '2' at a random empty cell of the grid after each turn of the player. This version of the game also allows the user to go back one step in the game.

Two adjacent cells (in any one of the 4 directions) can be combined (added) if they contain the same numbers. We can do this by moving the grid in the direction we want to combine. As in the image shown above, we can combine any two of the three 2's in the second column to get a 4 (move up or down). This creates an empty cell in the direction opposite to that of the combination.

The player loses the game if all the cells of the grid are filled with no chance of combining two adjacent cells. The score is the highest number in the grid.

This packet uses the Tkinter module. To download the module (via Ubuntu Terminal):

sudo apt install python3.6-tk (change the version of python depending on the version supported by the system)

Instructions for using the packet:

1) Unzip the packet and go into the '2048 Game' folder

2) To run the game:

     python3 (for Python2, use python2

3) Controls (not in uppercase) appear when the game is running in the terminal as well:

     W/J - move grid UP                                        A/H - move grid LEFT
     S/K - move grid DOWN                                  D/L - move grid RIGHT
     B - retrace step

     For retracing after any of the above four moves, the key has to be pressed twice to implement the change. For contiguous retracing, the key has to be pressed only once.

Download Complete Code


No comments yet