2048 source code
import pygame
import random
GRID_SIZE = 4
TILE_SIZE = 100
TILE_MARGIN = 10
WIDTH = GRID_SIZE * (TILE_SIZE + TILE_MARGIN) + TILE_MARGIN
HEIGHT = WIDTH
FPS = 60
BACKGROUND_COLOR = (187, 173, 160)
EMPTY_TILE_COLOR = (205, 192, 180)
TILE_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)
}
TEXT_COLOR = (119, 110, 101)
FONT_SIZE = 55
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(“2048”)
clock = pygame.time.Clock()
font = pygame.font.Font(None, FONT_SIZE)
class Game2048:
def __init__(self):
self.grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)]
self.spawn_tile()
self.spawn_tile()
def spawn_tile(self):
empty_tiles = [(r, c) for r in range(GRID_SIZE) for c in range(GRID_SIZE) if self.grid[r][c] == 0]
if not empty_tiles:
return
r, c = random.choice(empty_tiles)
self.grid[r][c] = 4 if random.random() > 0.9 else 2
def move_left(self):
moved = False
for row in self.grid:
tight_row = [tile for tile in row if tile != 0]
merged_row = []
skip = False
for i in range(len(tight_row)):
if skip:
skip = False
continue
if i + 1 < len(tight_row) and tight_row[i] == tight_row[i + 1]: merged_row.append(tight_row[i] * 2) skip = True moved = True else: merged_row.append(tight_row[i]) merged_row.extend([0] * (GRID_SIZE – len(merged_row))) if row != merged_row: moved = True row[:] = merged_row if moved: self.spawn_tile() def move_right(self): for row in self.grid: row.reverse() self.move_left() for row in self.grid: row.reverse() def move_up(self): self.grid = [list(row) for row in zip(*self.grid)] self.move_left() self.grid = [list(row) for row in zip(*self.grid)] def move_down(self): self.grid = [list(row) for row in zip(*self.grid)] self.move_right() self.grid = [list(row) for row in zip(*self.grid)] def is_game_over(self): for r in range(GRID_SIZE): for c in range(GRID_SIZE): if self.grid[r][c] == 0: return False if c + 1 < GRID_SIZE and self.grid[r][c] == self.grid[r][c + 1]: return False if r + 1 < GRID_SIZE and self.grid[r][c] == self.grid[r + 1][c]: return False return True def draw(self, surface): surface.fill(BACKGROUND_COLOR) for r in range(GRID_SIZE): for c in range(GRID_SIZE): value = self.grid[r][c] color = TILE_COLORS.get(value, EMPTY_TILE_COLOR) pygame.draw.rect( surface, color, (c * (TILE_SIZE + TILE_MARGIN) + TILE_MARGIN, r * (TILE_SIZE + TILE_MARGIN) + TILE_MARGIN, TILE_SIZE, TILE_SIZE) ) if value != 0: text = font.render(str(value), True, TEXT_COLOR) text_rect = text.get_rect(center=( c * (TILE_SIZE + TILE_MARGIN) + TILE_MARGIN + TILE_SIZE / 2, r * (TILE_SIZE + TILE_MARGIN) + TILE_MARGIN + TILE_SIZE / 2 )) surface.blit(text, text_rect) pygame.display.flip() def main(): game = Game2048() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: game.move_left() elif event.key == pygame.K_RIGHT: game.move_right() elif event.key == pygame.K_UP: game.move_up() elif event.key == pygame.K_DOWN: game.move_down() if game.is_game_over(): print(“Game Over! Press any key to exit.”) pygame.time.wait(3000) running = False game.draw(screen) clock.tick(FPS) pygame.quit() if __name__ == “__main__”: main()
Leave a Reply