-
Notifications
You must be signed in to change notification settings - Fork 206
/
2048.py
142 lines (121 loc) · 4.06 KB
/
2048.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pygame
import random
# Constants
GRID_SIZE = 4
CELL_SIZE = 100
GRID_COLOR = (187, 173, 160)
CELL_COLORS = {
0: (205, 193, 180),
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),
}
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE))
pygame.display.set_caption('2048 Game')
font = pygame.font.Font(None, 40)
# Game Functions
def init_game():
grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)]
add_random_tile(grid)
add_random_tile(grid)
return grid
def add_random_tile(grid):
empty_cells = [(i, j) for i in range(GRID_SIZE) for j in range(GRID_SIZE) if grid[i][j] == 0]
if empty_cells:
x, y = random.choice(empty_cells)
grid[x][y] = random.choice([2, 4])
def draw_grid(grid):
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))
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))
screen.blit(text, text_rect)
def move_left(grid):
new_grid = [[0] * GRID_SIZE for _ in range(GRID_SIZE)]
for x in range(GRID_SIZE):
position = 0
for y in range(GRID_SIZE):
if grid[x][y] != 0:
if new_grid[x][position] == 0:
new_grid[x][position] = grid[x][y]
elif new_grid[x][position] == grid[x][y]:
new_grid[x][position] *= 2
position += 1
else:
position += 1
new_grid[x][position] = grid[x][y]
return new_grid
def move_right(grid):
new_grid = [row[::-1] for row in grid]
new_grid = move_left(new_grid)
return [row[::-1] for row in new_grid]
def move_up(grid):
new_grid = [[grid[y][x] for y in range(GRID_SIZE)] for x in range(GRID_SIZE)]
new_grid = move_left(new_grid)
return [[new_grid[y][x] for y in range(GRID_SIZE)] for x in range(GRID_SIZE)]
def move_down(grid):
new_grid = [[grid[y][x] for y in range(GRID_SIZE)] for x in range(GRID_SIZE)]
new_grid = move_right(new_grid)
return [[new_grid[y][x] for y in range(GRID_SIZE)] for x in range(GRID_SIZE)]
def check_win(grid):
for row in grid:
if 2048 in row:
return True
return False
def check_game_over(grid):
for row in grid:
if 0 in row:
return False
for x in range(GRID_SIZE):
for y in range(GRID_SIZE - 1):
if grid[x][y] == grid[x][y + 1] or grid[y][x] == grid[y + 1][x]:
return False
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!')
pygame.display.flip()
if game_over or game_won:
pygame.time.wait(2000)
running = False
pygame.quit()