Skip to content

Updated the UI of the 2048 Game and added some features. #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions 2048 Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,41 @@ Classes have been defined in the given code:
1) **Board**:
In this I decided the background color and tile colors of the board game, along with initialising variables like score, grid, main_window, board etc,
along with initialising functions specifically:
1) __init__(self): which initialise the above mentioned variables
1) __init__(self): which initialise the above mentioned variables and sets up the game interface with score display, high score display, and restart button
2) Transpose: Uses zip function to get the transpose
3) Reverse: To reverse the grid matrix
4) CompressGrid: Moves all non empty to the left side to simplify merging!
5) mergeGrid: It adds the grid value of two adjacent tiles if they have same grid values.
5) mergeGrid: It adds the grid value of two adjacent tiles if they have same grid values and updates the score display
6) Random_cell: It first stores all the empty cells in a list and then picks a random cell from the created list and make its grid value 2
7) Can_merge: It returns a boolean value denoting we can merge any two tiles or not. We can merge two tiles if and only if they hold the same grid value.
8) paintGrid: It assigns foreground and background color to each tile of the 4×4 grid corresponding to its grid value.
7) Can_merge: It returns a boolean value denoting we can merge any two tiles or not. We can merge two tiles if and only if they hold the same grid value
8) paintGrid: It assigns foreground and background color to each tile of the 4×4 grid corresponding to its grid value
9) load_high_score: Loads the previous high score from a file
10) save_high_score: Saves the current high score to a file
11) restart_game: Resets the game state and starts a new game

2) **Game**:
Functions:
1) __init__(self): It is the constructor function. It initializes all the variables with appropriate default values.
2) start: It calls random_cell twice to assign ‘2’ to grid value of two random cells/tiles,
and then it paints the grid and after that, it calls link_keys to link up, down, left, and right keys.
1) __init__(self): It is the constructor function. It initializes all the variables with appropriate default values
2) start: It calls random_cell twice to assign '2' to grid value of two random cells/tiles,
and then it paints the grid and after that, it calls link_keys to link up, down, left, and right keys
3) Link_keys: It initially checks if the game is already won or lost, and if it is, it executes a return statement without doing anything.
Otherwise, it continues the execution procedure.
Otherwise, it continues the execution procedure
4) restart: Resets the game state flags and initializes a new game

### LIBRARIES NEEDED

The follwing are required :
The following are required:
1) Tkinter - The GUI library (python)
2) os - For file operations (high score persistence)




### DEMONSTRATION
### DEMONSTRATION

https://drive.google.com/file/d/1iTS2wU-UZ7ZeiQCI2A1W8D_NX4q-Xqvi/view?usp=sharing


### NAME
Shreya Ganjoo
Krishnaprasath Venkadesan
61 changes: 58 additions & 3 deletions 2048 Game/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from tkinter import *
from tkinter import messagebox
import random
import os

class Board:
bg_color={
#'2': '#eee4da',
Expand Down Expand Up @@ -33,6 +35,20 @@ def __init__(self):
self.number=4
self.main_window=Tk()
self.main_window.title('2048 Game')

# Load high score
self.high_score = self.load_high_score()

# Modified score frame to include restart button
self.score_frame = Frame(self.main_window)
self.score_frame.grid(row=0, column=0, columnspan=4, pady=5)
self.score_label = Label(self.score_frame, text="Score: 0", font=('arial', 20, 'bold'))
self.score_label.grid(row=0, column=0, padx=10)
self.high_score_label = Label(self.score_frame, text=f"High Score: {self.high_score}", font=('arial', 20, 'bold'))
self.high_score_label.grid(row=0, column=1, padx=10)
self.restart_button = Button(self.score_frame, text="Restart", font=('arial', 15, 'bold'), command=self.restart_game)
self.restart_button.grid(row=0, column=2, padx=10)

self.gameArea=Frame(self.main_window,bg= 'dark green')
self.board=[]
self.grid=[[0]*4 for i in range(4)]
Expand Down Expand Up @@ -71,6 +87,17 @@ def compressGrid(self):
self.compress=True
count+=1
self.grid=temp
def load_high_score(self):
try:
with open('high_score.txt', 'r') as file:
return int(file.read())
except:
return 0

def save_high_score(self):
with open('high_score.txt', 'w') as file:
file.write(str(self.high_score))

def mergeGrid(self):
self.merge=False
for i in range(4):
Expand All @@ -79,6 +106,11 @@ def mergeGrid(self):
self.grid[i][j] *= 2
self.grid[i][j + 1] = 0
self.score += self.grid[i][j]
if self.score > self.high_score:
self.high_score = self.score
self.save_high_score()
self.high_score_label.config(text=f"High Score: {self.high_score}")
self.score_label.config(text=f"Score: {self.score}")
self.merge = True
def random_cell(self):
cells=[]
Expand Down Expand Up @@ -111,11 +143,34 @@ def paintGrid(self):
self.board[i][j].config(text=str(self.grid[i][j]),
bg=self.bg_color.get(str(self.grid[i][j])),
fg=self.color.get(str(self.grid[i][j])))
def restart_game(self):
# Reset grid
self.grid = [[0]*4 for i in range(4)]
self.score = 0
self.compress = False
self.merge = False
self.moved = False

# Update score display
self.score_label.config(text="Score: 0")

# Initialize new game
self.random_cell()
self.random_cell()
self.paintGrid()
class Game:
def __init__(self,playpanel):
self.playpanel=playpanel
self.end=False
self.won=False
self.playpanel = playpanel
self.end = False
self.won = False

# Add restart method to reset game state
self.playpanel.restart_game = lambda: self.restart()

def restart(self):
self.end = False
self.won = False
self.playpanel.restart_game()
def start(self):
self.playpanel.random_cell()
self.playpanel.random_cell()
Expand Down
1 change: 1 addition & 0 deletions high_score.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1488