0% found this document useful (0 votes)
10 views

Divash Project

This Python program implements the popular 2048 game using Tkinter for the GUI. The program is divided into Board and Game classes - Board handles the GUI and game grid, Game controls logic and user input. The player uses arrow keys to merge tiles, with the goal of reaching a 2048 tile. The program provides a basic but functional 2048 game.

Uploaded by

Danger Yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Divash Project

This Python program implements the popular 2048 game using Tkinter for the GUI. The program is divided into Board and Game classes - Board handles the GUI and game grid, Game controls logic and user input. The player uses arrow keys to merge tiles, with the goal of reaching a 2048 tile. The program provides a basic but functional 2048 game.

Uploaded by

Danger Yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PROJECT REPORT

ON
PYTHON BASED
2048 GAME

SUBMITTED BY:-

NAME OF STUDENT:- ROLL NO:-


DIVANSH 210090800034

UNDER THE SUPERVISION OF:-

SOLITAIRE INFOSYS
Mohali: C-110, Industrial Area Phase-VII, Mohali, India.
DECLARATION
I am a student of Computer ENGG. Diploma, session: 2021- 2024
in your college. We hereby declare that the work presented in this
Project is the outcome of our own bona fide work and is correct
to the best of our knowledge and this work has been undertaken
taking care of Engineering Ethics. It contains no material
previously published or written by another person nor material
which has been accepted for the award of any other degree or
diploma of the university or other institute of higher learning,
except where due acknowledgment has been made in the text.

Place: Solitaire Infosys Pvt. Ltd. Mohali


CERTIFICATE
ACKNOWLEDGEMENT
ABSTRACT

I have done my training at Solitaire Infosys Pvt. Ltd. to


improve mytechnical abilities in computer and
Programming.
INTRODUCTION TO COMPANY

Solitaire Infosys Pvt. Ltd. is an acclaimed IT service provider contributing its part in
the development of many businesses around the globe. They socialize with their
clients to get a superior cognizance of their business and requirements and helpthem
in fabricating websites and applications for their business. The company was founded
in 2011 by a dynamic duo with the same aim and zeal; they have come a long way in
satisfying our clients. They are serving their clients with the world- class services for
more than seven years now.

Services provided by Solitaire Infosys are Web Development, Mobile Application


Development, UI & UX Design Services etc.

With every project that they deliver, they deliver their respect, creativity, quality,
transparency, and teamwork to their clients. They have the experience, expertise, and
capabilities to enable organizations to accelerate their service processes in every
possible way. They are known for their excellent customer satisfaction, cost-
effectiveness, and innovative skills that are unparalleled.

VISION

They visualize becoming the most trusted and respected IT service provider across
the globe with their vibrant, dynamic, and value-based IT solutions that revolve
around their clients, team, and international standards. Solitaire Infosys Pvt. Ltd.
envisions becoming a reliable partner to all its clients and focusing on doing
everything ethically and rightly. They are always open to accept their mistakes and
have the nerve to do the necessary changes.
CONTENT

1. Declaration

2. Certificate

3. Acknowledgement

4. Abstract

5. Introduction to Company

6. Introduction to Project

7. Program Description

8. Interaction Diagram
9.Project Implementation

10.Project Benefits

11.Project Architecture

12.Conclusion
INTRODUCTION TO PROJECT

The provided program is an


implementation of the popular game "2048" using the Python
tkinter library for the graphical user interface. "2048" is a
puzzle game where the player combines numbered tiles to
reach the tile with the value of 2048. The game is played on a
4x4 grid, and the player can move tiles in four directions: up,
down, left, and right. The goal is to strategically merge tiles
with the same number to create larger numbers and ultimately
reach 2048.

In this Python program, the game is represented as a graphical


interface, and the game logic is implemented using classes and
methods. The game grid is displayed using labels, and the
player can use arrow keys to make moves. The game keeps
track of the player's score and displays messages when the
player wins or loses.
PROGRAM DESCRIPTION

1. Importing Libraries:-
➢ from tkinter import *: Imports the tkinter library, which
is used to create the GUI for the game.
➢ from tkinter import messagebox: Imports the
messagebox module from tkinter to display messages in
pop-up windows.
➢ import random: Imports the random module for
generating random numbers.
2. Class Board:-
➢ This class represents the game board and contains
methods for managing the game grid and GUI elements.
➢ It initializes the Tkinter window, sets its title, and creates
a grid for the game board.
➢ The bg_color and color dictionaries store background and
text color codes for different tile values.
➢ The init method sets up the initial state of the game
board.
3.Class Methods:-
➢ reverse(): Reverses the rows of the game grid.
➢ transpose(): Transposes the game grid (switches rows
and columns).
➢ CompressGrid (): Compresses the grid by moving non-
zero elements to the left.
PROJECT EMPLEMENTATION

This is a Python program that implements the game 2048 using the
Tkinter library for the graphical user interface (GUI). The program is
divided into two main classes: Board and Game.
1. Board Class:
• This class defines the graphical interface of the game.

• It initializes the game window, sets its title, and creates a

grid of cells to represent the game board.


• The bg_color and color dictionaries are used to define the

background and text colors for different numbers on the


grid.
• The game board is represented as a 4x4 grid of labels, with

each label representing a cell on the game board.


• The compressGrid, mergeGrid, random_cell, and other

methods are used to manipulate the game board's state and


perform game logic.
• The paintGrid method updates the GUI to reflect the

current state of the game board.


1. Game Class:
• This class controls the game logic and user interactions.

• The start method initializes the game by placing two

random "2" tiles on the board and setting up event handling


for user input.
• The link_keys method handles key events (Up, Down,
Left, Right) and performs the corresponding game moves
(e.g., merging tiles, moving tiles).
• It also checks for game-over conditions and winning
conditions.
• If the player reaches a 2048 tile, they win the game, and a
message box is displayed.
• If there are no available moves left (no empty cells and no
adjacent tiles with the same value), the game is over, and a
message box is displayed.
• The paintGrid method updates the GUI after each move.

The main part of the program at the end creates an instance of the
Board class (gamepanel) and an instance of the Game class
(game2048). It then starts the game by calling the start method of
the Game class.
The program provides a basic implementation of the 2048 game,
allowing the player to play using arrow keys and displaying messages
when the game is won or lost. The game board is represented visually
using Tkinter's labels, and the game logic is implemented within the
Game class.
PROJECT BENEFITS

The given program is a Python implementation of the popular 2048


game using the Tkinter library for the graphical user interface (GUI).
The project can serve various educational and entertainment
purposes. Here are some potential uses for the project:
1. Game Development Learning Tool: This project can be used
as a learning tool for individuals who want to understand the
basics of game development. It demonstrates concepts such as
game loops, user input handling, and graphical rendering.
2. Python and Tkinter Practice: As a Python project, it provides
an opportunity for Python and Tkinter learners to practice their
skills. It involves GUI design, event handling, and data
manipulation in Python.
3. Algorithm and Logic Practice: The 2048 game involves
strategic thinking and planning. Players need to merge tiles
strategically to achieve higher numbers. This project can help
individuals practice problem-solving and algorithmic thinking.
4. Teaching Programming Concepts: For educators, this project
can be used to teach programming concepts to students. It
covers topics like classes, object-oriented programming, data
structures, and conditional statements.
5. Customization and Modification: Users can modify and
extend the project to create variations of the 2048 game or add
new features. This can be a great exercise in software
development and creativity.
6. Interactive GUI Development: The project showcases how to
create an interactive graphical user interface using Tkinter. It
can be a reference for those interested in GUI development in
Python.
7. Entertainment and Relaxation: Beyond its educational value,
the 2048 game is fun and can be used for relaxation and
entertainment. It offers a challenging puzzle-solving experience.
8. Portfolio Project: Aspiring developers can use this project as
part of their portfolio to showcase their programming skills and
their ability to create interactive applications.
9. Hackathons and Coding Challenges: Participants in
hackathons or coding challenges can use this project as a
starting point to build a game or gamified application quickly.
10.Open Source Contribution: The project could be open-
sourced, allowing developers to collaborate, improve the
codebase, and contribute to the broader Python and game
development communities.
In summary, the 2048 game project can serve both educational and
recreational purposes, offering a practical way to learn and practice
programming skills while having fun playing the game.
Project Architecture

1. GUI (Graphical User Interface):


• The graphical user interface is built using the Tkinter library.

• It provides the game board layout, score display, and interaction

with the user.


2. Classes:
Board Class:
• This class represents the game board and manages its

appearance and state.


• Attributes:

• window: The main game window.

• gameArea: A frame within the window for displaying the

game grid.
• board: A 2D list of Label widgets representing the game

cells.
• gridCell: A 2D list representing the game state.

• compress, merge, and moved: Flags to track whether

actions have occurred.


• score: Stores the player's score.

• Methods:

• __init (): Initializes the game board and creates the grid
of cells.
• reverse(): Reverses the rows of the grid.

• transpose(): Transposes the grid.

• compressGrid(): Compresses the grid by eliminating

empty cells.
• mergeGrid(): Merges cells with the same values.
• random_cell(): Generates a random "2" tile in an empty
cell.
• can_merge(): Checks if there are any possible merges.
• paintGrid(): Updates the GUI to reflect the current game
state.

Game Class:
• This class controls the game logic and user interactions.

• Attributes:

• gamepanel: An instance of the Board class.

• end: Tracks if the game is over.

• won: Tracks if the player has won.

• Methods:

• __init (): Initializes the game with the provided game


panel.
• start(): Starts the game, initializes the board, and binds key

events.
• link_keys(event): Handles user key events (Up, Down,

Left, Right) and updates the game state accordingly.


• Checks for game-over and winning conditions.

• Displays messages in case of a win or game over.

• paintGrid(): Updates the GUI to reflect the current game

state.

3. Main Program Flow:


• The program creates an instance of the Board class

(gamepanel) and an instance of the Game class (game2048).

• It starts the game by calling the start method of the Game class.
• The player interacts with the game using arrow keys, and the
game logic is handled by the Game class.
• Messages are displayed in message boxes when the player wins
or loses.
4. Data Structures:
• Lists and dictionaries are used to represent the game board and

manage its state.


• Lists of Label widgets are used to create the visual

representation of the game board.

5. Game Logic:
• Game logic, such as merging tiles, generating new tiles,

checking for game-over, and checking for a win, is implemented


in the Game and Board classes.
• The program tracks the player's score.

6. User Interface:
• Tkinter is used to create a simple and user-friendly interface for

the game.
• Labels are used to display the numbers on the game board, and

different colors are assigned to different numbers.


7. Event Handling:
• The program listens for keyboard events (arrow keys) to allow

the player to make moves.


• Key events trigger the link_keys method in the Game class,

which handles the game's response to player input.


This architecture provides a clear separation between the game logic
and the user interface, making the program modular and
maintainable. It allows the player to enjoy the 2048 game through a
graphical interface while tracking their progress and displaying
game-related messages.
CONCLUSON

By now, you should have a good understanding of the Python language


as well as the PyGame library. With the games included in this text you
should have a good understanding of what goes into creating a video
game. Indeed, armed with a good idea, you should have enough
knowledge to make a game on your own! In this book we’ve covered
player inputs displaying graphics, playing sounds, and moving
characters about the screen as well as alternative forms of input and
output in the form of reading and writing

You might also like