import pygame import sys # Initialize Pygame pygame.init() # Set up the screen SCREEN_WIDTH = 400 SCREEN_HEIGHT = 500 GRID_SIZE = 4 TILE_SIZE = 100 GRID_COLOR = (187, 173, 160) BACKGROUND_COLOR = (250, 248, 239) FONT_COLOR = (119, 110, 101) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("2048 Game") # Define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Define fonts font = pygame.font.Font(None, 36) # Define directions UP = 'up' DOWN = 'down' LEFT = 'left' RIGHT = 'right' # Function to draw grid lines def draw_grid(): for i in range(GRID_SIZE + 1): pygame.draw.line(screen, GRID_COLOR, (0, i * TILE_SIZE), (SCREEN_WIDTH, i * TILE_SIZE), 4) pygame.draw.line(screen, GRID_COLOR, (i * TILE_SIZE, 0), (i * TILE_SIZE, SCREEN_HEIGHT - 100), 4) # Function to draw tiles def draw_tiles(grid): for i in range(GRID_SIZE): for j in range(GRID_SIZE): if grid[i][j] != 0: color = get_tile_color(grid[i][j]) pygame.draw.rect(screen, color, (j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE)) text = font.render(str(grid[i][j]), True, FONT_COLOR) screen.blit(text, (j * TILE_SIZE + TILE_SIZE // 2 - text.get_width() // 2, i * TILE_SIZE + TILE_SIZE // 2 - text.get_height() // 2)) # Function to get tile color based on value def get_tile_color(value): colors = { 2: (238, 228, 218), 4: (237, 224, 200), 8: (242, 177, 121), 16: (245, 149, 99), 32: (246, 124, 95), 64: (246, 94, 59), 128: (237, 207, 114), 256: (237, 204, 97), 512: (237, 200, 80), 1024: (237, 197, 63), 2048: (237, 194, 46) } return colors.get(value, (255, 255, 255)) # Function to move tiles def move(grid, direction): # Implement movement logic here pass # Function to add a new tile def add_new_tile(grid): # Implement logic to add a new tile here pass # Function to initialize the game def initialize_game(): grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)] for _ in range(2): # Add initial tiles add_new_tile(grid) return grid # Main game loop def main(): grid = initialize_game() draw_grid() draw_tiles(grid) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: grid = move(grid, LEFT) elif keys[pygame.K_RIGHT]: grid = move(grid, RIGHT) elif keys[pygame.K_UP]: grid = move(grid, UP) elif keys[pygame.K_DOWN]: grid = move(grid, DOWN) draw_grid() draw_tiles(grid) pygame.display.update() if __name__ == "__main__": main()