This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
373 lines (328 loc) · 10.6 KB
/
index.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
from tkinter import Tk, Canvas, PhotoImage, ALL
from itertools import product
from random import randrange
from pieces import FORMES
SIZE = 700
TAILLE_CARRE = SIZE//20 - 1
X0 = SIZE//2-5*TAILLE_CARRE
Y0 = SIZE//2-10*TAILLE_CARRE
COLORS=["gray60", None, "red", "yellow", "green", "blue", "purple", "orange", "brown", "white","gray20"]
GrilleDeJeu = []
score = 0
level = 0
futurePieceNumber = randrange(7)
IDs = []
x = 0
y = 0
game_started = False
after_id = None
time = 0
def afficher(x, y, pieceNumber, GrilleDeJeu):
global IDs
posX0 = X0+x*TAILLE_CARRE
posY0 = Y0+y*TAILLE_CARRE
if GrilleDeJeu[y][x] == 1:
color = COLORS[pieceNumber+2]
else:
color = COLORS[GrilleDeJeu[y][x]]
IDs.append(cnv.create_rectangle(posX0, posY0, posX0+TAILLE_CARRE, posY0+TAILLE_CARRE, fill=color))
def update_affichage(GrilleDeJeu, pieceNumber):
global IDs, textScore, futurePieceNumber
for x in IDs:
cnv.delete(x)
IDs = []
for i,j in product(range(20), range(10)):
afficher(j, i, pieceNumber, GrilleDeJeu)
scoreText = "Score : " + str(score)
cnv.itemconfig(textScore, text=scoreText)
for i,j in product(range(len(FORMES[futurePieceNumber][0])), range(len(FORMES[futurePieceNumber][0][0]))):
if FORMES[futurePieceNumber][0][i][j] == 1:
IDs.append(cnv.create_rectangle(10+TAILLE_CARRE*j, 400+TAILLE_CARRE*i, 10+TAILLE_CARRE*(j+1), 400+TAILLE_CARRE*(i+1), fill=COLORS[futurePieceNumber+2]))
cnv.update()
def init_piece(GrilleDeJeu, piece):
global x,y
x = 3
y = 0
for i in range(len(piece[0])):
for j in range(len(piece)):
if piece[i][j] == 1:
if GrilleDeJeu[i][j+x] > 2:
return GrilleDeJeu, False
GrilleDeJeu[i][j+x] = 1
return GrilleDeJeu, True
def descente(GrilleDeJeu, pieceNumber):
global y
oldGrille = []
for i in range(20):
L = []
for j in range(10):
L.append(GrilleDeJeu[i][j])
oldGrille.append(L)
end = False
for i,j in product(range(20), range(10)):
i = 19-i
if i == 19 and GrilleDeJeu[i][j] == 1:
end = True
if not end and GrilleDeJeu[i][j] == 1:
if 10 > GrilleDeJeu[i+1][j] > 1:
end = True
else:
GrilleDeJeu[i+1][j] = 1
GrilleDeJeu[i][j] = 0
if end:
GrilleDeJeu = oldGrille
for i,j in product(range(20), range(10)):
if GrilleDeJeu[i][j] == 1:
GrilleDeJeu[i][j] = pieceNumber+2
return False, GrilleDeJeu
y+=1
return True, GrilleDeJeu
def ombre(GrilleDeJeu, pieceNumber):
global y
oldY = y
etat= False
Ombre = []
Ngdj = []
for i in range(20):
L = []
for j in range(10):
if GrilleDeJeu[i][j]==1:
etat = True
if GrilleDeJeu[i][j]==10:
L.append(0)
GrilleDeJeu[i][j] = 0
else:
L.append(GrilleDeJeu[i][j])
Ngdj.append(L)
Ombre.append(L)
if not etat:
return GrilleDeJeu
while etat:
etat,Ngdj=descente(Ngdj, pieceNumber)
if etat:
Ombre = []
for i in range(20):
L = []
for j in range(10):
L.append(Ngdj[i][j])
Ombre.append(L)
for i,j in product(range(20), range(10)):
if Ombre[i][j] == 1 and GrilleDeJeu[i][j] != 1:
GrilleDeJeu[i][j]=10
y = oldY
return GrilleDeJeu
def init_game():
global GrilleDeJeu, score
GrilleDeJeu = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]
update_affichage(GrilleDeJeu, 0)
score = 0
def game():
init_game()
global etatPiece, imageDepart, game_started
cnv.delete(imageDepart)
etatPiece = False
game_started = True
cnv_in_game()
def cnv_in_game():
global GrilleDeJeu, pieceNumber, etatPiece, pieceRotation, futurePieceNumber, after_id, time, score
time = 500 - (50 * level//10)
if not etatPiece:
GrilleDeJeu = check_ligne_complete(GrilleDeJeu)
pieceRotation = 0
pieceNumber = futurePieceNumber
futurePieceNumber = randrange(7)
piece = FORMES[pieceNumber]
GrilleDeJeu, jeu = init_piece(GrilleDeJeu, piece[0])
if not jeu:
cnv.create_text(SIZE//2, SIZE//2, text="Game Over", font=('Helvetica', 50), fill='white')
cnv.create_text(SIZE//2, 100,text="Your score is: "+str(score), font=('Helvetica', 20), fill='black')
update_score(score)
return
GrilleDeJeu = ombre(GrilleDeJeu, pieceNumber)
update_affichage(GrilleDeJeu, pieceNumber)
after_id = cnv.after(time, cnv_in_game)
etatPiece = True
else:
etatPiece, GrilleDeJeu = descente(GrilleDeJeu, pieceNumber)
GrilleDeJeu = ombre(GrilleDeJeu, pieceNumber)
update_affichage(GrilleDeJeu, pieceNumber)
if not etatPiece:
cnv_in_game()
else:
after_id = cnv.after(time, cnv_in_game)
def update_score(score):
global high_score_label
hs = open("high_scores.txt")
line= hs.read()
scores = line.split("\n")
for i in range(len(scores)):
scores[i] = int(scores[i])
scores.append(score)
scores.sort(reverse=True)
scores.pop()
hs.close()
hs = open("high_scores.txt", "w")
for i in range(len(scores)):
if i ==len(scores)-1:
scores[i] = str(scores[i])
else:
scores[i] = str(scores[i])+"\n"
hs.writelines(scores)
hs.close()
hs = open("high_scores.txt")
line= hs.read()
cnv.delete(high_score_label)
cnv.create_text(SIZE//2+6*TAILLE_CARRE, 100, text=line, font=('Helvetica', '16'), anchor='nw')
def cancel():
global after_id
if after_id is not None:
cnv.after_cancel(after_id)
after_id = None
def check_ligne_complete(GrilleDeJeu):
global score, level
session = 0
for i in range(20):
state = True
for j in range(10):
if GrilleDeJeu[i][j] == 0:
state = False
break
if state:
session+=1
level+=1
for j in range(10):
GrilleDeJeu[i][j] = 9
update_affichage(GrilleDeJeu, pieceNumber)
for j in range(10):
GrilleDeJeu[i][j] = 0
for ii in range(i-1, -1, -1):
for j in range(10):
GrilleDeJeu[ii+1][j] = GrilleDeJeu[ii][j]
update_affichage(GrilleDeJeu, pieceNumber)
if session == 1:
score += 40 * (level//10 + 1)
elif session == 2:
score += 100 * (level//10 + 1)
elif session == 3:
score += 300 * (level//10 + 1)
elif session == 4:
score += 1200 * (level//10 + 1)
return GrilleDeJeu
def clic(event):
global game_started
if not game_started:
game()
def changement(GrilleDeJeu):
global pieceRotation, x, y
oldGrille = []
for i in range(20):
L = []
for j in range(10):
L.append(GrilleDeJeu[i][j])
oldGrille.append(L)
for i,j in product(range(20), range(10)):
if GrilleDeJeu[i][j] == 1:
GrilleDeJeu[i][j] = 0
if len(FORMES[pieceNumber])-1 == pieceRotation:
nouvelleRotation = 0
else:
nouvelleRotation = pieceRotation + 1
nouvellePiece = FORMES[pieceNumber][nouvelleRotation]
for i,j in product(range(len(nouvellePiece)), range(len(nouvellePiece[0]))):
if nouvellePiece[i][j] == 1:
if x+j>=0 and x+j<10 and GrilleDeJeu[y+i][x+j] < 2:
GrilleDeJeu[y+i][x+j] = 1
else:
return oldGrille
pieceRotation = nouvelleRotation
return ombre(GrilleDeJeu, pieceNumber)
def move(sens, GrilleDeJeu):
global x, game_started
if not game_started:
return
oldGrille = []
for i in range(20):
L = []
for j in range(10):
if GrilleDeJeu[i][j] == 10:
L.append(0)
else:
L.append(GrilleDeJeu[i][j])
oldGrille.append(L)
if sens == 0:
for i,j in product(range(20), range(10)):
if GrilleDeJeu[i][j] == 1:
if j == 0:
return oldGrille
if GrilleDeJeu[i][j-1] > 0:
return oldGrille
GrilleDeJeu[i][j-1] = 1
GrilleDeJeu[i][j] = 0
else:
for i,j in product(range(19, -1, -1), range(9, -1, -1)):
if GrilleDeJeu[i][j] == 1:
if j == 9:
return oldGrille
if GrilleDeJeu[i][j+1] > 0:
return oldGrille
GrilleDeJeu[i][j+1] = 1
GrilleDeJeu[i][j] = 0
if sens == 0:
x -= 1
else:
x += 1
GrilleDeJeu = ombre(GrilleDeJeu, pieceNumber)
return GrilleDeJeu
def touches(event):
t = event.keysym
global GrilleDeJeu, etatPiece, after_id, time
if t == "Left":
GrilleDeJeu = move(0, GrilleDeJeu)
update_affichage(GrilleDeJeu, pieceNumber)
elif t == "Right":
GrilleDeJeu = move(1, GrilleDeJeu)
update_affichage(GrilleDeJeu, pieceNumber)
elif t == "Up":
GrilleDeJeu = changement(GrilleDeJeu)
update_affichage(GrilleDeJeu, pieceNumber)
elif t == "Down":
if not etatPiece:
return
#cancel()
etatPiece, GrilleDeJeu = descente(GrilleDeJeu, pieceNumber)
#after_id = cnv.after(time, cnv_in_game)
update_affichage(GrilleDeJeu, pieceNumber)
root = Tk()
cnv = Canvas(root, width=SIZE, height=SIZE, background="white")
cnv.pack()
startImg = PhotoImage(file="assets/start.gif")
imageDepart = cnv.create_image(SIZE//2, SIZE//2, image=startImg)
textScore = cnv.create_text(10, 100, text="", font=('Helvetica', '20'), anchor='nw')
high_score = open("high_scores.txt", "r")
high_score_text = high_score.read()
high_score.close()
high_score_label = cnv.create_text(SIZE//2+6*TAILLE_CARRE, 100, text=high_score_text, font=('Helvetica', '16'), anchor='nw')
root.bind("<Button>", clic)
root.bind("<Key>", touches)
root.mainloop()