2048 game using Python

2048 game

Introduction

We will demonstrate how to create a 2048 game using python. For this project, the Tkinter library and random is utilized. Tkinter is a powerful tool for developing graphical user interfaces (GUIs). The random module in Python provides functions for generating random numbers.  This program has straightforward buttons and canvas is used.

Explanation

A basic Tkinter-based GUI with buttons are produced by this code. The game logic revolves around a 4×4 grid where numbered tiles slide across the grid in response to user input (arrow keys). The objective is to combine tiles with the same number to reach the 2048 tile. The game grid is represented as a 2D list, with each element corresponding to a tile’s value. Tiles slide as far as possible in the chosen direction, combining with adjacent tiles of the same value along the way. After each move, a new tile (with a value of either 2 or 4) appears at a random empty spot on the grid.

The game responds to arrow key events for moving tiles in different directions (up, down, left, right). Each arrow key press triggers a move, which involves shifting tiles and combining them according to the game rules. A “New Game” button allows players to start a new game at any time, resetting the game grid and score.

Source Code for 2048 game

import tkinter as tk
import random

class Game2048:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title('2048')
        self.window.geometry('400x430')

        self.canvas = tk.Canvas(self.window, bg='white', width=400, height=400)
        self.canvas.pack()

        self.new_game_button = tk.Button(self.window, text='New Game', command=self.new_game)
        self.new_game_button.pack()

        self.grid = [[0]*4 for _ in range(4)]
        self.score = 0

        self.draw_grid()
        self.place_new_tile()
        self.place_new_tile()

        self.window.bind('<Key>', self.move)

    def draw_grid(self):
        for i in range(4):
            for j in range(4):
                self.canvas.create_rectangle(j*100, i*100, (j+1)*100, (i+1)*100, fill='light gray', outline='gray')
                self.canvas.create_text(j*100+50, i*100+50, text=str(self.grid[i][j]), font=('Arial', 24, 'bold'))

    def place_new_tile(self):
        available_cells = [(i, j) for i in range(4) for j in range(4) if self.grid[i][j] == 0]
        if available_cells:
            i, j = random.choice(available_cells)
            self.grid[i][j] = 2 if random.random() < 0.9 else 4
            self.update_grid()

    def move(self, event):
        if event.keysym in ('Up', 'Down', 'Left', 'Right'):
            if event.keysym == 'Up':
                self.shift_tiles(-1, 0)
            elif event.keysym == 'Down':
                self.shift_tiles(1, 0)
            elif event.keysym == 'Left':
                self.shift_tiles(0, -1)
            elif event.keysym == 'Right':
                self.shift_tiles(0, 1)
            self.place_new_tile()

    def shift_tiles(self, dx, dy):
        moved = False
        for i in range(4):
            for j in range(4):
                x, y = i+dx, j+dy
                while 0 <= x < 4 and 0 <= y < 4:
                    if self.grid[x][y] == 0:
                        self.grid[x][y] = self.grid[i][j]
                        self.grid[i][j] = 0
                        i, j = x, y
                        x, y = i+dx, j+dy
                        moved = True
                    elif self.grid[x][y] == self.grid[i][j]:
                        self.grid[x][y] *= 2
                        self.grid[i][j] = 0
                        self.score += self.grid[x][y]
                        moved = True
                        break
                    else:
                        break
                    x, y = x+dx, y+dy
        if moved:
            self.update_grid()

    def update_grid(self):
        self.canvas.delete('all')
        self.draw_grid()
        self.window.update()

    def new_game(self):
        self.grid = [[0]*4 for _ in range(4)]
        self.score = 0
        self.canvas.delete('all')
        self.draw_grid()
        self.place_new_tile()
        self.place_new_tile()

if __name__ == "__main__":
    game = Game2048()
    game.window.mainloop()

Output

Leave a Comment