أعتذر عن ذلك. لنضيف التفاعل وإمكانية إضافة الأرقام عند النقر على المربعات. هذا هو الكود المحدث: ```python import pygame import random # Initialize Pygame pygame.init() # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAY = (200, 200, 200) # Screen dimensions WIDTH = 400 HEIGHT = 400 GRID_SIZE = 4 TILE_SIZE = WIDTH // GRID_SIZE # Fonts FONT = pygame.font.SysFont("arial", 40) # Directional constants UP = "up" DOWN = "down" LEFT = "left" RIGHT = "right" # Initialize screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("2048") # Function to draw the grid def draw_grid(): for y in range(GRID_SIZE): for x in range(GRID_SIZE): rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE) pygame.draw.rect(screen, GRAY, rect, 3) # Function to draw the numbers def draw_numbers(grid): for y in range(GRID_SIZE): for x in range(GRID_SIZE): value = grid[y][x] if value: text = FONT.render(str(value), True, BLACK) text_rect = text.get_rect(center=(x * TILE_SIZE + TILE_SIZE // 2, y * TILE_SIZE + TILE_SIZE // 2)) screen.blit(text, text_rect) # Function to add a new tile def add_new_tile(grid): empty_cells = [(y, x) for y in range(GRID_SIZE) for x in range(GRID_SIZE) if grid[y][x] == 0] if empty_cells: y, x = random.choice(empty_cells) grid[y][x] = random.choice([2, 4]) return grid # Function to move the grid def move_grid(grid, direction): new_grid = [row[:] for row in grid] if direction == UP: for x in range(GRID_SIZE): for y in range(1, GRID_SIZE): for k in range(y, 0, -1): if new_grid[k - 1][x] == 0: new_grid[k - 1][x] = new_grid[k][x] new_grid[k][x] = 0 elif new_grid[k - 1][x] == new_grid[k][x]: new_grid[k - 1][x] *= 2 new_grid[k][x] = 0 elif direction == DOWN: for x in range(GRID_SIZE): for y in range(GRID_SIZE - 2, -1, -1): for k in range(y, GRID_SIZE - 1): if new_grid[k + 1][x] == 0: new_grid[k + 1][x] = new_grid[k][x] new_grid[k][x] = 0 elif new_grid[k + 1][x] == new_grid[k][x]: new_grid[k + 1][x] *= 2 new_grid[k][x] = 0 elif direction == LEFT: for y in range(GRID_SIZE): for x in range(1, GRID_SIZE): for k in range(x, 0, -1): if new_grid[y][k - 1] == 0: new_grid[y][k - 1] = new_grid[y][k] new_grid[y][k] = 0 elif new_grid[y][k - 1] == new_grid[y][k]: new_grid[y][k - 1] *= 2 new_grid[y][k] = 0 elif direction == RIGHT: for y in range(GRID_SIZE): for x in range(GRID_SIZE - 2, -1, -1): for k in range(x, GRID_SIZE - 1): if new_grid[y][k + 1] == 0: new_grid[y][k + 1] = new_grid[y][k] new_grid[y][k] = 0 elif new_grid[y][k + 1] == new_grid[y][k]: new_grid[y][k + 1] *= 2 new_grid[y][k] = 0 return new_grid # Function to check if the game is over def is_game_over(grid): for y in range(GRID_SIZE): for x in range(GRID_SIZE): if grid[y][x] == 0: return False if x < GRID_SIZE - 1 and grid[y][x] == grid[y][x + 1]: return False if y < GRID_SIZE - 1 and grid[y][x] == grid[y + 1][x]: return False return True # Function to display game over message def game_over_message(): screen.fill(WHITE) text = FONT.render("Game Over!", True, BLACK) text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2)) screen.blit(text, text_rect) pygame.display.flip() # Main function def main(): grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] draw_grid بالطبع، ها هو الجزء المتبقي من الكود: ```python draw_grid() pygame.display.flip() # Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() clicked_row = mouse_pos[1] // TILE_SIZE clicked_col = mouse_pos[0] // TILE_SIZE grid[clicked_row][clicked_col] = random.choice([2, 4]) screen.fill(WHITE) draw_grid() draw_numbers(grid) pygame.display.flip() pygame.quit() if __name__ == "__main__": main() ``` هذا يكمل الكود الذي يتيح لك إضافة الأرقام عند النقر بالماوس داخل المربعات. يرجى تجربته وإعلامي بأي مشاكل تواجهك.