2048 Game in Python

Share your love

Let’s create a step-by-step guide for building the classic 2048 game in Python. 2048 is a popular puzzle game where you slide numbered tiles on a grid to combine them and create a tile with the number 2048. We’ll implement a simplified version of this game without a graphical user interface (GUI). You can later enhance it by adding a GUI using libraries like Tkinter or Pygame.

1. Game Rules:

  • The game is played on a 4×4 grid.
  • Initially, two random cells are filled with the number 2.
  • You can move the tiles in four directions: up, down, left, or right.
  • When you move the tiles, identical numbers merge into one, and the extreme cell in that direction gets filled with the sum.
  • After each move, a new random empty cell is filled with the number 2.
  • The goal is to create a tile with the number 2048.

2. Logic Overview:

  • We’ll represent the game board as a 4×4 matrix (a list with four rows and four columns).
  • We’ll design functions for each logic (e.g., left swipe, right swipe, up swipe, down swipe).
  • The game will continue until there are no empty cells left or the player reaches 2048.

3. Implementation Steps:

a. Initialize the Game Board:

Python

def start_game():
    mat = [[0] * 4 for _ in range(4)]
    return mat

AI-generated code. Review and use carefully. More info on FAQ.

b. Add a Random 2 or 4 to an Empty Cell:

Python

def add_new(mat):
    empty_cells = [(i, j) for i in range(4) for j in range(4) if mat[i][j] == 0]
    if empty_cells:
        i, j = random.choice(empty_cells)
        mat[i][j] = random.choice([2, 4])

AI-generated code. Review and use carefully. More info on FAQ.

c. Swipe Left:

Python

def swipe_left(mat):
    for row in mat:
        temp = [x for x in row if x != 0]
        temp += [0] * (4 - len(temp))
        for i in range(3):
            if temp[i] == temp[i + 1]:
                temp[i] *= 2
                temp[i + 1] = 0
        temp = [x for x in temp if x != 0]
        temp += [0] * (4 - len(temp))
        row[:] = temp

AI-generated code. Review and use carefully. More info on FAQ.

d. Swipe Right:

Python

def swipe_right(mat):
    for row in mat:
        temp = [x for x in row if x != 0]
        temp = [0] * (4 - len(temp)) + temp
        for i in range(3, 0, -1):
            if temp[i] == temp[i - 1]:
                temp[i] *= 2
                temp[i - 1] = 0
        temp = [x for x in temp if x != 0]
        temp = [0] * (4 - len(temp)) + temp
        row[:] = temp

AI-generated code. Review and use carefully. More info on FAQ.

e. Swipe Up:

Python

def swipe_up(mat):
    for j in range(4):
        temp = [mat[i][j] for i in range(4) if mat[i][j] != 0]
        temp += [0] * (4 - len(temp))
        for i in range(3):
            if temp[i] == temp[i + 1]:
                temp[i] *= 2
                temp[i + 1] = 0
        temp = [x for x in temp if x != 0]
        temp += [0] * (4 - len(temp))
        for i in range(4):
            mat[i][j] = temp[i]

AI-generated code. Review and use carefully. More info on FAQ.

f. Swipe Down:

Python

def swipe_down(mat):
    for j in range(4):
        temp = [mat[i][j] for i in range(4) if mat[i][j] != 0]
        temp = [0] * (4 - len(temp)) + temp
        for i in range(3, 0, -1):
            if temp[i] == temp[i - 1]:
                temp[i] *= 2
                temp[i - 1] = 0
        temp = [x for x in temp if x != 0]
        temp = [0] * (4 - len(temp)) + temp
        for i in range(4):
            mat[i][j] = temp[i]

AI-generated code. Review and use carefully. More info on FAQ.

4. Play the Game:

  • Combine the logic functions to create the game loop.
  • Display the
Share your love