-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
278 lines (231 loc) · 9.68 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
import pygame,math,random,time, copy
from Case import Case
BLOCK_WIDTH = 200
BLOCK_HEIGHT = 200
GRID_CORNER_LEFT_X = 500
GRID_CORNER_LEFT_Y = 100
GRID_GAP = 15
BUTTON_QUIT_X = 100
BUTTON_QUIT_Y = 900
BUTTON_QUIT_WIDTH = 200
BUTTON_QUIT_HEIGTH = 100
BUTTON_START_X = 100
BUTTON_START_Y = 700
BUTTON_START_WIDTH = 200
BUTTON_START_HEIGTH = 100
IMAGE_SOUND_X = 150
IMAGE_SOUND_Y = 500
IMAGE_WIDTH = 100
IMAGE_HEIGHT = 100
LISTE_COLOR = ["lightgrey","#eee4da","#ede0c8",'#f2b179',"#f59563","#f67c5f","#f65e3b","#edcf72","#edcc61","#edc850","#edc53f","#edc22e"]
pygame.init()
SIZE = width, height = pygame.display.Info().current_w, pygame.display.Info().current_h
SCREEN = pygame.display.set_mode(SIZE)
img = pygame.transform.scale(pygame.image.load("sound.png").convert(),(IMAGE_WIDTH,IMAGE_HEIGHT))
music_on = True
tab = [[Case(), Case(), Case(), Case()] for _ in range(4)]
tab_old = copy.deepcopy(tab)
def main():
pygame.mixer.Channel(0).play(pygame.mixer.Sound("background.mp3"),loops=-1)
draw_grid()
spaw_case()
draw_buttons()
update_grid()
SCREEN.blit(img, (IMAGE_SOUND_X, IMAGE_SOUND_Y))
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
check_click_quit_button()
check_click_start_button()
check_click_image_sound()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN :
tab_move_down()
if event.key == pygame.K_RIGHT:
tab_move_right()
if event.key == pygame.K_LEFT:
tab_move_left()
if event.key == pygame.K_UP:
tab_move_up()
pygame.display.flip()
def draw_grid():
SCREEN.fill("white")
background_rect = pygame.Rect(GRID_CORNER_LEFT_X,GRID_CORNER_LEFT_Y,BLOCK_WIDTH*4+GRID_GAP*5,BLOCK_HEIGHT*4+GRID_GAP*5)
pygame.draw.rect(SCREEN,"black",background_rect)
for i in range(4):
for j in range(4):
tab[i][j].hasMerged = False
rect = pygame.Rect(GRID_CORNER_LEFT_X+i*(BLOCK_WIDTH)+(i+1)*GRID_GAP,GRID_CORNER_LEFT_Y+j*(BLOCK_HEIGHT)+(j+1)*GRID_GAP,BLOCK_WIDTH,BLOCK_HEIGHT)
pygame.draw.rect(SCREEN,LISTE_COLOR[int(math.log2(tab[i][j].val))],rect)
def update_grid():
global tab_old
for i in range(4):
for j in range(4):
tab[i][j].hasMerged = False
if (tab_old[i][j].val != tab[i][j].val):
rect = pygame.Rect(GRID_CORNER_LEFT_X+i*(BLOCK_WIDTH)+(i+1)*GRID_GAP,GRID_CORNER_LEFT_Y+j*(BLOCK_HEIGHT)+(j+1)*GRID_GAP,BLOCK_WIDTH,BLOCK_HEIGHT)
pygame.draw.rect(SCREEN,LISTE_COLOR[int(math.log2(tab[i][j].val))],rect)
if (tab[i][j].val != 1):
font = pygame.font.SysFont("consolas",50)
text = font.render(str(tab[i][j].val), True, (0,0,0))
text_rect = text.get_rect(center=(GRID_CORNER_LEFT_X+(i+1/2)*(BLOCK_WIDTH)+(i+1)*GRID_GAP,GRID_CORNER_LEFT_Y+(j+1/2)*(BLOCK_HEIGHT)+(j+1)*GRID_GAP))
SCREEN.blit(text, text_rect)
tab_old[i][j].val = tab[i][j].val
pygame.display.flip()
def draw_cases_value():
for i in range(4):
for j in range(4):
if (tab[i][j].val != 1):
font = pygame.font.SysFont("consolas",50)
text = font.render(str(tab[i][j].val), True, (0,0,0))
text_rect = text.get_rect(center=(GRID_CORNER_LEFT_X+(i+1/2)*(BLOCK_WIDTH)+(i+1)*GRID_GAP,GRID_CORNER_LEFT_Y+(j+1/2)*(BLOCK_HEIGHT)+(j+1)*GRID_GAP))
SCREEN.blit(text, text_rect)
def draw_buttons():
font = pygame.font.Font(pygame.font.get_default_font(),36)
background_rect_quit = pygame.Rect(BUTTON_QUIT_X,BUTTON_QUIT_Y,BUTTON_QUIT_WIDTH,BUTTON_QUIT_HEIGTH)
pygame.draw.rect(SCREEN,"grey",background_rect_quit)
text_surface_quit = font.render("Quit", True, (0, 0, 0))
SCREEN.blit(text_surface_quit, dest=(BUTTON_QUIT_X+BUTTON_QUIT_WIDTH/3.3,BUTTON_QUIT_Y+BUTTON_QUIT_HEIGTH/3))
background_rect_start = pygame.Rect(BUTTON_START_X,BUTTON_START_Y,BUTTON_START_WIDTH,BUTTON_START_HEIGTH)
pygame.draw.rect(SCREEN,"grey",background_rect_start)
text_surface_start = font.render("Start", True, (0, 0, 0))
SCREEN.blit(text_surface_start, dest=(BUTTON_START_X+BUTTON_START_WIDTH/3.3,BUTTON_START_Y+BUTTON_START_HEIGTH/3))
def spaw_case():
update_grid()
if check_tab_full():
return
i, j = random.randint(0,3),random.randint(0,3)
while (tab[i][j].val != 1):
i, j = random.randint(0,3),random.randint(0,3)
val = (4)if(random.randint(0,2) == 2)else(2)
print("Val : ",val)
for k in range(40):
rect = pygame.Rect(GRID_CORNER_LEFT_X+(i+1/2)*(BLOCK_WIDTH)+(i+1)*GRID_GAP-BLOCK_WIDTH/2*k/40,GRID_CORNER_LEFT_Y+(j+1/2)*(BLOCK_HEIGHT)+(j+1)*GRID_GAP-BLOCK_HEIGHT/2*k/40,(BLOCK_WIDTH)*k/40,(BLOCK_HEIGHT)*k/40)
pygame.draw.rect(SCREEN,LISTE_COLOR[int(math.log2(val))],rect)
draw_buttons()
pygame.display.flip()
tab[i][j].val = val
update_grid()
def check_click_quit_button():
mouse_x, mouse_y = pygame.mouse.get_pos()
if (mouse_x >= BUTTON_QUIT_X and mouse_x <= (BUTTON_QUIT_X + BUTTON_QUIT_WIDTH) and mouse_y >= BUTTON_QUIT_Y and mouse_y <= (BUTTON_QUIT_Y + BUTTON_QUIT_HEIGTH)):
pygame.quit()
def check_click_start_button():
mouse_x, mouse_y = pygame.mouse.get_pos()
if (mouse_x >= BUTTON_START_X and mouse_x <= (BUTTON_START_X + BUTTON_START_WIDTH) and mouse_y >= BUTTON_START_Y and mouse_y <= (BUTTON_START_Y + BUTTON_START_HEIGTH)):
clear_tab()
draw_grid()
spaw_case()
def check_click_image_sound():
global music_on
mouse_x, mouse_y = pygame.mouse.get_pos()
if (mouse_x >= IMAGE_SOUND_X and mouse_x <= (IMAGE_SOUND_X + IMAGE_WIDTH) and mouse_y >= (IMAGE_SOUND_Y) and mouse_y <= (IMAGE_SOUND_Y + IMAGE_HEIGHT)) :
if music_on :
pygame.mixer.Channel(0).pause()
path = "sound_off.jpg"
print("I'm pausing")
else :
pygame.mixer.Channel(0).unpause()
path = "sound.png"
print("I'm will play")
draw_grid()
draw_buttons()
draw_cases_value()
music_on = not(music_on)
img = pygame.transform.scale(pygame.image.load(path).convert(),(IMAGE_WIDTH,IMAGE_HEIGHT))
SCREEN.blit(img, (IMAGE_SOUND_X, IMAGE_SOUND_Y))
def clear_tab():
for i in range(4):
for j in range(4):
tab[i][j].val = 1
def check_tab_full():
for i in range(4):
for j in range(4):
if tab[i][j].val == 1 :
return False
return True
def tab_move_right():
Test = False
for i in range(2,-1,-1):
for j in range(4):
indice = i
while indice < 3 :
if tab[indice][j].can_merge(tab[indice+1][j]):
merge_two_cells(tab[indice][j],tab[indice+1][j])
indice = indice + 1
Test = True
elif tab[indice][j].val != 1 and tab[indice+1][j].val == 1:
tab[indice][j].val,tab[indice+1][j].val = tab[indice+1][j].val, tab[indice][j].val
indice = indice + 1
Test = True
else :
indice = 3
if Test:
spaw_case()
def tab_move_left():
Test = False
for i in range(1,4):
for j in range(4):
indice = i
while indice >= 1 :
if tab[indice][j].can_merge(tab[indice-1][j]):
merge_two_cells(tab[indice][j],tab[indice-1][j])
indice = indice - 1
Test = True
elif tab[indice][j].val != 1 and tab[indice-1][j].val == 1:
tab[indice][j].val,tab[indice-1][j].val = tab[indice-1][j].val, tab[indice][j].val
indice = indice - 1
Test = True
else :
indice = 0
if Test:
spaw_case()
def tab_move_up():
Test = False
for i in range(4):
for j in range(1,4):
indice = j
while indice >= 1 :
if tab[i][indice].can_merge(tab[i][indice-1]):
merge_two_cells(tab[i][indice],tab[i][indice-1])
indice = indice - 1
Test = True
elif tab[i][indice].val != 1 and tab[i][indice-1].val == 1:
tab[i][indice-1].val,tab[i][indice].val = tab[i][indice].val, tab[i][indice-1].val
indice = indice - 1
Test = True
else :
indice = 0
if Test :
spaw_case()
def tab_move_down():
Test = False
for i in range(4):
for j in range(2,-1,-1):
indice = j
while indice < 3 :
if tab[i][indice].can_merge(tab[i][indice+1]):
merge_two_cells(tab[i][indice],tab[i][indice+1])
indice = indice + 1
Test = True
elif tab[i][indice].val != 1 and tab[i][indice+1].val == 1:
tab[i][indice+1].val,tab[i][indice].val = tab[i][indice].val, tab[i][indice+1].val
indice = indice + 1
Test = True
else :
indice = 3
if Test :
spaw_case()
def merge_two_cells(cell,cell_dest):
cell_dest.val = cell_dest.val * 2
cell_dest.hasMerged = True
cell.val = 1
def print_tab():
print("Tab : ")
for i in range(4):
line = ""
for j in range(4):
line = line + " " + str(tab[j][i].val) + " "
print(line)
if __name__ == "__main__":
main()