Building a Simple 2048 Game in Python with Tkinter

Building a Simple 2048 Game in Python with Tkinter

Introduction

In this blog post, we’ll walk you through the process of building a simplified version of the popular 2048 game, where the objective is to combine numbered tiles on a grid to reach the elusive tile with the number 2048. Don’t worry if you’re new to programming or game development – we’ll break down each step in an easy-to-understand manner.

In our Python implementation of 2048, we’ll be using Tkinter, a built-in Python library for creating graphical user interfaces (GUIs). Tkinter provides us with tools to design the game window, handle user input, and update the game grid dynamically.

Throughout this tutorial, we’ll cover:

  1. Setting up the game window and grid layout using Tkinter.
  2. Managing the game state, including the grid and score.
  3. Implementing tile movement and merging logic for player interactions.
  4. Adding random tiles to the grid after each move.
  5. Checking for game over conditions to end the game.

Required Module to Run Above Code

In the provided code for the 2048 game using Python and Tkinter, there is one external module being used:

  1. tkinter (import tkinter as tk):
    • tkinter is the standard GUI (Graphical User Interface) toolkit for Python.
    • It provides various widgets (such as buttons, labels, entry fields) and methods for creating graphical applications.
    • In the code, tkinter is used to create the game window, grid cells, and handle user input events (such as key presses).
    • This module comes bundled with Python, so there’s no need to install it separately.

Apart from tkinter, the code also uses the built-in random module, which is a part of the Python standard library and does not require any additional installation.

Here’s a brief summary of the modules used in the code:

  • tkinter: Used for creating the GUI components and handling user input.
  • random: Used for generating random numbers for adding new tiles in the game.

How to run below code

To run the provided Python code in Visual Studio Code (VS Code), you can follow these steps:

  1. Install Python: Ensure you have Python installed on your system. You can download and install Python from the official website: Python Downloads.

  2. Install Visual Studio Code: If you haven’t already, download and install Visual Studio Code from the official website: Visual Studio Code.

  3. Open VS Code:

    • Launch Visual Studio Code.
  4. Create a New Python File:

    • Click on the “Explorer” icon on the sidebar (or go to View > Explorer).
    • Click on the “New File” button or press Ctrl + N to create a new file.
    • Save the file with a .py extension, for example, 2048_game.py.
  5. Copy the Code:

    • Copy the provided Python code (2048 game code) and paste it into the newly created Python file in Visual Studio Code.
  6. Run the Code:

    • Open the terminal in VS Code by selecting Terminal > New Terminal from the menu.
    • In the terminal, navigate to the directory where your Python file is saved using the cd command.
    • Once you’re in the correct directory, type python 2048_game.py (assuming you named your file 2048_game.py) and press Enter to run the code.
  7. Play the Game:

    • After running the code, the game window should open, and you can play the 2048 game using the arrow keys.

Code Explanation

Let’s break down the code step by step:

  1. Imports:

    • import tkinter as tk: Imports the Tkinter library, which is used for creating the graphical user interface (GUI) for the game.
    • import random: Imports the random module, which is used for generating random numbers for adding new tiles.
  2. Class Definition (Game2048):

    • This class represents the 2048 game. It contains methods and attributes to manage the game state and handle user input.
  3. __init__ method:

    • Initializes the game instance.
    • Sets up the game window (master), title, size, and event binding for key presses.
    • Initializes game variables such as the grid size, grid state, and score.
    • Calls init_grid() to set up the initial grid GUI.
    • Calls add_tile() to add the first tile to the grid.
    • Calls update_grid() to update the GUI with the initial grid state.
  4. init_grid method:

    • Initializes the GUI grid by creating Tkinter labels for each cell and placing them in a grid layout within the game window.
  5. add_tile method:

    • Adds a new tile (either 2 or 4 with probabilities 0.9 and 0.1, respectively) to a random empty cell on the grid.
  6. update_grid method:

    • Updates the GUI grid labels with the current state of the grid.
  7. handle_key method:

    • Handles user key presses.
    • Moves the tiles in the grid according to the arrow key pressed.
    • Adds a new tile after each move.
    • Updates the GUI grid.
    • Checks for game over condition and prints the score if the game is over.
  8. move_tiles method:

    • Moves the tiles in the grid based on the direction specified (Up, Down, Left, Right).
    • Uses helper methods transpose, reverse, and merge_tiles for grid manipulation.
  9. merge_tiles method:

    • Merges adjacent tiles with the same value and updates the score accordingly.
  10. check_game_over method:

    • Checks if the game is over by looking for any empty cells or adjacent tiles with the same value.
  11. Helper Methods:

    • transpose: Transposes a matrix (used for rotating the grid).
    • reverse: Reverses the rows of a matrix (used for flipping the grid horizontally).
  12. main function:

    • Creates a Tkinter root window and initializes the game.
  13. if __name__ == "__main__": block:

    • Ensures that the main function is executed when the script is run directly, but not when it’s imported as a module.

Source Code:

				
					import tkinter as tk
import random

class Game2048:
    def __init__(self, master):
        self.master = master
        self.master.title("2048")
        self.master.geometry("400x400")
        self.master.bind("<Key>", self.handle_key)
        
        self.grid_size = 4
        self.grid = [[0] * self.grid_size for _ in range(self.grid_size)]
        self.score = 0
        
        self.init_grid()
        self.add_tile()
        self.update_grid()
        
    def init_grid(self):
        self.tiles = []
        for i in range(self.grid_size):
            row = []
            for j in range(self.grid_size):
                tile = tk.Label(self.master, text="", font=("Helvetica", 32), width=4, height=2, relief="raised")
                tile.grid(row=i, column=j, padx=5, pady=5)
                row.append(tile)
            self.tiles.append(row)
    
    def add_tile(self):
        empty_cells = [(i, j) for i in range(self.grid_size) for j in range(self.grid_size) if self.grid[i][j] == 0]
        if empty_cells:
            i, j = random.choice(empty_cells)
            self.grid[i][j] = 2 if random.random() < 0.9 else 4
    
    def update_grid(self):
        for i in range(self.grid_size):
            for j in range(self.grid_size):
                value = self.grid[i][j]
                if value == 0:
                    self.tiles[i][j].configure(text="", bg="lightgray")
                else:
                    self.tiles[i][j].configure(text=str(value), bg="lightblue")
        self.master.update_idletasks()
        
    def handle_key(self, event):
        if event.keysym in ['Up', 'Down', 'Left', 'Right']:
            self.move_tiles(event.keysym)
            self.add_tile()
            self.update_grid()
            if self.check_game_over():
                print("Game Over! Score:", self.score)
    
    def move_tiles(self, direction):
        if direction == 'Up':
            self.grid = self.transpose(self.grid)
            self.grid = self.merge_tiles(self.grid)
            self.grid = self.transpose(self.grid)
        elif direction == 'Down':
            self.grid = self.reverse(self.transpose(self.grid))
            self.grid = self.merge_tiles(self.grid)
            self.grid = self.transpose(self.reverse(self.grid))
        elif direction == 'Left':
            self.grid = self.merge_tiles(self.grid)
        elif direction == 'Right':
            self.grid = self.reverse(self.grid)
            self.grid = self.merge_tiles(self.grid)
            self.grid = self.reverse(self.grid)
    
    def merge_tiles(self, grid):
        score = 0
        for i in range(self.grid_size):
            j = 0
            while j < self.grid_size - 1:
                if grid[i][j] == grid[i][j+1] and grid[i][j] != 0:
                    grid[i][j] *= 2
                    score += grid[i][j]
                    grid[i][j+1] = 0
                    j += 2
                else:
                    j += 1
        self.score += score
        return grid
    
    def check_game_over(self):
        for i in range(self.grid_size):
            for j in range(self.grid_size):
                if self.grid[i][j] == 0:
                    return False
                if j < self.grid_size - 1 and self.grid[i][j] == self.grid[i][j+1]:
                    return False
                if i < self.grid_size - 1 and self.grid[i][j] == self.grid[i+1][j]:
                    return False
        return True
    
    @staticmethod
    def transpose(matrix):
        return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
    
    @staticmethod
    def reverse(matrix):
        return [row[::-1] for row in matrix]

def main():
    root = tk.Tk()
    game = Game2048(root)
    root.mainloop()

if __name__ == "__main__":
    main()

				
			

Output

2048 game using python

Find More Projects

Billing Management System using Python introduction: The Billing software using python is a simple yet effective Python application designed to facilitate the …

Medical Management System using Python with Complete Source code [By using Flask Framework] Introduction Hospital Management System The Hospital Management System is …

Chat Application Using HTML , CSS And Javascript Introduction Hello developers friends, all of you are welcome to this new and wonderful …

Travel Planner Website Using HTML , CSS And Javascript Introduction Hello friends, welcome to all of you developer friends, in this new …

Dictionary App using HTML, CSS, JavaScript & ReactJs Introduction : The React dictionary app is a web application designed to provide users …

Weather App using HTML, CSS, JavaScript & ReactJs Introduction : The React Weather App is a web application designed to provide users …

More Python Projects
Get Huge Discounts

All Coding Handwritten Notes

Browse Handwritten Notes