-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
774 lines (728 loc) · 30.5 KB
/
Copy pathmain.py
File metadata and controls
774 lines (728 loc) · 30.5 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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
import arcade,random,math,PIL,time
# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 900
SCREEN_TITLE = "PyTetris"
GRID_WIDTH = 10
GRID_HEIGHT = 20
CELL_SIZE = 30
FPS = 60.0
# Display Constants
NEXT_SZ=0.9
HOLD_SZ=0.8
LANE_SZ=0.7
# DAS settings
DAS_DELAY = 0.15 # seconds before auto-shift starts
ARR_INTERVAL = 0.05 # seconds between auto-shifts
SOFT_ARR_INTERVAL = 1/30.0
MASTER_TIMINGS = [
((0, 499), {'are':25, 'line_are':25, 'das':14, 'lock':30, 'line_clear':40}),
((500, 599), {'are':25, 'line_are':25, 'das':8, 'lock':26, 'line_clear':25}),
((600, 699), {'are':25, 'line_are':16, 'das':8, 'lock':26, 'line_clear':16}),
((700, 799), {'are':16, 'line_are':12, 'das':8, 'lock':26, 'line_clear':12}),
((800, 899), {'are':12, 'line_are':6, 'das':8, 'lock':26, 'line_clear':6}),
((900, 999), {'are':12, 'line_are':6, 'das':6, 'lock':17, 'line_clear':6}),
((1000,1099), {'are':6, 'line_are':6, 'das':6, 'lock':17, 'line_clear':6}),
((1100,1199), {'are':5, 'line_are':5, 'das':6, 'lock':15, 'line_clear':6}),
((1200,9999), {'are':4, 'line_are':4, 'das':6, 'lock':15, 'line_clear':6}),
]
CR_TIMINGS = [
[(0, 70, 99), {"COOL":52,"REGRET":90},False,False,False],
[(100, 170, 199), {"COOL":52,"REGRET":75},False,False,False],
[(200, 270, 299), {"COOL":49,"REGRET":75},False,False,False],
[(300, 370, 399), {"COOL":45,"REGRET":68},False,False,False],
[(400, 470, 499), {"COOL":45,"REGRET":60},False,False,False],
[(500, 570, 599), {"COOL":42,"REGRET":60},False,False,False],
[(600, 670, 699), {"COOL":42,"REGRET":50},False,False,False],
[(700, 770, 799), {"COOL":38,"REGRET":50},False,False,False],
[(800, 870, 899), {"COOL":38,"REGRET":50},False,False,False],
[(900, 900, 999), {"COOL": 0,"REGRET":50},False,False,False],
]
GRADE=[str(i)for i in range(10,0,-1)]
GRADE+=[f"S{i}"for i in range(11)]
GRADE+=[f"m{i}"for i in range(11)]
GRADE+=["M","MK","MV","MO","MM-","MM","MM+","GM-","GM","GM+","TM-","TM","TM+","ΩM","ΣM","∞M","∞M+"]
DECAY_RATE=[125,80,80,50,50,50,45,45,45,45,40,40,40,40,40,30,30,30,30,30,20,20,20,20,20,15,15,15,15,15,15,15,15,15,15,10,10,10,10,9,9,9,8,8,8,7,7,7,6]
GRAVITY = [
(( 0, 29), 1024),
(( 30, 34), 1536),
(( 35, 39), 2048),
(( 40, 49), 2560),
(( 50, 59), 3072),
(( 60, 69), 4096),
(( 70, 79), 8192),
(( 80, 89), 12288),
(( 90, 99), 16384),
((100, 119), 20480),
((120, 139), 24576),
((140, 159), 28672),
((160, 169), 32768),
((170, 199), 36864),
((200, 219), 1024),
((220, 229), 8192),
((230, 232), 16384),
((233, 235), 24576),
((236, 238), 32768),
((239, 242), 40960),
((243, 246), 49152),
((247, 250), 57344),
((251, 299), 65536),
((300, 329), 131072),
((330, 359), 196608),
((360, 399), 262144),
((400, 419), 327680),
((420, 449), 262144),
((450, 499), 196608),
((500, 999), 1310720),
]
# Colors
COLORS = {
'I': (78,172,231),
'O': (248,211,72),
'T': (135,48,137),
'S': (137,192,85),
'Z': (220,67,51),
'J': (51,117,186),
'L': (232,157,69)
}
# SRS rotation and wall kicks
offsets_I = {(0,1):[(1,0),(-1,0),(2,0),(-1,-1),(2,2)], (1,0):[(-1,0),(1,0),(-2,0),(1,1),(-2,-2)],
(1,2):[(0,-1),(-1,-1),(2,-1),(-1,1),(2,-2)], (2,1):[(0,1),(1,1),(-2,1),(1,-1),(-2,2)],
(2,3):[(-1,0),(1,0),(-2,0),(1,1),(-2,-2)], (3,2):[(1,0),(-1,0),(2,0),(-1,-1),(2,2)],
(3,0):[(0,1),(1,1),(-2,1),(1,-1),(-2,2)], (0,3):[(0,-1),(-1,-1),(2,-1),(-1,1),(2,-2)],
(0,2):[(0,0),(0,1),(1,1),(-1,1),(1,0),(-1,0)], (2,0):[(0,0),(0,-1),(-1,-1),(1,-1),(-1,0),(1,0)],
(1,3):[(0,0),(1,0),(1,2),(1,1),(0,2),(0,1)], (3,1):[(0,0),(-1,0),(-1,2),(-1,1),(0,2),(0,1)]}
offsets_oth = {(0,1):[(0,0),(-1,0),(-1,1),(0,-2),(-1,-2)], (1,0):[(0,0),(1,0),(1,-1),(0,2),(1,2)],
(1,2):[(0,0),(1,0),(1,-1),(0,2),(1,2)], (2,1):[(0,0),(-1,0),(-1,1),(0,-2),(-1,-2)],
(2,3):[(0,0),(1,0),(1,1),(0,-2),(1,-2)], (3,2):[(0,0),(-1,0),(-1,-1),(0,2),(-1,2)],
(3,0):[(0,0),(-1,0),(-1,-1),(0,2),(-1,2)], (0,3):[(0,0),(1,0),(1,1),(0,-2),(1,-2)],
(0,2):[(1,-1),(0,1),(1,1),(-1,1),(1,0),(-1,0)], (2,0):[(-1,1),(0,-1),(-1,-1),(1,-1),(-1,0),(1,0)],
(1,3):[(-1,-1),(1,0),(1,2),(1,1),(0,2),(0,1)], (3,1):[(1,1),(-1,0),(-1,2),(-1,1),(0,2),(0,1)]}
SRS_OFFSETS = {'I': offsets_I, 'O': {}, 'other': offsets_oth}
# Up,Right=+
# Tetromino shapes
SHAPES = {
'I': [
[(-1,0),(0,0),(1,0),(2,0)],
[(0,1),(0,0),(0,-1),(0,-2)],
[(-2,0),(-1,0),(0,0),(1,0)],
[(0,2),(0,1),(0,0),(0,-1)]
],
'O': [
[(0,0),(1,0),(0,1),(1,1)]
]*4,
'T': [
[(-1,0),(0,0),(1,0),(0,1)],
[(0,1),(0,0),(0,-1),(1,0)],
[(-1,0),(0,0),(1,0),(0,-1)],
[(0,1),(0,0),(0,-1),(-1,0)]
],
'S': [
[(-1,0),(0,0),(0,1),(1,1)],
[(0,1),(0,0),(1,0),(1,-1)],
[(-1,-1),(0,-1),(0,0),(1,0)],
[(-1,1),(-1,0),(0,0),(0,-1)]
],
'Z': [
[(-1,1),(0,1),(0,0),(1,0)],
[(1,1),(1,0),(0,0),(0,-1)],
[(-1,0),(0,0),(0,-1),(1,-1)],
[(0,1),(0,0),(-1,0),(-1,-1)]
],
'J': [
[(-1,1),(-1,0),(0,0),(1,0)],
[(0,1),(0,0),(0,-1),(1,1)],
[(-1,0),(0,0),(1,0),(1,-1)],
[(-1,-1),(0,1),(0,0),(0,-1)]
],
'L': [
[(-1,0),(0,0),(1,0),(1,1)],
[(0,1),(0,0),(0,-1),(1,-1)],
[(-1,-1),(-1,0),(0,0),(1,0)],
[(-1,1),(0,1),(0,0),(0,-1)]
]
}
SHAPE_PLACE=[x for x in SHAPES.keys()]
def get_master_timing(level):
for (low, high), t in MASTER_TIMINGS:
if low <= level <= high:
return t
return MASTER_TIMINGS[-1][1]
def SNO(shape):
if shape=="O":
dx=-0.5
dy=-0.5
elif shape=="I":
dx=-0.5
dy=0
else:
dx=0
dy=-0.5
return dx,dy
def ELO(shape):
if shape=="O":
dx=0
else:
dx=1
return dx
def ERO(shape):
if shape=="I":
dx=2
else:
dx=1
return dx
class Piece:
def __init__(self, shape,spawn=1):
self.shape = shape
self.rotation = 0
self.blocks = SHAPES[shape]
self.last_kick=0
self.last_action="spawn"
if spawn==-1:
self.x = 1+spawn+ELO(shape)
elif spawn==1:
self.x=GRID_WIDTH//2
else:
self.x=GRID_WIDTH-(1+ERO(shape))
self.y = GRID_HEIGHT-2
def current_coords(self):
return [(self.x+dx, self.y+dy) for dx, dy in self.blocks[self.rotation]]
def is_t_spin(self, grid):
if self.shape != 'T' or self.last_action!="rotate":return None
corners = [(-1, 1), (1, 1), (1, -1), (-1, -1)];occupied = 0;mini_parse = 0
pnum=[self.rotation,(self.rotation+1)%4]
for i,(dx, dy) in enumerate(corners):
x, y = self.x + dx, self.y + dy
try:
if grid[y][x] is not None:
occupied += 1
if i in pnum: mini_parse+=1
except IndexError:
occupied += 1
if i in pnum: mini_parse+=1
if occupied >= 3:
if mini_parse!=2 and self.last_kick!=4:
return 'T-Spin Mini'
return 'T-Spin'
return None
def rotate(self, dir, grid):
old, new = self.rotation, (self.rotation+dir)%4
offsets = SRS_OFFSETS.get(self.shape, SRS_OFFSETS['other']).get((old,new), [(0,0)])
for i, (ox, oy) in enumerate(offsets):
self.rotation, self.x, self.y = new, self.x+ox, self.y+oy
if self._valid(grid): self.last_kick=i;return True
self.rotation, self.x, self.y = old, self.x-ox, self.y-oy
return False
def _valid(self, grid):
for x,y in self.current_coords():
if x<0 or x>=GRID_WIDTH or y<0 or grid[y][x] is not None: return False
return True
class Tetris(arcade.Window):
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color((106,90,205))
self.background=arcade.load_texture("./bg/title.png")
self.block_sprite=[[[[] for _ in range(8)] for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
self.hold_sprite=[[] for _ in range(8)]
self.next_sprite=[[] for _ in range(8)]
self.lane_sprite=[[[] for _ in range(5)] for _ in range(8)]
self.grade_sprite=[]
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
for i in range(8):
block=arcade.Sprite(
arcade.Texture(PIL.Image.open("./image/block.png").convert("RGBA")).crop(30*(i%4),30*(i//4),30,30),
center_x=300+(x+0.5)*CELL_SIZE, center_y=100+(y+0.5)*CELL_SIZE,
image_width=30,
image_height=30)
self.block_sprite[y][x][i]=block
for a in range(len(GRADE)):
x=a//7;y=a%7
self.grade_sprite.append(
arcade.Sprite(
arcade.Texture(PIL.Image.open("./image/grade.png").convert("RGBA")).crop(150*x,120*y,150,120),
center_x=205, center_y=300,
image_width=150,image_height=120)
)
for a,(i,v) in enumerate(SHAPES.items()):
ns=int(CELL_SIZE*NEXT_SZ);hs=int(CELL_SIZE*HOLD_SZ);ls=int(CELL_SIZE*LANE_SZ)
f=SNO(i)
n=[];h=[];l=[[] for _ in range(5)]
for dx,dy in v[0]:
dx+=f[0];dy+=f[1]
h.append(
arcade.Sprite(
arcade.Texture(PIL.Image.open("./image/block.png").convert("RGBA").resize((hs*4,hs*2),PIL.Image.Resampling.LANCZOS)).crop(hs*(a%4),hs*(a//4),hs,hs),
center_x=200+dx*hs, center_y=95+(GRID_HEIGHT-2)*CELL_SIZE+dy*hs,
image_width=hs,image_height=hs)
)
n.append(
arcade.Sprite(
arcade.Texture(PIL.Image.open("./image/block.png").convert("RGBA").resize((ns*4,ns*2),PIL.Image.Resampling.LANCZOS)).crop(ns*(a%4),ns*(a//4),ns,ns),
center_x=300+(GRID_WIDTH)*CELL_SIZE+dx*ns, center_y=110+(GRID_HEIGHT+2)*CELL_SIZE+dy*ns,
image_width=ns,
image_height=ns)
)
for j in range(5):
l[j].append(
arcade.Sprite(
arcade.Texture(PIL.Image.open("./image/block.png").convert("RGBA").resize((ls*4,ls*2),PIL.Image.Resampling.LANCZOS)).crop(ls*(a%4),ls*(a//4),ls,ls),
center_x=350+(GRID_WIDTH+1)*CELL_SIZE+dx*ls, center_y=110+(GRID_HEIGHT-2-j*3)*CELL_SIZE+dy*ls,
image_width=ls,
image_height=ls)
)
self.hold_sprite[a]=h
self.next_sprite[a]=n
self.lane_sprite[a]=l
self.reset_game()
def reset_game(self):
self.section_start_time = 0.0
self.section=0
self.g_section=GRAVITY[:]
self.gs=0
self.cr_section=CR_TIMINGS[:]
self.cool_count = 0
self.regret_count = 0
self.grid = [[None]*GRID_WIDTH for _ in range(GRID_HEIGHT)]
k=random.sample(["I","T","J","L"],1)
w=list(SHAPES.keys());w.remove(k[0])
self.bag = k+random.sample(w, 6)
self.hold = None
self.hold_used = False
self.up_used = False
self.next_pieces = []
self.level = 0
self.speed_level = 0
self.in_grade = 0
self.gp=0
self.cool = 0
self.regret = 0
self.score = 0
self.combo = 0
self.sf_t=0;self.hd_t=0
self.b2b=False
self.last_clear=None
self.frame=arcade.Sprite("./image/frame.png")
self.frame_list=arcade.SpriteList()
self.block_list=arcade.SpriteList()
self.hold_list=arcade.SpriteList()
self.next_list=arcade.SpriteList()
self.lane_list=arcade.SpriteList()
self.grade_list=arcade.SpriteList()
self.frame_list.append(self.frame)
self.frame.center_x, self.frame.center_y=500,450
self.drop_timer = 0.0
self.lock_timer = 0.0
self.are_timer = 0.0
self.line_are_timer = 0.0
self.decay_timer=0.0
self.pending_spawn = False
self.move_left = self.move_right = self.move_down = False
self.das_timer = self.arr_timer = 0.0
self.das_down_timer = self.arr_down_timer = 0.0
self.elp=0.0
self.paused = False
self.game_over = False
self.move_floor = 0
self.p_move = 0
self.perfect=False
self.spinp=None
self.spawn_piece()
def spawn_piece(self):
if self.move_left:spawn=-1
elif self.move_right:spawn=0
else:spawn=1
# increase level on each spawn
self.level += 1 if (self.level+1)%100!=0 else 0
if self.level>=self.cr_section[self.section][0][1] and not self.cr_section[self.section][2]:
self.cr_section[self.section][2]=True
elapsed = self.elp - self.section_start_time
if elapsed <= self.cr_section[self.section][1]["COOL"]:
self.cool+=1
self.cr_section[self.section][3]=True
elapsed = self.elp - self.section_start_time
if elapsed >= self.cr_section[self.section][1]["REGRET"]:
self.regret+=1
self.cr_section[self.section][4]=True
if self.level>self.cr_section[self.section][0][2]:
self.section+=1
self.section_start_time=self.elp
cr_count = max(self.cool - self.regret,0)
self.speed_level = self.level + cr_count * 100
self.timing = get_master_timing(self.speed_level)
while len(self.next_pieces) < 7:
if len(self.bag)==0:
self.bag += random.sample(list(SHAPES.keys()), 7)
self.next_pieces.append(self.bag.pop(0))
shape = self.next_pieces.pop(0)
piece=Piece(shape,spawn)
if not piece._valid(self.grid): self.game_over = True
else:
self.piece = piece
self.hold_used = False
self.up_used = False
def on_draw(self):
self.clear()
self.block_list.clear()
self.hold_list.clear()
self.next_list.clear()
self.lane_list.clear()
self.grade_list.clear()
arcade.draw_texture_rect(
self.background,
arcade.LBWH(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
)
# Settled blocks
arcade.draw_rect_filled(
arcade.rect.XYWH(300+5*CELL_SIZE, 100+10*CELL_SIZE, CELL_SIZE*GRID_WIDTH, CELL_SIZE*GRID_HEIGHT),(0,0,0,100))
self.grade_list.append(self.grade_sprite[self.gp])
for y, row in enumerate(self.grid):
for x, color in enumerate(row):
if color:
i=SHAPE_PLACE.index(color);b=self.block_sprite[y][x][i]
if not self.block_list.__contains__(b):
self.block_list.append(b)
if not self.pending_spawn:
for x, y in self.piece.current_coords():
i=SHAPE_PLACE.index(self.piece.shape);b=self.block_sprite[y][x][i]
if not self.block_list.__contains__(b):
self.block_list.append(b)
arcade.draw_rect_filled(
arcade.rect.XYWH(300+(GRID_WIDTH)*CELL_SIZE, 120+(GRID_HEIGHT+2)*CELL_SIZE, CELL_SIZE*5, CELL_SIZE*4),
(0,0,0,100)
)
arcade.draw_rect_filled(
arcade.rect.XYWH(350+(GRID_WIDTH+1)*CELL_SIZE, 115+(GRID_HEIGHT-8)*CELL_SIZE, CELL_SIZE*4, CELL_SIZE*15),
(0,0,0,100)
)
arcade.draw_rect_filled(
arcade.rect.XYWH(205, 100+10*CELL_SIZE, 160, CELL_SIZE*GRID_HEIGHT),
(0,0,0,100)
)
self.frame_list.draw()
# Ghost piece
ghost = Piece(self.piece.shape)
ghost.rotation = self.piece.rotation
ghost.x, ghost.y = self.piece.x, self.piece.y
while ghost._valid(self.grid):
ghost.y -= 1
if ghost.y!=self.piece.y:
ghost.y += 1
for x, y in ghost.current_coords():
arcade.draw_rect_outline(
arcade.rect.XYWH(300+(x+0.5)*CELL_SIZE, 100+(y+0.5)*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1),
arcade.color.RED,border_width=2
)
self.block_list.draw()
for y, row in enumerate(self.grid):
for x, color in enumerate(row):
if color:
arcade.draw_rect_filled(
arcade.rect.XYWH(300+(x+0.5)*CELL_SIZE, 100+(y+0.5)*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1),
(0,0,0,50)
)
if self.pending_spawn and all(c is not None for c in row):
delay = self.line_are_timer or self.are_timer
arcade.draw_rect_filled(
arcade.rect.XYWH(300+5*CELL_SIZE, 100+(y+0.5)*CELL_SIZE, CELL_SIZE*GRID_WIDTH, CELL_SIZE-1),
(255,255,255,int(200*delay))
)
# Active piece
for x, y in self.piece.current_coords():
if self.pending_spawn:
delay = self.line_are_timer or self.are_timer
arcade.draw_rect_filled(
arcade.rect.XYWH(300+(x+0.5)*CELL_SIZE, 100+(y+0.5)*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1),
(255,255,255,int(255*delay)))
elif ghost.y==self.piece.y:
arcade.draw_rect_filled(
arcade.rect.XYWH(300+(x+0.5)*CELL_SIZE, 100+(y+0.5)*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1),
(0,0,0,int(150*(self.lock_timer*FPS/self.timing['lock'])))
)
# Hold display
if self.hold:
i=SHAPE_PLACE.index(self.hold);b=self.hold_sprite[i]
if not self.hold_list.__contains__(b[0]):
for x in b:
self.hold_list.append(x)
self.hold_list.draw()
if self.hold_used:
csz=int(CELL_SIZE*HOLD_SZ)
f=SNO(self.hold)
for dx, dy in SHAPES[self.hold][0]:
dx+=f[0];dy+=f[1]
arcade.draw_rect_filled(
arcade.rect.XYWH(200+dx*csz, 95+(GRID_HEIGHT-2)*CELL_SIZE+dy*csz, csz-1, csz-1),
(0,0,0,100)
)
# Next pieces
i=SHAPE_PLACE.index(self.next_pieces[0]);b=self.next_sprite[i]
if not self.next_list.__contains__(b[0]):
for x in b:
self.next_list.append(x)
for j in range(1,6):
i=SHAPE_PLACE.index(self.next_pieces[j]);b=self.lane_sprite[i][j-1]
if not self.lane_list.__contains__(b[0]):
for x in b:
self.lane_list.append(x)
self.next_list.draw()
self.lane_list.draw()
e = self.elp
m=min(int(e)//60,99);s=int(e)%60;ms=int(100*(e-int(e)))
elapsed=f"{m:02}'{s:02}\"{ms:03}"
# score display
sc=[arcade.Text("Score:", 130, SCREEN_HEIGHT-330-40, arcade.color.WHITE, 16),arcade.Text(f"{self.score}", 130, SCREEN_HEIGHT-330-60, arcade.color.WHITE, 16),arcade.Text("Level:", 130, SCREEN_HEIGHT-330-100, arcade.color.WHITE, 16),arcade.Text(f"{self.level}", 130, SCREEN_HEIGHT-330-120, arcade.color.WHITE, 16),arcade.Text("TIME:", 130, SCREEN_HEIGHT-330-160, arcade.color.WHITE, 16),arcade.Text(f"{elapsed}", 130, SCREEN_HEIGHT-330-180, arcade.color.WHITE, 16),arcade.Text("Grade:", 130, SCREEN_HEIGHT-330-220, arcade.color.GOLD, 16)]
if self.combo > 1:
sc+=[arcade.Text("COMBO:", 130, SCREEN_HEIGHT-330-360, arcade.color.WHITE, 16),arcade.Text(f"{self.combo}", 130, SCREEN_HEIGHT-330-380, arcade.color.WHITE, 16)]
if self.last_clear is not None:
sc+=[arcade.Text(f"{self.last_clear}", 130, SCREEN_HEIGHT-330-420, arcade.color.WHITE, 16)]
if self.cr_section[self.section][3]:
d="COOL"
c=arcade.color.AQUA
if self.cr_section[self.section][4]:
d="REGRET"
c=arcade.color.ORANGE if self.cr_section[self.section][3] else arcade.color.RED
if not (self.cr_section[self.section][3] or self.cr_section[self.section][4]):
d=""
c=arcade.color.WHITE
sc+=[arcade.Text(f"{d}", 130, SCREEN_HEIGHT-330-440, c, 16)]
[x.draw() for x in sc]
self.grade_list.draw()
if self.paused:
arcade.Text("PAUSED", SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2, arcade.color.WHITE, 24).draw()
if self.game_over:
arcade.Text("GAME OVER", SCREEN_WIDTH/2-80, SCREEN_HEIGHT/2, arcade.color.RED, 24).draw()
def clear_check(self):
return self.grid[:]!=[r for r in self.grid if any(c is None for c in r)]
def get_ingrade(self,cleared):
if self.spinp == "T-Spin":sm=1.5
elif self.spinp == "T-Spin Mini":sm=1.2
else:sm=1.0
if self.perfect: sm*=2.0
if self.b2b: sm*=1.5
if cleared==1:
if self.in_grade<5:pt=10
elif self.in_grade<10:pt=5
else: pt=2
cm=1
elif cleared==2:
if self.in_grade<3:pt=20
elif self.in_grade<6:pt=15
elif self.in_grade<10:pt=12
else: pt=10
cm=1+0.1*(self.combo//2) if self.combo<10 else 2
elif cleared==3:
if self.in_grade==0:pt=40
elif self.in_grade<4:pt=30
elif self.in_grade<7:pt=20
elif self.in_grade<10:pt=15
else: pt=13
cm=1+0.1*(self.combo+2) if self.combo<10 else 2.5
else:
if self.in_grade==0:pt=50
elif self.in_grade<5:pt=40
else: pt=30
cm=1.5+0.2*min(max(self.combo-2,0),5)+0.1*max(self.combo-5,0) if self.combo<10 else 3
if self.combo==1: cm=1
return math.ceil(pt*cm*sm)*(1+self.level//250)
def get_clear(self,cleared):
t="B2B " if self.b2b and cleared > 0 else ""
t+=f"{self.spinp} " if self.spinp is not None else ""
if cleared == 1: t+="Single" if self.spinp is None else ""
elif cleared == 2: t+="Double"
elif cleared == 3: t+="Triple"
elif cleared == 4: t+="Tetris"
if t=="":return None
if len(t)>10:
t=t.replace("T-Spin","TS").replace("Single","S").replace("Double","D").replace("Triple","T").replace("Mini","M").replace(" ","").replace("B2B","B2B ")
return t
def clear_lines(self):
new_grid = [r for r in self.grid if any(c is None for c in r)]
cleared = GRID_HEIGHT - len(new_grid)
self.spinp=self.piece.is_t_spin(self.grid)
if cleared > 0 and self.spinp is None and cleared<4: self.b2b=False
self.last_clear=self.get_clear(cleared)
for _ in range(cleared):
new_grid.append([None] * GRID_WIDTH)
if all(all(cell is None for cell in row) for row in new_grid): self.perfect=True
self.combo = self.combo if cleared > 0 or self.spinp is not None else 0
if cleared > 0:
self.combo+=1
self.in_grade += self.get_ingrade(cleared)
else:
self.combo = 0
self.grid = new_grid
self.score+=self.get_score(cleared)
self.level += max(1,2*(cleared-1)) if cleared > 0 else 0
if self.b2b and cleared > 0: self.level += 1
if self.spinp == "T-Spin" and cleared > 0: self.level+=2
if cleared > 0: self.b2b=True if self.spinp is not None or cleared >= 4 else False
self.perfect=False
self.spinp=None
self.sf_t=0
self.hd_t=0
return cleared
def get_score(self,cleared):
r=0
if cleared == 0:
if self.spinp == "T-Spin Mini": r+=100
elif self.spinp == "T-Spin": r+=400
elif cleared == 1:
r+=100
if self.perfect: r+= 700
if self.spinp == "T-Spin Mini": r+=100
elif self.spinp == "T-Spin": r+=700
elif cleared == 2:
r+=300
if self.perfect: r+= 900
if self.spinp == "T-Spin Mini": r+=100
elif self.spinp == "T-Spin": r+=900
elif cleared == 3:
r+=500
if self.perfect: r+= 1300
if self.spinp == "T-Spin": r+=1100
elif cleared == 4:
r+=800
if self.perfect: r+= 1200
if self.b2b:
r+=1200 if self.perfect else 0
r*=1.5
return int((r+50*(max(0,self.combo-1)))*self.level+self.sf_t+2*self.hd_t)
def on_update(self, dt):
if self.paused or self.game_over: return
self.elp+=dt
if self.pending_spawn:
delay = self.line_are_timer or self.are_timer
delay -= dt
if self.line_are_timer:
self.line_are_timer = delay
else:
self.are_timer = delay
if delay <= 0:
cleared = self.clear_lines()
self.spawn_piece()
self.are_timer = 0.0
self.line_are_timer = 0.0
self.pending_spawn = False
return
# handle DAS horizontal
if self.move_left or self.move_right:
self.das_timer += dt
if self.das_timer >= self.timing['das']/FPS:
self.arr_timer += dt
if self.arr_timer >= SOFT_ARR_INTERVAL: # immediate ARR
self.arr_timer = 0
self.move_floor+=1 if self.lock_timer!=0 else 0
dx = -1 if self.move_left else 1
self.piece.x += dx
if not self.piece._valid(self.grid): self.piece.x -= dx
# handle DAS soft drop
if self.move_down:
if self.das_down_timer==0:
self.try_drop(ad=True)
self.das_down_timer += dt
if self.das_down_timer >= self.timing['das']/FPS:
self.try_drop(ad=True)
# gravity
self.drop_timer += dt
while self.drop_timer >= self.gravity()/FPS:
self.drop_timer -= self.gravity()/FPS
self.try_drop()
self.decay_timer += dt
if self.decay_timer >= self.decay()/FPS:
self.decay_timer= 0
self.in_grade=max(self.in_grade-1,0)
if not all(0<=nx<GRID_WIDTH and ny>=0 and self.grid[ny][nx] is None
for nx,ny in [(x,y-1) for x,y in self.piece.current_coords()]):
self.lock_timer+=dt
else:
self.lock_timer=0
if self.p_move!=self.move_floor:
self.lock_timer=0
self.p_move=self.move_floor
if self.lock_timer>=self.timing['lock']/FPS or self.move_floor>=15:
self.lock_piece()
self.get_grade()
def get_grade(self):
g=self.in_grade
if math.floor(self.in_grade + max(self.cool - self.regret,0))>=100:
self.gp+=1
self.in_grade=0
self.grade=GRADE[self.gp]
def decay(self):
return DECAY_RATE[min(len(DECAY_RATE)-1,int(self.gp))]
def gravity(self):
while self.speed_level > self.g_section[self.gs][0][1]:
self.gs+=1
return 65536/self.g_section[self.gs][1]
def try_drop(self,ad=False):
if all(0<=nx<GRID_WIDTH and ny>=0 and self.grid[ny][nx] is None
for nx,ny in [(x,y-1) for x,y in self.piece.current_coords()]):
self.piece.y-=1
if ad: self.sf_t+=1
self.piece.last_action="move"
self.lock_timer=0
self.move_floor=0
def lock_piece(self):
for x,y in self.piece.current_coords(): self.grid[y][x]=self.piece.shape
# apply line clear ARE if any lines
cl=self.clear_check()
if cl:
self.line_are_timer = self.timing['line_are']/FPS
else:
self.are_timer = self.timing['are']/FPS
self.pending_spawn = True
self.lock_timer = 0
self.drop_timer = 0
self.move_floor = 0
def on_key_press(self,key,_):
if key == arcade.key.R or (key==arcade.key.ESCAPE and self.game_over):self.reset_game();return
elif key==arcade.key.ESCAPE:self.paused=not self.paused;return
if self.paused or self.game_over: return
if key==arcade.key.LEFT:
self.move_left=True;self.move_right=False;self.das_timer=0
self.move_floor+=1 if self.lock_timer!=0 else 0
if not self.pending_spawn:
self.piece.x-=1
if not self.piece._valid(self.grid): self.piece.x+=1
self.piece.last_action="move"
elif key==arcade.key.RIGHT:
self.move_right=True;self.move_left=False;self.das_timer=0;self.arr_timer=0
self.move_floor+=1 if self.lock_timer!=0 else 0
if not self.pending_spawn:
self.piece.x+=1
if not self.piece._valid(self.grid): self.piece.x-=1
self.piece.last_action="move"
elif key==arcade.key.DOWN:
self.move_down=True;self.das_down_timer=0
elif key==arcade.key.UP and not self.pending_spawn:
while all(0<=nx<GRID_WIDTH and ny>=0 and self.grid[ny][nx] is None
for nx,ny in [(x,y-1) for x,y in self.piece.current_coords()]):
self.piece.y-=1
self.hd_t+=1
self.up_used=True
self.lock_piece()
elif key==arcade.key.RSHIFT:
while all(0<=nx<GRID_WIDTH and ny>=0 and self.grid[ny][nx] is None
for nx,ny in [(x,y-1) for x,y in self.piece.current_coords()]):
self.piece.y-=1
self.hd_t+=1
elif key in (arcade.key.Z,arcade.key.X,arcade.key.C):
rot=[-1,1,2][[arcade.key.Z,arcade.key.X,arcade.key.C].index(key)]
if not self.pending_spawn: self.piece.rotate(rot,self.grid)
self.move_floor+=1 if self.lock_timer!=0 else 0
self.piece.last_action="rotate"
elif key==arcade.key.A and not (self.hold_used or self.up_used):
if self.move_left:spawn=-1
elif self.move_right:spawn=0
else:spawn=1
if self.hold is None: self.hold,self.piece=self.piece.shape,None;self.spawn_piece()
else: self.hold,self.piece.shape=self.piece.shape,self.hold;self.piece = Piece(self.piece.shape,spawn)
self.hold_used=True
def on_key_release(self,key,_):
if key==arcade.key.LEFT: self.move_left=False
elif key==arcade.key.RIGHT: self.move_right=False
elif key==arcade.key.DOWN: self.move_down=False
if __name__=='__main__':
Tetris()
arcade.run()