-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn.py
More file actions
280 lines (229 loc) · 7.22 KB
/
Copy pathcolumn.py
File metadata and controls
280 lines (229 loc) · 7.22 KB
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
import pygame
import random
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((6 * 50, 50 * 12))
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
DOWNEV = pygame.USEREVENT + 1
inter = 1000
pygame.time.set_timer(DOWNEV, inter)
mat = [[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
for i in range(14):
for j in range(6):
mat[i].append(0)
n = 4 # WARNING : THIS VARIABLE MUST BE FROM 3 UP TO 8
color_m1 = random.randint(1,n)
color_m2 = random.randint(1,n)
color_m3 = random.randint(1,n)
mat[0][2] = color_m1
mat[1][2] = color_m2
mat[2][2] = color_m3
sabz = 0
ghermez = 1
abi = 2
z = 1
stop = 0
idx = 0 #Left Or Right
score = 0
def tek():
global color_m1,color_m2,color_m3,mat,sabz,ghermez,abi,idx,inter,pygame,n,running
color_m1 = random.randint(1,n)
color_m2 = random.randint(1,n)
color_m3 = random.randint(1,n)
if mat[2][2] == 0:
mat[0][2] = color_m1
mat[1][2] = color_m2
mat[2][2] = color_m3
idx = 0
else:
if 0 not in mat[2]:
running = False
else:
rnd = random.randint(0,5)
while(mat[2][rnd] != 0):
rnd = random.randint(0,5)
mat[0][rnd] = color_m1
mat[1][rnd] = color_m2
mat[2][rnd] = color_m3
idx = rnd - 2
sabz = 0
ghermez = 1
abi = 2
inter = 1000
pygame.time.set_timer(DOWNEV, inter)
def check_delete(m):
result = False
for i in range(2,14):
for j in range(6):
if(m[i][j] != 0 and i<=11 and m[i][j] == m[i+1][j] and m[i+1][j] == m[i+2][j]):
result = True
if(m[i][j] != 0 and j<=3 and m[i][j] == m[i][j+1] and m[i][j+1] == m[i][j+2]):
result = True
if (m[i][j] != 0 and i<=11 and j<=3 and m[i][j] == m[i + 1][j + 1] and m[i + 1][j + 1] == m[i + 2][j + 2]):
result = True
if(m[i][j] != 0 and i>=4 and j<=3 and m[i][j] == m[i - 1][j + 1] and m[i - 1][j + 1] == m[i - 2][ j + 2]):
result = True
return result
def check_down(m):
for i in range(2,14):
for j in range(6):
if(m[i][j] != 0 and i<=12 and m[i+1][j] == 0):
return 1
return 0
def delete(m):
d = []
global score
k = 3
for i in range(2,14):
for j in range(6):
if(m[i][j] != 0 and i<=11 and m[i][j] == m[i+1][j] and m[i+1][j] == m[i+2][j]):
while(k + i <= 13 and m[i][j] == m[i + k][j]):
d.append([i+k,j])
k+=1
d.append([i,j])
d.append([i+1,j])
d.append([i+2,j])
score += k
k = 3
if(m[i][j] != 0 and j<=3 and m[i][j] == m[i][j+1] and m[i][j+1] == m[i][j+2]):
while(k + j <= 5 and m[i][j] == m[i][j + k]):
d.append([i,j+k])
k+=1
d.append([i,j])
d.append([i,j+1])
d.append([i,j+2])
score += k
k = 3
if(m[i][j] != 0 and i<=11 and j<=3 and m[i][j] == m[i + 1][j + 1] and m[i + 1][j + 1] == m[i + 2][ j + 2]):
while(i + k <=13 and k + j <= 5 and m[i][j] == m[i + k][j + k]):
d.append([i+k,j+k])
k += 1
d.append([i,j])
d.append([i+1,j+1])
d.append([i+2,j+2])
score += k
k = 3
if(m[i][j] != 0 and i<=11 and j>=2 and m[i][j] == m[i + 1][j - 1] and m[i + 1][j - 1] == m[i + 2][ j - 2]):
while(i + k <=13 and k + j >= 4 and m[i][j] == m[i + k][j - k]):
d.append([i+k,j-k])
k += 1
d.append([i,j])
d.append([i+1,j-1])
d.append([i+2,j-2])
score += k
k = 3
res = []
for i in d:
if i not in res:
res.append(i)
for i in res:
m[i[0]][i[1]] = 0
return m
def down(m):
for i in range(2,14):
for j in range(6):
if(m[i][j] != 0 and i<=12 and m[i+1][j] == 0):
(m[i][j] , m[i+1][j]) = (m[i+1][j] , m[i][j])
return m
running = True
while(running):
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_q:
running = False
if event.key == K_RIGHT:
if(idx < 3):
if(mat[abi][2 + idx + 1] == 0 and mat[ghermez][2 + idx + 1] == 0 and mat[sabz][2 + idx + 1] == 0):
(mat[abi][2 + idx] , mat[abi][2 + idx + 1]) = (mat[abi][2 + idx + 1], mat[abi][2 + idx])
(mat[sabz][2 + idx] , mat[sabz][2 + idx + 1]) = (mat[sabz][2 + idx + 1], mat[sabz][2 + idx])
(mat[ghermez][2 + idx] , mat[ghermez][2 + idx + 1]) = (mat[ghermez][2 + idx + 1], mat[ghermez][2 + idx])
idx += 1
if(abi < 13 and mat[abi + 1][2 + idx] != 0):
if(sabz == 1 or sabz == 0):
running = False
print("Cant Place")
if event.key == K_LEFT:
if(idx > -2):
if(mat[abi][2 + idx - 1] == 0 and mat[ghermez][2 + idx - 1] == 0 and mat[sabz][2 + idx - 1] == 0):
(mat[abi][2 + idx] , mat[abi][2 + idx - 1]) = (mat[abi][2 + idx - 1], mat[abi][2 + idx])
(mat[sabz][2 + idx] , mat[sabz][2 + idx - 1]) = (mat[sabz][2 + idx - 1], mat[sabz][2 + idx])
(mat[ghermez][2 + idx] , mat[ghermez][2 + idx - 1]) = (mat[ghermez][2 + idx - 1], mat[ghermez][2 + idx])
idx += -1
if(abi < 13 and mat[abi + 1][2 + idx] != 0):
if(sabz == 1 or sabz == 0):
running = False
print("Cant Place")
if event.key == K_DOWN:
inter = 50
pygame.time.set_timer(DOWNEV, inter)
if event.key == K_SPACE:
(mat[ghermez][2 + idx],mat[abi][2 + idx]) = (mat[abi][2 + idx],mat[ghermez][2 + idx])
(mat[ghermez][2 + idx],mat[sabz][2 + idx]) = (mat[sabz][2 + idx],mat[ghermez][2 + idx])
if event.key == K_s:
if(stop == 1):
stop = 0
else:
stop = 1
elif event.type == QUIT:
running = False
elif event.type == DOWNEV:
if(abi<13 and ghermez<13 and sabz <13):
if(mat[abi + 1][2 + idx] != 0 and mat[ghermez + 1][2 + idx] != 0 and mat[sabz + 1][2 + idx] != 0):
while(check_delete(mat) == 1 or check_down(mat) == 1):
mat = delete(mat)
mat = down(mat)
tek()
z = 1
#Go Down
if(mat[abi + 1][2 + idx] == 0 and z != 1 and stop == 0):
(mat[abi + 1][2 + idx] , mat[abi][2 + idx]) = (mat[abi][2 + idx] , mat[abi + 1][2 + idx])
abi+=1
if(mat[ghermez + 1][2 + idx] == 0 and z != 1 and stop == 0):
(mat[ghermez + 1][2 + idx] , mat[ghermez][2 + idx]) = (mat[ghermez][2 + idx] , mat[ghermez + 1][2 + idx])
ghermez+=1
if(mat[sabz + 1][2 + idx] == 0 and z != 1 and stop == 0):
(mat[sabz + 1][2 + idx] , mat[sabz][2 + idx]) = (mat[sabz][2 + idx] , mat[sabz + 1][2 + idx])
sabz+=1
#Check Not Finish Game
if(abi < 13 and mat[abi + 1][2 + idx] != 0):
if(sabz == 1 or sabz == 0):
running = False
print("Cant Place")
z = 0
else:
while(check_delete(mat) == 1 or check_down(mat) == 1):
mat = delete(mat)
mat = down(mat)
tek()
for i in range(2,14):
for j in range(6):
if(mat[i][j] != 0):
if(mat[i][j] == 1):
color=(0,255,0)
elif(mat[i][j] == 2):
color=(255,0,0)
elif(mat[i][j] == 3):
color=(0,0,255)
elif(mat[i][j] == 4):
color=(255,140,0)
elif(mat[i][j] == 5):
color=(255,0,255)
elif(mat[i][j] == 6):
color=(139,69,19)
elif(mat[i][j] == 7):
color=(255,255,0)
elif(mat[i][j] == 8):
color=(128,0,128)
pygame.draw.rect(screen,(color),(j*50,(i-2)*50,50,50))
pygame.display.flip()
screen.blit(background, (0, 0))
print()
for i in mat:
print(i)
print()
print()
print()
print("YOUR SCORE :",score)