-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
256 lines (200 loc) · 7.05 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import pygame
import random
import math
pygame.init()
FPS = 60
WIDTH, HEIGHT = 800, 800
ROWS = 4
COLS = 4
RECT_HEIGHT = HEIGHT // ROWS
RECT_WIDTH = WIDTH // COLS
OUTLINE_COLOR = (187, 173, 160)
OUTLINE_THICKNESS = 10
BACKGROUND_COLOR = (205, 192, 180)
FONT_COLOR = (119, 110, 101)
FONT = pygame.font.SysFont("comicsans", 60, bold=True)
MOVE_VEL = 20
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2048")
class Tile:
COLORS = [
(237, 229, 218),
(238, 225, 201),
(243, 178, 122),
(246, 150, 101),
(247, 124, 95),
(247, 95, 59),
(237, 208, 115),
(237, 204, 99),
(236, 202, 80),
]
def __init__(self, value, row, col):
self.value = value
self.row = row
self.col = col
self.x = col * RECT_WIDTH
self.y = row * RECT_HEIGHT
def get_color(self):
color_index = int(math.log2(self.value)) - 1
color = self.COLORS[color_index]
return color
def draw(self, window):
color = self.get_color()
pygame.draw.rect(window, color, (self.x, self.y, RECT_WIDTH, RECT_HEIGHT))
text = FONT.render(str(self.value), 1, FONT_COLOR)
window.blit(
text,
(
self.x + (RECT_WIDTH / 2 - text.get_width() / 2),
self.y + (RECT_HEIGHT / 2 - text.get_height() / 2),
),
)
def set_pos(self, ceil=False):
if ceil:
self.row = math.ceil(self.y / RECT_HEIGHT)
self.col = math.ceil(self.x / RECT_WIDTH)
else:
self.row = math.floor(self.y / RECT_HEIGHT)
self.col = math.floor(self.x / RECT_WIDTH)
def move(self, delta):
self.x += delta[0]
self.y += delta[1]
def draw_grid(window):
for row in range(1, ROWS):
y = row * RECT_HEIGHT
pygame.draw.line(window, OUTLINE_COLOR, (0, y), (WIDTH, y), OUTLINE_THICKNESS)
for col in range(1, COLS):
x = col * RECT_WIDTH
pygame.draw.line(window, OUTLINE_COLOR, (x, 0), (x, HEIGHT), OUTLINE_THICKNESS)
pygame.draw.rect(window, OUTLINE_COLOR, (0, 0, WIDTH, HEIGHT), OUTLINE_THICKNESS)
def draw(window, tiles):
window.fill(BACKGROUND_COLOR)
for tile in tiles.values():
tile.draw(window)
draw_grid(window)
pygame.display.update()
def get_random_pos(tiles):
row = None
col = None
while True:
row = random.randrange(0, ROWS)
col = random.randrange(0, COLS)
if f"{row}{col}" not in tiles:
break
return row, col
def move_tiles(window, tiles, clock, direction):
updated = True
blocks = set()
if direction == "left":
sort_func = lambda x: x.col
reverse = False
delta = (-MOVE_VEL, 0)
boundary_check = lambda tile: tile.col == 0
get_next_tile = lambda tile: tiles.get(f"{tile.row}{tile.col - 1}")
merge_check = lambda tile, next_tile: tile.x > next_tile.x + MOVE_VEL
move_check = (
lambda tile, next_tile: tile.x > next_tile.x + RECT_WIDTH + MOVE_VEL
)
ceil = True
elif direction == "right":
sort_func = lambda x: x.col
reverse = True
delta = (MOVE_VEL, 0)
boundary_check = lambda tile: tile.col == COLS - 1
get_next_tile = lambda tile: tiles.get(f"{tile.row}{tile.col + 1}")
merge_check = lambda tile, next_tile: tile.x < next_tile.x - MOVE_VEL
move_check = (
lambda tile, next_tile: tile.x + RECT_WIDTH + MOVE_VEL < next_tile.x
)
ceil = False
elif direction == "up":
sort_func = lambda x: x.row
reverse = False
delta = (0, -MOVE_VEL)
boundary_check = lambda tile: tile.row == 0
get_next_tile = lambda tile: tiles.get(f"{tile.row - 1}{tile.col}")
merge_check = lambda tile, next_tile: tile.y > next_tile.y + MOVE_VEL
move_check = (
lambda tile, next_tile: tile.y > next_tile.y + RECT_HEIGHT + MOVE_VEL
)
ceil = True
elif direction == "down":
sort_func = lambda x: x.row
reverse = True
delta = (0, MOVE_VEL)
boundary_check = lambda tile: tile.row == ROWS - 1
get_next_tile = lambda tile: tiles.get(f"{tile.row + 1}{tile.col}")
merge_check = lambda tile, next_tile: tile.y < next_tile.y - MOVE_VEL
move_check = (
lambda tile, next_tile: tile.y + RECT_HEIGHT + MOVE_VEL < next_tile.y
)
ceil = False
while updated:
clock.tick(FPS)
updated = False
sorted_tiles = sorted(tiles.values(), key=sort_func, reverse=reverse)
for i, tile in enumerate(sorted_tiles):
if boundary_check(tile):
continue
next_tile = get_next_tile(tile)
if not next_tile:
tile.move(delta)
elif (
tile.value == next_tile.value
and tile not in blocks
and next_tile not in blocks
):
if merge_check(tile, next_tile):
tile.move(delta)
else:
next_tile.value *= 2
sorted_tiles.pop(i)
blocks.add(next_tile)
elif move_check(tile, next_tile):
tile.move(delta)
else:
continue
tile.set_pos(ceil)
updated = True
update_tiles(window, tiles, sorted_tiles)
return end_move(tiles)
def end_move(tiles):
if len(tiles) == 16:
return "lost"
row, col = get_random_pos(tiles)
tiles[f"{row}{col}"] = Tile(random.choice([2, 4]), row, col)
return "continue"
def update_tiles(window, tiles, sorted_tiles):
tiles.clear()
for tile in sorted_tiles:
tiles[f"{tile.row}{tile.col}"] = tile
draw(window, tiles)
def generate_tiles():
tiles = {}
for _ in range(2):
row, col = get_random_pos(tiles)
tiles[f"{row}{col}"] = Tile(2, row, col)
return tiles
def main(window):
clock = pygame.time.Clock()
run = True
tiles = generate_tiles()
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_tiles(window, tiles, clock, "left")
if event.key == pygame.K_RIGHT:
move_tiles(window, tiles, clock, "right")
if event.key == pygame.K_UP:
move_tiles(window, tiles, clock, "up")
if event.key == pygame.K_DOWN:
move_tiles(window, tiles, clock, "down")
draw(window, tiles)
pygame.quit()
if __name__ == "__main__":
main(WINDOW)