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:
  4. Create a New Python File:
  5. Copy the Code:
  6. Run the Code:
  7. Play the Game:

Code Explanation

Let’s break down the code step by step:

  1. Imports:
  2. Class Definition (Game2048):
  3. __init__ method:
  4. init_grid method:
  5. add_tile method:
  6. update_grid method:
  7. handle_key method:
  8. move_tiles method:
  9. merge_tiles method:
  10. check_game_over method:
  11. Helper Methods:
  12. main function:
  13. if __name__ == "__main__": block:

Source Code: Python


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


要查看或添加评论,请登录

社区洞察

其他会员也浏览了