From 7f4f111bc1c621c4b9fc6add70ee5a9e115cb1a5 Mon Sep 17 00:00:00 2001 From: Krishnaprasath <230801093@rajalakshmi.edu.in> Date: Tue, 8 Apr 2025 20:27:24 +0530 Subject: [PATCH] Updated the UI of the 2048 Game and some features. --- 2048 Game/README.md | 27 ++++++++++++-------- 2048 Game/main.py | 61 ++++++++++++++++++++++++++++++++++++++++++--- high_score.txt | 1 + 3 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 high_score.txt diff --git a/2048 Game/README.md b/2048 Game/README.md index 5aab9d8..0b1f09a 100644 --- a/2048 Game/README.md +++ b/2048 Game/README.md @@ -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 \ No newline at end of file diff --git a/2048 Game/main.py b/2048 Game/main.py index 46d759e..dbb58b7 100644 --- a/2048 Game/main.py +++ b/2048 Game/main.py @@ -1,6 +1,8 @@ from tkinter import * from tkinter import messagebox import random +import os + class Board: bg_color={ #'2': '#eee4da', @@ -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)] @@ -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): @@ -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=[] @@ -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() diff --git a/high_score.txt b/high_score.txt new file mode 100644 index 0000000..222272a --- /dev/null +++ b/high_score.txt @@ -0,0 +1 @@ +1488 \ No newline at end of file