-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.py
238 lines (198 loc) · 7.96 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import pygame
import random
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BACKGROUND = (187, 173, 160)
# Game settings
GRID_SIZE = 4
TILE_SIZE = 100
GRID_MARGIN = 10
# Tile colors
TILE_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),
4096: (238, 228, 218),
8192: (242, 177, 121),
16384: (245, 149, 99),
32768: (246, 124, 95),
65536: (246, 94, 59),
}
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode(((TILE_SIZE + GRID_MARGIN) * GRID_SIZE + GRID_MARGIN,
(TILE_SIZE + GRID_MARGIN) * GRID_SIZE + GRID_MARGIN))
pygame.display.set_caption("2048")
font = pygame.font.Font(None, 36)
large_font = pygame.font.Font(None, 48)
# Game Logic Functions
def initialize_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:
row, col = random.choice(empty_cells)
grid[row][col] = 2 if random.random() < 0.9 else 4
def move_and_merge(grid, direction):
moved = False
if direction == "up":
for col in range(GRID_SIZE):
merged_col = move_and_merge_row_or_col([grid[row][col] for row in range(GRID_SIZE)])
for row in range(GRID_SIZE):
if grid[row][col] != merged_col[row]:
moved = True
grid[row][col] = merged_col[row]
elif direction == "down":
for col in range(GRID_SIZE):
merged_col = move_and_merge_row_or_col([grid[row][col] for row in reversed(range(GRID_SIZE))])
for row in range(GRID_SIZE - 1, -1, -1):
if grid[row][col] != merged_col[GRID_SIZE - 1 - row]:
moved = True
grid[row][col] = merged_col[GRID_SIZE - 1 - row]
elif direction == "left":
for row in range(GRID_SIZE):
merged_row = move_and_merge_row_or_col(grid[row])
for col in range(GRID_SIZE):
if grid[row][col] != merged_row[col]:
moved = True
grid[row][col] = merged_row[col]
elif direction == "right":
for row in range(GRID_SIZE):
merged_row = move_and_merge_row_or_col(grid[row][::-1])
for col in range(GRID_SIZE - 1, -1, -1):
if grid[row][col] != merged_row[GRID_SIZE - 1 - col]:
moved = True
grid[row][col] = merged_row[GRID_SIZE - 1 - col]
return moved
def move_and_merge_row_or_col(row_or_col):
new_row_or_col = [x for x in row_or_col if x != 0]
i = 0
while i < len(new_row_or_col) - 1:
if new_row_or_col[i] == new_row_or_col[i + 1]:
new_row_or_col[i] *= 2
del new_row_or_col[i + 1]
i += 1
new_row_or_col += [0] * (GRID_SIZE - len(new_row_or_col))
return new_row_or_col
def move_up(grid):
return move_and_merge(grid, "up")
def move_down(grid):
return move_and_merge(grid, "down")
def move_left(grid):
return move_and_merge(grid, "left")
def move_right(grid):
return move_and_merge(grid, "right")
def game_over(grid):
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
if grid[row][col] == 0:
return False
if col < GRID_SIZE - 1 and grid[row][col] == grid[row][col + 1]:
return False
if row < GRID_SIZE - 1 and grid[row][col] == grid[row + 1][col]:
return False
return True
# Drawing Functions
def draw_grid(grid):
screen.fill(BACKGROUND)
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
value = grid[row][col]
color = TILE_COLORS[value]
pygame.draw.rect(screen,
color,
[(GRID_MARGIN + TILE_SIZE) * col + GRID_MARGIN,
(GRID_MARGIN + TILE_SIZE) * row + GRID_MARGIN,
TILE_SIZE,
TILE_SIZE])
if value != 0:
text = font.render(str(value), True, WHITE)
text_rect = text.get_rect(center=((GRID_MARGIN + TILE_SIZE) * col + GRID_MARGIN + TILE_SIZE // 2,
(GRID_MARGIN + TILE_SIZE) * row + GRID_MARGIN + TILE_SIZE // 2))
screen.blit(text, text_rect)
def draw_button(text, x, y, width, height, color, hover_color, text_color=BLACK, action=None):
mouse_pos = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()[0]
if x < mouse_pos[0] < x + width and y < mouse_pos[1] < y + height:
pygame.draw.rect(screen, hover_color, (x, y, width, height))
if click and action is not None:
action()
else:
pygame.draw.rect(screen, color, (x, y, width, height))
text_surface = font.render(text, True, text_color)
text_rect = text_surface.get_rect(center=((x + width // 2), (y + height // 2)))
screen.blit(text_surface, text_rect)
def draw_start_screen():
screen.fill(BACKGROUND)
text = large_font.render("2048", True, WHITE)
text_rect = text.get_rect(center=(screen.get_width() // 2, screen.get_height() // 2 - 50))
screen.blit(text, text_rect)
button_width = 150
button_height = 50
button_x = screen.get_width() // 2 - button_width // 2
button_y = screen.get_height() // 2 + 50
draw_button("Start Game", button_x, button_y, button_width, button_height, WHITE, (200, 200, 200), action=start_game)
pygame.display.flip()
def draw_game_over_screen():
screen.fill(BACKGROUND)
text = large_font.render("Game Over", True, WHITE)
text_rect = text.get_rect(center=(screen.get_width() // 2, screen.get_height() // 2 - 50))
screen.blit(text, text_rect)
button_width = 100
button_height = 50
button_x = screen.get_width() // 2 - button_width // 2 - 100
button_y = screen.get_height() // 2 + 50
draw_button("Restart", button_x, button_y, button_width, button_height, WHITE, (200, 200, 200), action=start_game)
button_x = screen.get_width() // 2 - button_width // 2 + 100
draw_button("Quit", button_x, button_y, button_width, button_height, WHITE, (200, 200, 200), action=quit_game)
pygame.display.flip()
# Main game loop
def start_game():
global grid, game_state
grid = initialize_game()
game_state = "play"
def quit_game():
global running
running = False
game_state = "start"
grid = None
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if game_state == "play" and event.type == pygame.KEYDOWN:
if event.key in (pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT): # Check if arrow key
if event.key == pygame.K_UP:
moved = move_up(grid)
elif event.key == pygame.K_DOWN:
moved = move_down(grid)
elif event.key == pygame.K_LEFT:
moved = move_left(grid)
elif event.key == pygame.K_RIGHT:
moved = move_right(grid)
if moved:
add_random_tile(grid)
if game_over(grid):
game_state = "game_over"
if game_state == "start":
draw_start_screen()
elif game_state == "play":
draw_grid(grid)
pygame.display.flip()
elif game_state == "game_over":
draw_game_over_screen()
pygame.quit()