Skip to content

Commit

Permalink
Added the more features in 2048 game
Browse files Browse the repository at this point in the history
  • Loading branch information
Kritika75 authored Feb 6, 2025
1 parent acbf3a0 commit 1926aa5
Showing 1 changed file with 150 additions and 41 deletions.
191 changes: 150 additions & 41 deletions Game_Development/2048/2048.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pygame
import random
import os
import json

# Constants
GRID_SIZE = 4
Expand All @@ -20,13 +22,27 @@
2048: (237, 194, 46),
}

# Leaderboard file
LEADERBOARD_FILE = "leaderboard.json"

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE))
screen = pygame.display.set_mode((GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE + 150)) # Extra space for UI elements
pygame.display.set_caption('2048 Game')
font = pygame.font.Font(None, 40)
score_font = pygame.font.Font(None, 36)

# Game Functions
def load_leaderboard():
if os.path.exists(LEADERBOARD_FILE):
with open(LEADERBOARD_FILE, "r") as file:
return json.load(file)
return {}

def save_leaderboard(leaderboard):
with open(LEADERBOARD_FILE, "w") as file:
json.dump(leaderboard, file, indent=4)

def init_game():
grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)]
add_random_tile(grid)
Expand All @@ -39,16 +55,28 @@ def add_random_tile(grid):
x, y = random.choice(empty_cells)
grid[x][y] = random.choice([2, 4])

def draw_grid(grid):
def draw_grid(grid, score, high_score, username):
# Draw the grid cells
for x in range(GRID_SIZE):
for y in range(GRID_SIZE):
value = grid[x][y]
color = CELL_COLORS.get(value, (60, 58, 50))
pygame.draw.rect(screen, color, (y * CELL_SIZE, x * CELL_SIZE, CELL_SIZE, CELL_SIZE))
pygame.draw.rect(screen, color, (y * CELL_SIZE, x * CELL_SIZE + 100, CELL_SIZE, CELL_SIZE)) # Shifted down for UI space
if value != 0:
text = font.render(str(value), True, (255, 255, 255))
text_rect = text.get_rect(center=(y * CELL_SIZE + CELL_SIZE // 2, x * CELL_SIZE + CELL_SIZE // 2))
text_rect = text.get_rect(center=(y * CELL_SIZE + CELL_SIZE // 2, x * CELL_SIZE + 100 + CELL_SIZE // 2))
screen.blit(text, text_rect)

# Draw the scores
score_text = score_font.render(f"Score: {score}", True, (255, 255, 255))
screen.blit(score_text, (10, 10))

high_score_text = score_font.render(f"High Score: {high_score}", True, (255, 255, 255))
screen.blit(high_score_text, (10, 50))

# Draw the username
username_text = score_font.render(f"Player: {username}", True, (255, 255, 255))
screen.blit(username_text, (10, 90))

def move_left(grid):
new_grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)]
Expand Down Expand Up @@ -100,43 +128,124 @@ def check_game_over(grid):
return True

# Main Game Loop
grid = init_game()
running = True
game_over = False
game_won = False

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and not game_over and not game_won:
if event.key == pygame.K_LEFT:
grid = move_left(grid)
elif event.key == pygame.K_RIGHT:
grid = move_right(grid)
elif event.key == pygame.K_UP:
grid = move_up(grid)
elif event.key == pygame.K_DOWN:
grid = move_down(grid)
add_random_tile(grid)

screen.fill(GRID_COLOR)
draw_grid(grid)

# Check for win
if check_win(grid):
game_won = True
pygame.display.set_caption('You Won!')

# Check for game over
if check_game_over(grid):
game_over = True
pygame.display.set_caption('Game Over!')
def game_loop(username):
grid = init_game()
current_score = 0
leaderboard = load_leaderboard()
game_over = False
game_won = False

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

if event.type == pygame.KEYDOWN and not game_over and not game_won:
if event.key == pygame.K_LEFT:
grid = move_left(grid)
elif event.key == pygame.K_RIGHT:
grid = move_right(grid)
elif event.key == pygame.K_UP:
grid = move_up(grid)
elif event.key == pygame.K_DOWN:
grid = move_down(grid)
add_random_tile(grid)

# Update the current score based on merged tiles (this is where you'd add your logic)
current_score += 10 # Example, replace with actual score logic

screen.fill(GRID_COLOR)
draw_grid(grid, current_score, current_score, username)

# Check for win
if check_win(grid):
game_won = True
pygame.display.set_caption('You Won!')

# Check for game over
if check_game_over(grid):
game_over = True
pygame.display.set_caption('Game Over!')

# Update the leaderboard if needed
if current_score > leaderboard.get(username, 0):
leaderboard[username] = current_score
save_leaderboard(leaderboard)

pygame.display.flip()

if game_over or game_won:
pygame.time.wait(2000)
return leaderboard

# Display the leaderboard
def display_leaderboard():
leaderboard = load_leaderboard()
leaderboard_sorted = sorted(leaderboard.items(), key=lambda x: x[1], reverse=True)

leaderboard_text = "Leaderboard:\n"
for idx, (user, score) in enumerate(leaderboard_sorted[:5]): # Display top 5
leaderboard_text += f"{idx + 1}. {user}: {score}\n"

leaderboard_surface = pygame.Surface((GRID_SIZE * CELL_SIZE, 150))
leaderboard_surface.fill((0, 0, 0))
leaderboard_surface.set_alpha(200)
screen.blit(leaderboard_surface, (0, GRID_SIZE * CELL_SIZE))

leaderboard_display = score_font.render(leaderboard_text, True, (255, 255, 255))
screen.blit(leaderboard_display, (10, GRID_SIZE * CELL_SIZE + 10))

pygame.display.flip()

if game_over or game_won:
pygame.time.wait(2000)
running = False

pygame.time.wait(5000)

# Main Menu - Allow the user to input their username
def main_menu():
username = ''
input_box = pygame.Rect(100, 100, 140, 32)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
clock = pygame.time.Clock()

while True:
screen.fill((30, 30, 30))

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = True
color = color_active
else:
active = False
color = color_inactive

if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
return text
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode

txt_surface = font.render(text, True, color)
width = max(200, txt_surface.get_width()+10)
input_box.w = width
screen.blit(txt_surface, (input_box.x+5, input_box.y+5))
pygame.draw.rect(screen, color, input_box, 2)

pygame.display.flip()
clock.tick(30)

# Start Game
username = main_menu()
leaderboard = game_loop(username)
display_leaderboard()
pygame.quit()

0 comments on commit 1926aa5

Please sign in to comment.