حسنًا، دعني أرسل لك الجزء الأول من السكربت. هذا الجزء يتضمن تعريف الواجهة الرسومية وتهيئة الشبكة الرئيسية. ```python import pygame import sys import random # تعيين الألوان BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (200, 200, 200) LIGHT_BLUE = (173, 216, 230) GREEN = (0, 255, 0) # تعيين المتغيرات العامة للشاشة SCREEN_WIDTH = 400 SCREEN_HEIGHT = 400 GRID_SIZE = 4 GRID_SPACING = 10 CELL_SIZE = (SCREEN_WIDTH - (GRID_SIZE + 1) * GRID_SPACING) // GRID_SIZE # تهيئة الشاشة pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("2048 Game") # تعريف الخطوط title_font = pygame.font.Font(None, 48) score_font = pygame.font.Font(None, 36) button_font = pygame.font.Font(None, 24) # تهيئة المتغيرات للعبة grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] last_move_text = "" def draw_grid(): screen.fill(WHITE) for i in range(GRID_SIZE): for j in range(GRID_SIZE): rect = pygame.Rect((GRID_SPACING + CELL_SIZE) * j + GRID_SPACING, (GRID_SPACING + CELL_SIZE) * i + GRID_SPACING, CELL_SIZE, CELL_SIZE) pygame.draw.rect(screen, GRAY, rect) if grid[i][j] != 0: draw_number(grid[i][j], rect.topleft) def draw_number(number, pos): number_text = score_font.render(str(number), True, BLACK) text_rect = number_text.get_rect(center=pos) screen.blit(number_text, text_rect) def draw_buttons(): button_rect = pygame.Rect(50, SCREEN_HEIGHT - 80, 100, 50) pygame.draw.rect(screen, GREEN, button_rect) button_text = button_font.render("Start", True, WHITE) text_rect = button_text.get_rect(center=button_rect.center) screen.blit(button_text, text_rect) def draw_last_move(): last_move_rect = pygame.Rect(180, SCREEN_HEIGHT - 80, 200, 50) pygame.draw.rect(screen, LIGHT_BLUE, last_move_rect) last_move_text_render = button_font.render(last_move_text, True, BLACK) text_rect = last_move_text_render.get_rect(center=last_move_rect.center) screen.blit(last_move_text_render, text_rect) def main(): global last_move_text running = True while running: screen.fill(WHITE) draw_grid() draw_buttons() draw_last_move() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if button_rect.collidepoint(mouse_pos): # قم بتنفيذ بدء اللعبة pass pygame.display.flip() if __name__ == "__main__": main() ``` هل تحتاج إلى شرح لأي جزء من هذا الكود؟ حسنًا، سأقوم بإرسال الجزء الثاني من السكربت الآن الذي يتضمن المنطق للعبة 2048. ```python # تعريف الحركات الأساسية def move_left(grid): new_grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] for i in range(GRID_SIZE): position = 0 for j in range(GRID_SIZE): if grid[i][j] != 0: if new_grid[i][position] == 0: new_grid[i][position] = grid[i][j] elif new_grid[i][position] == grid[i][j]: new_grid[i][position] *= 2 position += 1 else: position += 1 new_grid[i][position] = grid[i][j] return new_grid def move_right(grid): new_grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] for i in range(GRID_SIZE): position = GRID_SIZE - 1 for j in range(GRID_SIZE - 1, -1, -1): if grid[i][j] != 0: if new_grid[i][position] == 0: new_grid[i][position] = grid[i][j] elif new_grid[i][position] == grid[i][j]: new_grid[i][position] *= 2 position -= 1 else: position -= 1 new_grid[i][position] = grid[i][j] return new_grid def move_up(grid): new_grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] for j in range(GRID_SIZE): position = 0 for i in range(GRID_SIZE): if grid[i][j] != 0: if new_grid[position][j] == 0: new_grid[position][j] = grid[i][j] elif new_grid[position][j] == grid[i][j]: new_grid[position][j] *= 2 position += 1 else: position += 1 new_grid[position][j] = grid[i][j] return new_grid def move_down(grid): new_grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] for j in range(GRID_SIZE): position = GRID_SIZE - 1 for i in range(GRID_SIZE - 1, -1, -1): if grid[i][j] != 0: if new_grid[position][j] == 0: new_grid[position][j] = grid[i][j] elif new_grid[position][j] == grid[i][j]: new_grid[position][j] *= 2 position -= 1 else: position -= 1 new_grid[position][j] = grid[i][j] return new_grid def is_game_over(grid): if any(0 in row for row in grid): return False for i in range(GRID_SIZE): for j in range(GRID_SIZE - 1): if grid[i][j] == grid[i][j + 1] or grid[j][i] == grid[j + 1][i]: return False return True def get_available_moves(grid): moves = [move_left, move_right, move_up, move_down] available_moves = [] for move in moves: new_grid = move(grid) if new_grid != grid: available_moves.append(move) return available_moves def expectimax(grid, depth): if depth == 0 or is_game_over(grid): return evaluate(grid) available_moves = get_available_moves(grid) if not available_moves: return evaluate(grid) if depth % 2 == 0: # Max node return max(expectimax(move(grid), depth - 1) for move in available_moves) else: # Expectation node total_score = 0 empty_tiles = [(i, j) for i in range(GRID_SIZE) for j in range(GRID_SIZE) if grid[i][j] == 0] if not empty_tiles: return evaluate(grid) for tile in empty_tiles: grid[tile[0]][tile[1]] = 2 total_score += 0.9 * expectimax(grid, depth - 1) grid[tile[0]][tile[1]] = 4 total_score += 0.1 * expectimax(grid, depth - 1) grid[tile[0]][tile[1]] = 0 return total_score / len(empty_tiles) def evaluate(grid): return sum(sum(row) for row in grid) ``` هل تحتاج إلى شرح لأي جزء من هذا الجزء من الكود؟