-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·384 lines (340 loc) · 12.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import random
from GiaoDien import *
from Define import *
def init_matrix(ds):
for i in range(4):
for j in range(4):
ds[i][j] = 0
for k in range(2):
add_block(ds)
def add_block(ds):
while True:
i = random.randint(0, 3)
j = random.randint(0, 3)
if (ds[i][j] == 0):
break
ds[i][j] = random.choice(VAL_CHOICE)
def output_ds(ds):
print("======================")
for i in range(4):
for j in range(4):
print(format(ds[i][j], "3d"), end=' ')
print()
def update_diem(plus):
global souce
souce += plus
pygame.draw.rect(menuScreen, BG_TEXT, (220, 10, 90, 40), )
font = pygame.font.SysFont('Bahnschrift', 15)
text = font.render(str(souce), True, (0, 0, 0))
menuScreen.blit(text, (220, 18))
def getbest():
file = open('BXH.txt')
best = int(file.readline())
pygame.draw.rect(menuScreen, BG_TEXT, (400, 10, 70, 40), )
font = pygame.font.SysFont('Bahnschrift', 15)
text = font.render(str(best), True, (0, 0, 0))
menuScreen.blit(text, (400, 18))
def move_left(ds):
dsdadichchuyen = []
codichchuyen = False
for i in range(4):
for j in range(1, 4):
if ds[i][j] == 0:
continue
for k in reversed(range(j)):
if (ds[i][k] != 0):
x = i
y = k
break
else:
x = i
y = j
if (x, y) == (i, j):
ds[i][0] = ds[x][y]
ds[x][y] = 0
codichchuyen = True
else:
if ds[i][j] == ds[x][y] and (x, y) not in dsdadichchuyen:
ds[x][y] *= 2
ds[i][j] = 0
dsdadichchuyen.append((x, y))
update_diem(ds[x][y])
codichchuyen = True
elif (y + 1 != j):
ds[x][y + 1] = ds[i][j]
ds[i][j] = 0
codichchuyen = True
return codichchuyen
def move_right(ds):
dsdadichchuyen = []
codichchuyen = False
for i in range(4):
for j in reversed(range(3)):
if ds[i][j] == 0:
continue
for k in range(j + 1, 4):
if (ds[i][k] != 0):
x = i
y = k
break
else:
x = i
y = j
if (x, y) == (i, j):
ds[i][3] = ds[x][y]
ds[x][y] = 0
codichchuyen = True
else:
if ds[i][j] == ds[x][y] and (x, y) not in dsdadichchuyen:
ds[x][y] *= 2
ds[i][j] = 0
dsdadichchuyen.append((x, y))
update_diem(ds[x][y])
codichchuyen = True
elif (y - 1 != j):
ds[x][y - 1] = ds[i][j]
ds[i][j] = 0
codichchuyen = True
return codichchuyen
def move_up(ds):
dsdadichchuyen = []
codichchuyen = False
for j in range(4):
for i in range(1, 4):
if ds[i][j] == 0:
continue
for k in reversed(range(i)):
if (ds[k][j] != 0):
x = k
y = j
break
else:
x = i
y = j
if (x, y) == (i, j):
ds[0][j] = ds[x][y]
ds[x][y] = 0
codichchuyen = True
else:
if ds[i][j] == ds[x][y] and (x, y) not in dsdadichchuyen:
ds[x][y] *= 2
ds[i][j] = 0
dsdadichchuyen.append((x, y))
update_diem(ds[x][y])
codichchuyen = True
elif (x + 1 != i):
ds[x + 1][j] = ds[i][j]
ds[i][j] = 0
codichchuyen = True
return codichchuyen
def move_down(ds):
dsdadichchuyen = []
codichchuyen = False
for j in range(4):
for i in reversed(range(3)):
if ds[i][j] == 0:
continue
for k in range(i + 1, 4):
if (ds[k][j] != 0):
x = k
y = j
break
else:
x = i
y = j
if (x, y) == (i, j):
ds[3][j] = ds[x][y]
ds[x][y] = 0
codichchuyen = True
else:
if ds[i][j] == ds[x][y] and (x, y) not in dsdadichchuyen:
ds[x][y] *= 2
ds[i][j] = 0
dsdadichchuyen.append((x, y))
update_diem(ds[x][y])
codichchuyen = True
elif (x - 1 != i):
ds[x - 1][j] = ds[i][j]
ds[i][j] = 0
codichchuyen = True
return codichchuyen
def cannot_move(ds):
for i in range(4):
for j in range(4):
if ds[i][j] == 0:
return False
if i > 0:
if ds[i - 1][j] == ds[i][j]:
return False
if i < 3:
if ds[i + 1][j] == ds[i][j]:
return False
if j > 0:
if ds[i][j - 1] == ds[i][j]:
return False
if j < 3:
if ds[i][j + 1] == ds[i][j]:
return False
return True
def isFull(ds):
for i in range(4):
for j in range(4):
if (ds[i][j] == 0):
return False
return True
def isWin(ds):
for i in range(4):
for j in range(4):
if (ds[i][j] == 2048):
return True
return False
def updateBXH(souce):
files = open('BXH.txt')
list = files.readlines()
if (len(list) == 10 and int(list[len(list) - 1]) >= souce):
return
files = open('BXH.txt', 'w')
list.append(str(souce) + '\n')
list = [int(i) for i in list]
list.sort(reverse=True)
if len(list) > 10:
del list[10:len(list)]
list = [str(i) + '\n' for i in list]
files.writelines(list)
def hover_move(menuScreen: pygame.Surface, mouse: pygame.mouse, modes):
if modes["Play"] == False and modes["Home"] == True:
if WIDTH / 2 - 125 <= mouse[0] <= (WIDTH / 2 - 125 + 250) and 300 <= mouse[1] <= 350:
pygame.draw.rect(menuScreen, BG_BLOCK2, (WIDTH / 2 - 125, 300, 250, 50), border_radius=8)
playgame(menuScreen)
else:
pygame.draw.rect(menuScreen, BG_BLOCK, (WIDTH / 2 - 125, 300, 250, 50), border_radius=8)
playgame(menuScreen)
if WIDTH / 2 - 125 <= mouse[0] <= (WIDTH / 2 - 125 + 250) and 370 <= mouse[1] <= 420:
pygame.draw.rect(menuScreen, BG_BLOCK2, (WIDTH / 2 - 125, 370, 250, 50), border_radius=8)
rank(menuScreen)
else:
pygame.draw.rect(menuScreen, BG_BLOCK, (WIDTH / 2 - 125, 370, 250, 50), border_radius=8)
rank(menuScreen)
if WIDTH / 2 - 125 <= mouse[0] <= (WIDTH / 2 - 125 + 250) and 440 <= mouse[1] <= 490:
pygame.draw.rect(menuScreen, BG_BLOCK2, (WIDTH / 2 - 125, 440, 250, 50), border_radius=8)
drawquit(menuScreen)
else:
pygame.draw.rect(menuScreen, BG_BLOCK, (WIDTH / 2 - 125, 440, 250, 50), border_radius=8)
drawquit(menuScreen)
if __name__ == '__main__':
ds = [[0 for i in range(4)] for j in range(4)]
souce = 0
init_matrix(ds)
# ds = [[32, 4, 0, 0], [512, 256, 8, 0], [2048, 64, 4, 0], [128, 32, 2, 2]]
output_ds(ds)
pygame.init()
menuScreen = initWindown()
setMenuScreen(menuScreen)
drawsound(menuScreen)
pygame.display.update()
clock = pygame.time.Clock()
audio = pygame.mixer.Sound
musicHome = audio('assets/music/SoundTheme.mp3')
musicGameOver = audio('./assets/music/MusicGameOver.mp3')
musicWin = audio('./assets/music/MusicWin.mp3')
isplaymusic = True;
musicHome.play(10)
game_over = False
win = False
running = True
modes = {"Play": False,
"Home": True,
"Bxh": False
}
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if WIDTH / 2 - 125 <= mouse[0] <= (WIDTH / 2 - 125 + 250) and 300 <= mouse[1] <= 350 and modes[
"Home"] == True:
background(menuScreen)
thanhcongcu(menuScreen)
drawBlock(menuScreen, ds)
modes["Home"] = False
modes["Play"] = True
update_diem(0)
getbest()
if game_over:
drawgameover(menuScreen)
musicHome.stop()
if 14 <= mouse[0] <= 54 and 15 <= mouse[1] <= 55 and modes[
"Home"] == True:
if (isplaymusic):
isplaymusic = False
drawmute(menuScreen)
musicHome.stop()
else:
isplaymusic = True
drawsound(menuScreen)
musicHome.play(10)
if WIDTH / 2 - 125 <= mouse[0] <= (WIDTH / 2 - 125 + 250) and 370 <= mouse[1] <= 420 and modes[
"Home"] == True:
bangxephang(menuScreen)
modes["Home"] = False
modes["Bxh"] = True
if WIDTH / 2 - 125 <= mouse[0] <= (WIDTH / 2 - 125 + 250) and 440 <= mouse[1] <= 490 and modes[
"Home"] == True:
running = False
if 22 <= mouse[0] <= 66 and 12 <= mouse[1] <= 52 and modes["Home"] == False:
if game_over and modes["Play"] and isplaymusic:
musicHome.play()
modes["Play"] = False
setMenuScreen(menuScreen)
if (isplaymusic):
drawsound(menuScreen)
else:
drawmute(menuScreen)
modes["Home"] = True
if 69 <= mouse[0] <= 109 and 10 <= mouse[1] <= 50 and modes["Play"] == True:
if game_over:
if (isplaymusic):
musicHome.play()
game_over = False
souce = 0
init_matrix(ds)
update_diem(0)
getbest()
background(menuScreen)
drawBlock(menuScreen, ds)
pygame.display.update()
if event.type == pygame.KEYDOWN and modes["Play"] == True and not game_over:
dichuyen = False
if event.key == pygame.K_LEFT:
dichuyen = move_left(ds)
elif event.key == pygame.K_RIGHT:
dichuyen = move_right(ds)
elif event.key == pygame.K_UP:
dichuyen = move_up(ds)
elif event.key == pygame.K_DOWN:
dichuyen = move_down(ds)
if dichuyen == False:
continue
if not isFull(ds):
add_block(ds)
output_ds(ds)
drawmh(menuScreen, ds)
if isWin(ds) and win == False:
if (isplaymusic):
musicWin.play()
draw_win(menuScreen)
drawmh(menuScreen, ds)
win = True
if cannot_move(ds):
if (isplaymusic):
musicHome.stop()
musicGameOver.play()
game_over = True
drawgameover(menuScreen)
updateBXH(souce)
else:
continue
mouse = pygame.mouse.get_pos()
hover_move(menuScreen, mouse, modes)
pygame.display.update()