-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.py
552 lines (502 loc) · 24.3 KB
/
Tetris.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import os
import random
import sys, subprocess
import time
from pytimedinput import timedInput
def next_figure_fun(next_figure):
if next_figure == "Z-l":
draw = "[]\n[] []\n []"
elif next_figure == "Z-r":
draw = " []\n[] []\n[] "
elif next_figure == "C":
draw = "[] []\n[] []"
elif next_figure == "I":
draw = "[] [] [] []"
elif next_figure == "T":
draw = "[] [] []\n []"
elif next_figure == "G-l":
draw = "[] []\n[]\n[]"
elif next_figure == "G-r":
draw = "[] []\n []\n []"
else:
draw = ""
return draw
def field_strike_checker(saver_field):
global points, HEIGHT
new_field = saver_field
counter_id = 0
position = []
for i in saver_field:
strike = True
for i2 in i:
if i2 == 0:
strike = False
break
if strike:
points += 1
position.append(counter_id)
counter_id += 1
if len(position) > 0:
new_field = []
for i2 in position:
new_field.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
counter = 0
for kolumn in saver_field:
if counter < i2:
new_field.append(kolumn)
elif counter > i2:
new_field.append(kolumn)
counter += 1
saver_field = new_field
new_field = []
new_field = saver_field
return new_field
else:
return new_field
def action_checker(button, cords, type_figure, field, position_type):
global cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1
if button == 'a':
available = True
for cord in cords:
for x in cord:
if not cord[x] >= 1 or field[x][cord[x] - 1]:
return False, 0, position_type
if available:
return True, -1, position_type
time.sleep(3)
elif button == 'd':
available = True
for cord in cords:
for x in cord:
if not cord[x] < 9 or field[x][cord[x] + 1]:
return False, 0, position_type
if available:
return True, 1, position_type
# right()
elif button == ' ':
if type_figure == "I":
for cord in cords:
for x in cord:
if position_type == "I-0": # Checker
if cord_1 >= HEIGHT - 1 or field[cord_1 + 1][cord_1_1 + 1] == 1 or field[cord_3 - 1][
cord_3_1 - 1] == 1 or field[cord_4 - 2][cord_4_1 - 2] == 1:
return False, 0, "I-0"
else:
cord_1, cord_1_1 = cord_1 + 1, cord_1_1 + 1
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3 - 1, cord_3_1 - 1
cord_4, cord_4_1 = cord_4 - 2, cord_4_1 - 2
return True, 0, "I-1"
elif position_type == "I-1":
if cord_4 >= HEIGHT - 1 or field[cord_1 - 2][cord_1_1 - 1] == 1 or field[cord_2 - 1][
cord_2_1] == 1 or field[cord_3][cord_3_1 + 1] == 1 or field[cord_4 + 1][cord_4_1 + 2] == 1:
return False, 0, "I-1"
else:
cord_1, cord_1_1 = cord_1 - 2, cord_1_1 - 1
cord_2, cord_2_1 = cord_2 - 1, cord_2_1
cord_3, cord_3_1 = cord_3, cord_3_1 + 1
cord_4, cord_4_1 = cord_4 + 1, cord_4_1 + 2
return True, 0, "I-2"
elif position_type == "I-2": # Checker
if field[cord_1 - 2][cord_1_1 + 2] == 1 or field[cord_2 - 1][cord_2_1 + 1] == 1 or \
field[cord_4 + 1][cord_4_1 - 1] == 1:
return False, 0, "I-2"
else:
cord_1, cord_1_1 = cord_1 - 2, cord_1_1 + 2
cord_2, cord_2_1 = cord_2 - 1, cord_2_1 + 1
cord_3, cord_3_1 = cord_3, cord_3_1
cord_4, cord_4_1 = cord_4 + 1, cord_4_1 - 1
return True, 0, "I-3"
elif position_type == "I-3": # Checker
if field[cord_1 + 1][cord_1_1 - 2] == 1 or field[cord_2][cord_2_1 - 1] == 1 or \
field[cord_4 - 2][cord_4_1 + 1] == 1:
return False, 0, "I-3"
else:
cord_1, cord_1_1 = cord_1 + 3, cord_1_1 - 2
cord_2, cord_2_1 = cord_2 + 2, cord_2_1 - 1
cord_3, cord_3_1 = cord_3 + 1, cord_3_1
cord_4, cord_4_1 = cord_4, cord_4_1 + 1
return True, 0, "I-0"
if type_figure == "Z-r":
for cord in cords:
for x in cord:
if position_type == "Z-r-0": # Checker
if field[cord_1][cord_1_1 + 2] == 1:
return False, 0, "Z-r-0"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 + 2
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3, cord_3_1
cord_4, cord_4_1 = cord_4 - 2, cord_4_1
return True, 0, "Z-r-1"
else:
if cord_4 >= HEIGHT - 2 or field[cord_1][cord_1_1 - 2] == 1:
return False, 0, "Z-r-1"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 - 2
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3, cord_3_1
cord_4, cord_4_1 = cord_4 + 2, cord_4_1
return True, 0, "Z-r-0"
if type_figure == "Z-l":
for cord in cords:
for x in cord:
if position_type == "Z-l-0": # Checker
if cord_4 >= HEIGHT - 2 or field[cord_3][cord_3_1 + 2] == 1:
return False, 0, "Z-l-0"
else:
cord_1, cord_1_1 = cord_1, cord_1_1
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3, cord_3_1 + 2
cord_4, cord_4_1 = cord_4 - 2, cord_4_1
return True, 0, "Z-l-1"
else:
if cord_4 >= HEIGHT - 1 or field[cord_3 - 1][cord_3_1 - 2] == 1:
return False, 0, "Z-l-1"
else:
cord_1, cord_1_1 = cord_1 - 1, cord_1_1
cord_2, cord_2_1 = cord_2 - 1, cord_2_1
cord_3, cord_3_1 = cord_3 - 1, cord_3_1 - 2
cord_4, cord_4_1 = cord_4 + 1, cord_4_1
return True, 0, "Z-l-0"
if type_figure == "G-l":
for cord in cords:
for x in cord:
if position_type == "G-l-0": # Checker 0, 0+2, 0+1, 1+1, 1-1, 0+1, 2-2, 0
if cord_2 >= HEIGHT - 1 or field[cord_1][cord_1_1 + 2] == 1 or field[cord_2 + 1][
cord_2_1 + 1] == 1 or field[cord_3 - 1][cord_3_1 + 1] == 1:
return False, 0, "G-l-0"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 + 2
cord_2, cord_2_1 = cord_2 + 1, cord_2_1 + 1
cord_3, cord_3_1 = cord_3 - 1, cord_3_1 + 1
cord_4, cord_4_1 = cord_4 - 2, cord_4_1
return True, 0, "G-l-1"
elif position_type == "G-l-1": # Checker
if cord_4 >= HEIGHT - 1 or field[cord_2][cord_2_1 - 2] == 1 or field[cord_4 + 1][
cord_4_1 + 1] == 1:
return False, 0, "G-l-1"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 - 1
cord_2, cord_2_1 = cord_2, cord_2_1 - 2
cord_3, cord_3_1 = cord_3 - 1, cord_3_1
cord_4, cord_4_1 = cord_4 + 1, cord_4_1 + 1
return True, 0, "G-l-2"
elif position_type == "G-l-2": # Checker
if cord_3 >= HEIGHT - 2 or field[cord_1][cord_1_1 - 1] == 1 or field[cord_4][cord_4_1 + 1] == 1:
return False, 0, "G-l-2"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 - 1
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3 + 2, cord_3_1
cord_4, cord_4_1 = cord_4, cord_4_1 + 1
return True, 0, "G-l-3"
elif position_type == "G-l-3": # Checker 0, 0+2, 0+1, 1+1, 1-1, 0+1, 2-2, 0
if field[cord_3 - 1][cord_3_1 - 1] == 1 or field[cord_2 - 2][cord_2_1 + 1] == 1 or \
field[cord_4][cord_4_1 - 2] == 1:
return False, 0, "G-l-3"
else:
cord_1, cord_1_1 = cord_1 - 1, cord_1_1
cord_2, cord_2_1 = cord_2 - 2, cord_2_1 + 1
cord_3, cord_3_1 = cord_3 - 1, cord_3_1 - 1
cord_4, cord_4_1 = cord_4, cord_4_1 - 2
return True, 0, "G-l-0"
if type_figure == "G-r":
for cord in cords:
for x in cord:
if position_type == "G-r-0": # Checker 0, 0+2, 0+1, 1+1, 1-1, 0+1, 2-2, 0
try:
if cord_1 >= HEIGHT - 1 or field[cord_1 + 1][cord_1_1 - 1] == 1 or field[cord_2][
cord_2_1 - 2] == 1 or field[cord_3 - 1][cord_3_1 - 1] == 1:
return False, 0, "G-r-0"
else:
cord_1, cord_1_1 = cord_1 + 1, cord_1_1 - 1
cord_2, cord_2_1 = cord_2, cord_2_1 - 2
cord_3, cord_3_1 = cord_3 - 1, cord_3_1 - 1
cord_4, cord_4_1 = cord_4 - 2, cord_4_1
return True, 0, "G-r-1"
except:
print("Error")
elif position_type == "G-r-1": # Checker
try:
if cord_2 >= HEIGHT - 1 or field[cord_1][cord_1_1 + 2] == 1 or field[cord_2 + 1][
cord_2_1 + 1] == 1 or field[cord_4 - 1][cord_4_1 - 1] == 1:
return False, 0, "G-r-1"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 + 2
cord_2, cord_2_1 = cord_2 + 1, cord_2_1 + 1
cord_3, cord_3_1 = cord_3, cord_3_1
cord_4, cord_4_1 = cord_4 - 1, cord_4_1 - 1
return True, 0, "G-r-2"
except:
print("Error")
elif position_type == "G-r-2": # Checker
try:
if cord_4 >= HEIGHT - 2 or field[cord_1 - 1][cord_1_1 + 1] == 1 or field[cord_2][
cord_2_1 + 2] == 1 or field[cord_3 + 1][cord_3_1 + 1] == 1:
return False, 0, "G-r-2"
else:
cord_1, cord_1_1 = cord_1 - 1, cord_1_1 + 1
cord_2, cord_2_1 = cord_2, cord_2_1 + 2
cord_3, cord_3_1 = cord_3 + 1, cord_3_1 + 1
cord_4, cord_4_1 = cord_4 + 2, cord_4_1
return True, 0, "G-r-3"
except:
print("Error")
elif position_type == "G-r-3": # Checker 0, 0+2, 0+1, 1+1, 1-1, 0+1, 2-2, 0
try:
if field[cord_1 - 1][cord_1_1 - 1] == 1 or field[cord_3 - 1][cord_3_1 + 1] == 1 or \
field[cord_4][cord_4_1 + 2] == 1:
return False, 0, "G-r-3"
else:
cord_1, cord_1_1 = cord_1 - 1, cord_1_1 - 1
cord_2, cord_2_1 = cord_2 - 2, cord_2_1
cord_3, cord_3_1 = cord_3 - 1, cord_3_1 + 1
cord_4, cord_4_1 = cord_4, cord_4_1 + 2
return True, 0, "G-r-0"
except:
print("Error")
if type_figure == "T":
for cord in cords:
for x in cord:
if position_type == "T-0":
if cord_1 >= HEIGHT - 1:
return False, 0, "T-0"
else:
cord_1, cord_1_1 = cord_1 + 1, cord_1_1 + 1
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3, cord_3_1
cord_4, cord_4_1 = cord_4 - 2, cord_4_1
return True, 0, "T-1"
elif position_type == "T-1": # Checker
if cord_4 >= HEIGHT - 2 or field[cord_3 + 1][cord_3_1 - 2] == 1 or field[cord_4 + 2][
cord_4_1] == 1 or field[cord_1][cord_1_1 + 1] == 1:
return False, 0, "T-1"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 + 1
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3 + 1, cord_3_1 - 2
cord_4, cord_4_1 = cord_4 + 2, cord_4_1
return True, 0, "T-2"
elif position_type == "T-2": # Checker
if cord_3 >= HEIGHT - 1 or field[cord_1][cord_1_1 - 2] == 1 or field[cord_3 + 1][
cord_3_1 + 1] == 1:
return False, 0, "T-2"
else:
cord_1, cord_1_1 = cord_1, cord_1_1 - 2
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3 + 1, cord_3_1 + 1
cord_4, cord_4_1 = cord_4, cord_4_1
return True, 0, "T-3"
elif position_type == "T-3": # Checker 0, 1,0,2,0,3,1,2
if field[cord_3 - 2][cord_3_1 + 1] == 1:
return False, 0, "T-3"
else:
cord_1, cord_1_1 = cord_1 - 1, cord_1_1
cord_2, cord_2_1 = cord_2, cord_2_1
cord_3, cord_3_1 = cord_3 - 2, cord_3_1 + 1
cord_4, cord_4_1 = cord_4, cord_4_1
return True, 0, "T-0"
if type_figure == "C":
return False, 0, "C"
time.sleep(3)
# change()
elif button == 's':
return True, -10, position_type
# down()
def main():
global HEIGHT, points
field = [[0 for _ in range(10)] for _ in range(15)]
HEIGHT = 15
points = 0
list_of_figures = ["G-l", "G-r", "Z-l", "Z-r", "C", "T", "I"] # "I",
random_figure = random.choice(list_of_figures)
random_figure_2 = random.choice(list_of_figures)
random_figure_3 = random.choice(list_of_figures)
while True:
next_figure = [random_figure, random_figure_2, random_figure_3]
field_1 = run_game(HEIGHT, field, random_figure, next_figure)
if not field_1:
print(f"The end. Your score:{points}")
break
random_figure = random.choice(list_of_figures)
next_figure = [random_figure_2, random_figure_3, random_figure]
field_2 = run_game(HEIGHT, field_1, random_figure_2, next_figure)
if not field_2:
print(f"The end. Your score:{points}")
break
random_figure_2 = random.choice(list_of_figures)
next_figure = [random_figure_3, random_figure, random_figure_2]
field = run_game(HEIGHT, field_2, random_figure_3, next_figure)
if not field:
print(f"The end. Your score:{points}")
break
# new_field = run_game(HEIGHT, field, random_figure)
#
#
# if not new_field:
# print(f"The end. Your score:{points}")
# break
#
# field = run_game(HEIGHT, new_field, random_figure_2)
# if not field:
# print(f"The end. Your score:{points}")
# break
def run_game(HEIGHT, field, type_figure, next_figure):
global position_type, cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1, points
next_is_break = False
saver_field = []
action_list = []
move = False
if type_figure == "Z-r":
position_type = "Z-r-0"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 1, 4, 1, 5, 0, 5, 2, 4
elif type_figure == "Z-l":
position_type = "Z-l-0"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 1, 4, 1, 5, 0, 4, 2, 5
elif type_figure == "G-l":
position_type = "G-l-0"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 0, 4, 0, 5, 1, 4, 2, 4
elif type_figure == "G-r":
position_type = "G-r-0"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 0, 4, 0, 5, 1, 5, 2, 5
elif type_figure == "T":
position_type = "T-0"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 0, 3, 0, 4, 0, 5, 1, 4
elif type_figure == "I":
position_type = "I-0"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 0, 2, 0, 3, 0, 4, 0, 5
else:
position_type = "C"
cord_1, cord_1_1, cord_2, cord_2_1, cord_3, cord_3_1, cord_4, cord_4_1 = 0, 4, 0, 5, 1, 4, 1, 5
stop = False
unlow_move = True
for i in range(HEIGHT + 1):
os.system('cls')
counter = 0
if not next_is_break:
print(f"Your's points score:{points}")
for i2 in field:
if counter == 0:
if field[cord_1][cord_1_1] == 1 or field[cord_2][cord_2_1] == 1 or field[cord_3][cord_3_1] == 1 or field[cord_4][cord_4_1] == 1:
next_is_break = True
stop = True
break
cords = [{cord_1: cord_1_1}, {cord_2: cord_2_1}, {cord_3: cord_3_1}, {cord_4: cord_4_1}]
if move and unlow_move:
if len(action_list) == 0:
check, action, position_type = action_checker(button, cords, type_figure, field, position_type)
else:
for i in action_list:
check, action, position_type = action_checker(i, cords, type_figure, field, position_type)
action_list = []
if check:
if action == -10:
unlow_move = False
time.sleep(0.5)
else:
cord_1_1 = cord_1_1 + action
cord_2_1 = cord_2_1 + action
cord_3_1 = cord_3_1 + action
cord_4_1 = cord_4_1 + action
button = ""
move = False
for cord in cords:
for x in cord:
if x == counter:
field[counter][cord[x]] = 1
if counter != HEIGHT - 1:
if field[counter + 1][cord[x]] != 1:
pass
else:
next_is_break = True
elif counter == HEIGHT - 1:
next_is_break = True
break
# Printing file
for i3 in i2:
if i3 == 0:
print("--", end=" ")
else:
print("[]", end=" ")
print()
# Create a scale of last dict
for cord in cords:
for x in cord:
if x == counter:
if next_is_break:
break
else:
field[counter][cord[x]] = 0
counter += 1
else:
# Save last snapshot of dict
cord_1 -= 1
cord_2 -= 1
cord_3 -= 1
cord_4 -= 1
saver_field = []
for i2 in field:
cords = [{cord_1: cord_1_1}, {cord_2: cord_2_1}, {cord_3: cord_3_1}, {cord_4: cord_4_1}]
for cord in cords:
for x in cord:
if x == counter:
field[counter][cord[x]] = 1
copy_of_field = i2
saver_field.append(copy_of_field)
# Create a scale of last dict
for cord in cords:
for x in cord:
if x == counter:
if next_is_break:
break
else:
field[counter][cord[x]] = 0
counter += 1
if next_is_break: # Exit of loop
os.system('cls') # cls
break
if not stop:
print("Next figure will be:\n")
print("1.")
print(next_figure_fun(next_figure[1]))
print()
print("2.")
print(next_figure_fun(next_figure[2]))
cord_1 += 1
cord_2 += 1
cord_3 += 1
cord_4 += 1
if unlow_move:
if points <= 5:
button, _ = timedInput("", timeout=0.55)
elif points <= 15:
button, _ = timedInput("", timeout=0.35)
elif points <= 25:
button, _ = timedInput("", timeout=0.2)
else:
button, _ = timedInput("", timeout=0.1)
if button == "aaa" or button == "aa" or button == "aaaa":
button = "a"
move = True
if button == "ddd" or button == "dd" or button == "dddd":
button = "d"
move = True
if button == " a":
move = True
action_list.append(" ")
action_list.append("a")
elif button == " d":
move = True
action_list.append(" ")
action_list.append("d")
if button == "a" or button == " " or button == "s" or button == "d":
move = True
if stop:
return []
else:
saver_field = field_strike_checker(saver_field)
return saver_field
if __name__ == '__main__':
main()
# python C:\Python\Projects\OwnProjects\GeniusGames\Tetris.py