-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.py
1043 lines (904 loc) · 36.1 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
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
from random import shuffle
from sys import exit
clock = pygame.time.Clock()
displayDimensions = width, height = 600, 800
blockSize = 24
blockGap = 3
white = (255, 255, 255)
black = (0, 0, 0)
cyan = (0, 255, 255) # I, 1
blue = (0, 0, 255) # J, 2
orange = (255, 165, 0) # L, 3
yellow = (255, 255, 0) # O, 4
lime = (0, 255, 0) # S, 5
purple = (128, 0, 128) # T, 6
red = (255, 0, 0) # Z, 7
colorList = [cyan, blue, orange, yellow, lime, purple, red]
showGhostPiece = True
themeVolume = 1
boardSize = (12 * blockSize + 11 * blockGap, 21 * blockSize + 20 * blockGap)
bsbg = blockSize + blockGap
startPosOutline = (int((displayDimensions[0] - boardSize[0]) / 2), int((displayDimensions[1] - boardSize[1]) / 2))
startPosBoard = (startPosOutline[0] + bsbg, startPosOutline[1] - 2 * bsbg)
scoreboardXCenter = displayDimensions[0] - startPosOutline[0] // 2
tetrominos = ["I", "J", "L", "O", "S", "T", "Z"]
tetrominosBag = []
downWait = 2
sideWait1 = 10
sideWait2 = 2
I = [[
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
], [
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0]
], [
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0]
], [
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]
]]
J = [[
[2, 0, 0],
[2, 2, 2],
[0, 0, 0]
], [
[0, 2, 2],
[0, 2, 0],
[0, 2, 0]
], [
[0, 0, 0],
[2, 2, 2],
[0, 0, 2]
], [
[0, 2, 0],
[0, 2, 0],
[2, 2, 0]
]]
L = [[
[0, 0, 3],
[3, 3, 3],
[0, 0, 0]
], [
[0, 3, 0],
[0, 3, 0],
[0, 3, 3]
], [
[0, 0, 0],
[3, 3, 3],
[3, 0, 0]
], [
[3, 3, 0],
[0, 3, 0],
[0, 3, 0]
]]
O = [[
[4, 4],
[4, 4]
], [
[4, 4],
[4, 4],
], [
[4, 4],
[4, 4]
], [
[4, 4],
[4, 4]
]]
S = [[
[0, 5, 5],
[5, 5, 0],
[0, 0, 0]
], [
[0, 5, 0],
[0, 5, 5],
[0, 0, 5]
], [
[0, 0, 0],
[0, 5, 5],
[5, 5, 0]
], [
[5, 0, 0],
[5, 5, 0],
[0, 5, 0]
]]
T = [[
[0, 6, 0],
[6, 6, 6],
[0, 0, 0]
], [
[0, 6, 0],
[0, 6, 6],
[0, 6, 0]
], [
[0, 0, 0],
[6, 6, 6],
[0, 6, 0]
], [
[0, 6, 0],
[6, 6, 0],
[0, 6, 0]
]]
Z = [[
[7, 7, 0],
[0, 7, 7],
[0, 0, 0]
], [
[0, 0, 7],
[0, 7, 7],
[0, 7, 0]
], [
[0, 0, 0],
[7, 7, 0],
[0, 7, 7]
], [
[0, 7, 0],
[7, 7, 0],
[7, 0, 0]
]]
pygame.init()
pygame.font.init()
mainDisplay = pygame.display.set_mode(displayDimensions)
menuDisplay = pygame.Surface(displayDimensions, pygame.SRCALPHA)
pauseDisplay = pygame.Surface(displayDimensions, pygame.SRCALPHA)
gameDisplay = pygame.Surface(displayDimensions, pygame.SRCALPHA)
fontBasic = pygame.font.Font("fonts/prstart.ttf", 20)
fontBig = pygame.font.Font("fonts/prstart.ttf", 32)
lvlText = fontBasic.render("LEVEL", False, white)
lnsText = fontBasic.render("LINES", False, white)
scrText = fontBasic.render("SCORE", False, white)
nextTetrominoText = fontBasic.render("NEXT", False, white)
gameOverText = fontBig.render("GAME OVER", False, white)
sgpText = fontBasic.render("SHOW GHOST PIECE", False, white)
volumeText = fontBasic.render("VOLUME", False, white)
settingsText = fontBig.render("SETTINGS", False, white)
lvlTextRect = lvlText.get_rect()
lnsTextRect = lnsText.get_rect()
scrTextRect = scrText.get_rect()
lvlTextRectGO = lvlText.get_rect()
lnsTextRectGO = lnsText.get_rect()
scrTextRectGO = scrText.get_rect()
sgpTextSize = sgpText.get_size()
volumeTextRect = volumeText.get_rect()
nextTetrominoTextRect = nextTetrominoText.get_rect()
gameOverTextRect = gameOverText.get_rect()
settingsTextRect = settingsText.get_rect()
lvlTextRect.center = (scoreboardXCenter, 300)
lnsTextRect.center = (scoreboardXCenter, 400)
scrTextRect.center = (scoreboardXCenter, 500)
nextTetrominoTextRect.center = (scoreboardXCenter, startPosOutline[1] + 3 * bsbg - 25)
gameOverTextRect.center = (width // 2, 100)
lvlTextRectGO.center = (width // 2, 300)
lnsTextRectGO.center = (width // 2, 400)
scrTextRectGO.center = (width // 2, 500)
settingsTextRect.center = (width // 2, 100)
sgpTextPos = ((width - sgpTextSize[0]) // 2 - sgpTextSize[1], 250)
volumeTextRect.center = (width // 2, 400)
# Texture Loading
bgTexture = pygame.image.load("textures/background.png")
rButtonTexture = pygame.image.load("textures/buttonRight.png")
lButtonTexture = pygame.image.load("textures/buttonLeft.png")
rPlayButtonTexture = pygame.image.load("textures/playButtonRight.png")
tetrisLogo = pygame.image.load("textures/tetrisLogo.png")
mainMenuButtonTexture = pygame.image.load("textures/mainMenuButton.png")
quitButtonTexture = pygame.image.load("textures/quitButton.png")
backButtonTexture = pygame.image.load("textures/backButton.png")
icon = pygame.image.load("textures/icon.png")
buttonSize = rPlayButtonTexture.get_size()
gOButtonSize = mainMenuButtonTexture.get_size()
logoSize = tetrisLogo.get_size()
pygame.mixer.music.load("music/tetrisTheme.ogg")
pygame.display.set_caption("Tetris Clone No.\u0236")
pygame.display.set_icon(icon)
mMButtonPos = ((width // 2 - gOButtonSize[0] - (width - 2 * gOButtonSize[0]) // 6), 600)
qButtonPos = ((width // 2 + (width - 2 * gOButtonSize[0]) // 6), 600)
class SlidingButton:
def __init__(self, texture, start, sposition, direction):
self.t = 50
self.acc = 0.7
self.speed = 9
self.texture = texture
self.start = start
self.end = start + self.t
self.xPosition, self.yPosition = sposition
self.sPosition = sposition
self.direction = direction # -1 moving to left, 1 moving to right
def reset_speed(self):
self.speedClone = self.speed
def reset_position(self):
self.xPosition, self.yPosition = self.sPosition
def move_button_in(self, time):
if self.end >= time >= self.start and (self.xPosition < 0 or self.xPosition > width - buttonSize[0]):
if (time - self.start) / self.t > 0.76 and self.speedClone > 0:
self.speedClone -= self.acc
self.xPosition += self.direction * self.speedClone
elif self.end < time:
if self.direction == 1:
self.xPosition = 0
elif self.direction == -1:
self.xPosition = width - buttonSize[0]
menuDisplay.blit(self.texture, (self.xPosition, self.yPosition))
def move_button_out(self, time):
if self.end >= time >= self.start:
if (time - self.start) / self.t < 0.25:
self.speedClone += self.acc
self.xPosition -= self.direction * self.speedClone
menuDisplay.blit(self.texture, (self.xPosition, self.yPosition))
class SlidingLogo:
def __init__(self, texture, start, sposition, speed):
self.t = 40
self.speed = speed
self.texture = texture
self.start = start
self.end = start + self.t
self.xPosition, self.yPosition = sposition
def move_logo_in(self, time):
if self.start <= time <= self.end:
self.yPosition += self.speed
menuDisplay.blit(self.texture, (self.xPosition, self.yPosition))
def move_logo_out(self, time):
if self.start <= time <= self.end:
self.yPosition -= self.speed
menuDisplay.blit(self.texture, (self.xPosition, self.yPosition))
button1pos = ((198, 300), (600, 354))
button2pos = ((0, 400), (402, 454))
button3pos = ((198, 500), (600, 554))
playButton1 = SlidingButton(rPlayButtonTexture, 10, (600, button1pos[0][1]), -1)
playButton2 = SlidingButton(lButtonTexture, 35, (-400, button2pos[0][1]), 1)
playButton3 = SlidingButton(rButtonTexture, 60, (600, button3pos[0][1]), -1)
tLogo = SlidingLogo(tetrisLogo, 0, (199, -logoSize[1]), 5)
def quitplz():
pygame.quit()
exit()
def random_tetromino():
global tetrominosBag
if len(tetrominosBag) == 0:
tetrominosBag = tetrominos[:]
shuffle(tetrominosBag)
return tetrominosBag.pop()
def translate_to_list(t_name):
if t_name == "I":
t = I
elif t_name == "J":
t = J
elif t_name == "L":
t = L
elif t_name == "O":
t = O
elif t_name == "S":
t = S
elif t_name == "T":
t = T
elif t_name == "Z":
t = Z
return t
def line_out_animation(board, level, lines, score):
gameDisplay.blit(bgTexture, (0, 0))
draw_board_outline()
draw_board(board)
draw_scoreboard(level, lines, score)
mainDisplay.blit(gameDisplay, (0, 0))
pygame.display.update()
pygame.time.delay(100)
def line_checker(board, level, lines_total, score):
lines = 0
for y, yn in zip(board, range(len(board))):
for x in y:
if x == 0:
break
else:
if lines > 0:
line_out_animation(board, level, lines_total, score)
board.pop(yn)
board.insert(0, [0] * 10)
lines += 1
return board, lines
def draw_next_tetromino(next_tetromino, pos):
t = translate_to_list(next_tetromino)
for y, yn in zip(t[0], range(len(t[0]))):
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
pygame.draw.rect(gameDisplay, colorList[x - 1], (pos[0] + xn * bsbg, pos[1] + yn * bsbg,
blockSize, blockSize))
def draw_scoreboard(level, lines, score):
lvlTextNumber = fontBasic.render(str(level), False, white)
lnsTextNumber = fontBasic.render(str(lines), False, white)
scrTextNumber = fontBasic.render(str(score), False, white)
lvlTextNumberRect = lvlTextNumber.get_rect()
lnsTextNumberRect = lnsTextNumber.get_rect()
scrTextNumberRect = scrTextNumber.get_rect()
lvlTextNumberRect.center = (scoreboardXCenter, 325)
lnsTextNumberRect.center = (scoreboardXCenter, 425)
scrTextNumberRect.center = (scoreboardXCenter, 525)
gameDisplay.blit(lvlText, lvlTextRect)
gameDisplay.blit(lvlTextNumber, lvlTextNumberRect)
gameDisplay.blit(lnsText, lnsTextRect)
gameDisplay.blit(lnsTextNumber, lnsTextNumberRect)
gameDisplay.blit(scrText, scrTextRect)
gameDisplay.blit(scrTextNumber, scrTextNumberRect)
gameDisplay.blit(nextTetrominoText, nextTetrominoTextRect)
def draw_sidebar(score, lines, level, next_tetromino, ntpos):
draw_next_tetromino(next_tetromino, ntpos)
draw_scoreboard(level, lines, score)
def draw_tetromino(aT, tP):
t = translate_to_list(aT)
for y, yn in zip(t[tP[1]], range(len(t[0]))):
if tP[0][1] + yn > 1:
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
pygame.draw.rect(gameDisplay, colorList[x - 1],
(startPosBoard[0] + tP[0][0] * bsbg + xn * bsbg,
startPosBoard[1] + tP[0][1] * bsbg + yn * bsbg, blockSize, blockSize))
def can_tetromino_fall(board, aT, tP):
t = translate_to_list(aT)
for y, yn in zip(t[tP[1]], range(len(t[0][0]))):
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
try:
if board[tP[0][1] + yn + 1][tP[0][0] + xn] > 0:
return False
except IndexError:
return False
return True
def can_tetromino_move(board, aT, tP, direction):
t = translate_to_list(aT)
for y, yn in zip(t[tP[1]], range(len(t[0][0]))):
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
try:
if board[tP[0][1] + yn][tP[0][0] + xn + direction] > 0 or (tP[0][0] + xn <= 0 and direction == -1):
return False
except IndexError:
return False
return True
def can_tetromino_rotate(board, aT, tP, direction):
t = translate_to_list(aT)
for y, yn in zip(t[tP[1] + direction], range(len(t[0][0]))):
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
try:
if board[tP[0][1] + yn][tP[0][0] + xn] > 0 or tP[0][0] + xn < 0:
return False
except IndexError:
return False
return True
def solidify_tetromino(board, aT, tP):
end_game = False
t = translate_to_list(aT)
for y, yn in zip(t[tP[1]], range(len(t[0][0]))):
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
if board[tP[0][1] + yn][tP[0][0] + xn] == 0:
board[tP[0][1] + yn][tP[0][0] + xn] = x
else:
end_game = True
return board, end_game
def tp_tetromino_down(board, aT, tP):
while can_tetromino_fall(board, aT, tP):
tP[0][1] += 1
return solidify_tetromino(board, aT, tP)
def draw_ghost_tetromino(board, aT, tP):
t = translate_to_list(aT)
tcoords = [tP[0][0], tP[0][1]]
while can_tetromino_fall(board, aT, [tcoords, tP[1]]):
tcoords[1] += 1
for y, yn in zip(t[tP[1]], range(len(t[0]))):
if tcoords[1] + yn > 1:
for x, xn in zip(y, range(len(t[0][0]))):
if x > 0:
pygame.draw.rect(gameDisplay, colorList[x - 1],
(startPosBoard[0] + tcoords[0] * bsbg + xn * bsbg,
startPosBoard[1] + tcoords[1] * bsbg + yn * bsbg,
blockSize, blockSize), 1)
def draw_board_outline():
for a in range(21):
pygame.draw.rect(gameDisplay, white, (startPosOutline[0], startPosOutline[1] + a * bsbg, blockSize, blockSize))
pygame.draw.rect(gameDisplay, white, (startPosOutline[0] + 11 * bsbg,
startPosOutline[1] + a * bsbg, blockSize, blockSize))
for b in range(10):
pygame.draw.rect(gameDisplay, white, (startPosOutline[0] + (b + 1) * bsbg,
startPosOutline[1] + 20 * bsbg, blockSize, blockSize))
def board_outline_animation(size):
for a in range(21):
pygame.draw.rect(gameDisplay, white, (startPosOutline[0] + blockSize / 2 - size,
startPosOutline[1] + a * bsbg + blockSize / 2 - size, 2 * size, 2 * size))
pygame.draw.rect(gameDisplay, white, (startPosOutline[0] + 11 * bsbg + blockSize / 2 - size,
startPosOutline[1] + a * bsbg + blockSize / 2 - size, 2 * size, 2 * size))
for b in range(10):
pygame.draw.rect(gameDisplay, white, (startPosOutline[0] + (b + 1) * bsbg + blockSize / 2 - size,
startPosOutline[1] + 20 * bsbg + blockSize / 2 - size,
2 * size, 2 * size))
def game_in_animation():
for size in range(1, blockSize // 2 + 1):
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
gameDisplay.blit(bgTexture, (0, 0))
board_outline_animation(size)
mainDisplay.blit(gameDisplay, (0, 0))
pygame.display.update()
clock.tick(60)
def board_animation(size, board):
for y, yn in zip(board, range(len(board))):
for x, xn in zip(y, range(len(y))):
if yn >= 2 and x > 0:
pygame.draw.rect(gameDisplay, colorList[x - 1],
(startPosBoard[0] + xn * bsbg + blockSize / 2 - size,
startPosBoard[1] + yn * bsbg + blockSize / 2 - size, 2 * size, 2 * size))
def game_out_animation(board):
for size in range(blockSize // 2, 0, -1):
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
gameDisplay.blit(bgTexture, (0, 0))
board_outline_animation(size)
board_animation(size, board)
mainDisplay.blit(gameDisplay, (0, 0))
pygame.display.update()
clock.tick(60)
gameDisplay.blit(bgTexture, (0, 0))
mainDisplay.blit(gameDisplay, (0, 0))
pygame.display.update()
def draw_board(board):
for y, yn in zip(board, range(len(board))):
for x, xn in zip(y, range(len(y))):
if yn >= 2 and x > 0:
pygame.draw.rect(gameDisplay, colorList[x - 1],
(startPosBoard[0] + xn * bsbg, startPosBoard[1] + yn * bsbg, blockSize, blockSize))
def mm_in_animation():
pygame.mouse.set_visible(False)
playButton1.reset_position()
playButton2.reset_position()
playButton3.reset_position()
playButton1.reset_speed()
playButton2.reset_speed()
playButton3.reset_speed()
for bTime in range(110):
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
menuDisplay.blit(bgTexture, (0, 0))
playButton1.move_button_in(bTime)
playButton2.move_button_in(bTime)
playButton3.move_button_in(bTime)
tLogo.move_logo_in(bTime)
mainDisplay.blit(menuDisplay, (0, 0))
pygame.display.update()
clock.tick(60)
def mm_out_animation():
pygame.mouse.set_visible(False)
playButton1.reset_speed()
playButton2.reset_speed()
playButton3.reset_speed()
for aTime in range(110):
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
menuDisplay.blit(bgTexture, (0, 0))
playButton1.move_button_out(aTime)
playButton2.move_button_out(aTime)
playButton3.move_button_out(aTime)
tLogo.move_logo_out(aTime)
mainDisplay.blit(menuDisplay, (0, 0))
pygame.display.update()
clock.tick(60)
def game_over_screen(level, lines, score):
global tetrominosBag
pygame.mouse.set_visible(True)
pygame.mixer.music.fadeout(500)
lvlTextNumber = fontBasic.render(str(level), False, white)
lnsTextNumber = fontBasic.render(str(lines), False, white)
scrTextNumber = fontBasic.render(str(score), False, white)
lvlTextNumberRect = lvlTextNumber.get_rect()
lnsTextNumberRect = lnsTextNumber.get_rect()
scrTextNumberRect = scrTextNumber.get_rect()
lvlTextNumberRect.center = (width // 2, 325)
lnsTextNumberRect.center = (width // 2, 425)
scrTextNumberRect.center = (width // 2, 525)
gameDisplay.blit(lvlText, lvlTextRectGO)
gameDisplay.blit(lvlTextNumber, lvlTextNumberRect)
gameDisplay.blit(lnsText, lnsTextRectGO)
gameDisplay.blit(lnsTextNumber, lnsTextNumberRect)
gameDisplay.blit(scrText, scrTextRectGO)
gameDisplay.blit(scrTextNumber, scrTextNumberRect)
gameDisplay.blit(gameOverText, gameOverTextRect)
gameDisplay.blit(mainMenuButtonTexture, mMButtonPos)
gameDisplay.blit(quitButtonTexture, qButtonPos)
mainDisplay.blit(gameDisplay, (0, 0))
pygame.display.update()
game_over = True
while game_over:
alt_down = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.MOUSEBUTTONUP:
mpos = pygame.mouse.get_pos()
if mMButtonPos[0] <= mpos[0] <= mMButtonPos[0] + gOButtonSize[0] and\
mMButtonPos[1] <= mpos[1] <= mMButtonPos[1] + gOButtonSize[1]:
tetrominosBag = tetrominos[:]
return True
elif qButtonPos[0] <= mpos[0] <= qButtonPos[0] + gOButtonSize[0] and\
qButtonPos[1] <= mpos[1] <= qButtonPos[1] + gOButtonSize[1]:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
def game():
global themeVolume, lastThemeVolume
pygame.mouse.set_visible(False)
pygame.mixer.music.play(-1)
gBoard = [[0] * 10 for i in range(22)]
activeTetromino = random_tetromino()
nextTetromino = random_tetromino()
tetrominoProps = [[4, 0], 0] # Coords, rotation
if activeTetromino == "I":
tetrominoProps[0][0] = 3
nextTetrominoPos = [displayDimensions[0] - (startPosOutline[0] - blockSize) // 2 - bsbg - blockSize,
startPosOutline[1] + 3 * bsbg]
if nextTetromino == "I":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 - 2 * bsbg
elif nextTetromino == "O":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 - bsbg
left_down = False
right_down = False
down_down = False
alt_down = False
down_countdown = 0
right_countdown1 = 0
left_countdown1 = 0
right_countdown2 = 0
left_countdown2 = 0
s_cycle_left = 0
s_cycle_right = 0
lines_total = 0
level = 0
score = 0
fallDelay = 33
updatedisplay = True
playing = True
while playing:
if not pygame.key.get_focused():
pause_menu(True)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
pause_menu(False)
elif event.key == pygame.K_m:
if themeVolume == 0:
if lastThemeVolume == 0:
themeVolume = 1
else:
themeVolume = lastThemeVolume
else:
lastThemeVolume = themeVolume
themeVolume = 0
pygame.mixer.music.set_volume(themeVolume)
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
down_down = True
elif event.key == pygame.K_UP or event.key == pygame.K_w:
if can_tetromino_rotate(gBoard, activeTetromino, tetrominoProps, 1):
tetrominoProps[1] += 1
if tetrominoProps[1] >= 3:
tetrominoProps[1] = -1
updatedisplay = True
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
right_down = True
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
left_down = True
elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
gBoard, gameOver = tp_tetromino_down(gBoard, activeTetromino, tetrominoProps)
if gameOver:
game_out_animation(gBoard)
go_to_main_menu = game_over_screen(level, lines_total, score)
if go_to_main_menu:
return True
activeTetromino = nextTetromino
nextTetromino = random_tetromino()
tetrominoProps = [[4, 0], 0]
if activeTetromino == "I":
tetrominoProps[0][0] = 3
nextTetrominoPos = [
displayDimensions[0] - (startPosOutline[0] - blockSize) // 2 - bsbg - blockSize,
startPosOutline[1] + 3 * bsbg]
if nextTetromino == "I":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2\
- 2 * bsbg
elif nextTetromino == "O":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 - bsbg
gBoard, lines = line_checker(gBoard, level, lines_total, score)
if lines == 1:
score += 40 * (level + 1)
elif lines == 2:
score += 100 * (level + 1)
elif lines == 3:
score += 300 * (level + 1)
elif lines == 4:
score += 1200 * (level + 1)
lines_total += lines
level = lines_total // 10
updatedisplay = True
elif event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
down_down = False
down_countdown = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
right_down = False
right_countdown1 = 0
right_countdown2 = 0
s_cycle_right = 0
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
left_down = False
left_countdown1 = 0
left_countdown2 = 0
s_cycle_left = 0
elif event.key == pygame.K_LALT:
alt_down = False
if left_down and not right_down:
if (s_cycle_left == 1 and left_countdown1 <= 0) or (s_cycle_left >= 2 and left_countdown2 <= 0)\
or s_cycle_left == 0:
if can_tetromino_move(gBoard, activeTetromino, tetrominoProps, -1):
tetrominoProps[0][0] -= 1
s_cycle_left += 1
left_countdown1 = sideWait1
left_countdown2 = sideWait2
updatedisplay = True
else:
left_countdown1 -= 1
left_countdown2 -= 1
if right_down and not left_down:
if (s_cycle_right == 1 and right_countdown1 <= 0) or (s_cycle_right >= 2 and right_countdown2 <= 0)\
or s_cycle_right == 0:
if can_tetromino_move(gBoard, activeTetromino, tetrominoProps, 1):
tetrominoProps[0][0] += 1
s_cycle_right += 1
right_countdown1 = sideWait1
right_countdown2 = sideWait2
updatedisplay = True
else:
right_countdown1 -= 1
right_countdown2 -= 1
if down_down:
if down_countdown <= 0:
if can_tetromino_fall(gBoard, activeTetromino, tetrominoProps):
tetrominoProps[0][1] += 1
else:
gBoard, gameOver = solidify_tetromino(gBoard, activeTetromino, tetrominoProps)
if gameOver:
game_out_animation(gBoard)
go_to_main_menu = game_over_screen(level, lines_total, score)
if go_to_main_menu:
return True
activeTetromino = nextTetromino # Having this here is bullshit
nextTetromino = random_tetromino()
tetrominoProps = [[4, 0], 0] # Coords, rotation
if activeTetromino == "I":
tetrominoProps[0][0] = 3
nextTetrominoPos = [
displayDimensions[0] - (startPosOutline[0] - blockSize) // 2 - bsbg - blockSize,
startPosOutline[1] + 3 * bsbg]
if nextTetromino == "I":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 \
- 2 * bsbg
elif nextTetromino == "O":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 - bsbg
gBoard, lines = line_checker(gBoard, level, lines_total, score)
if lines == 1:
score += 40 * (level + 1)
elif lines == 2:
score += 100 * (level + 1)
elif lines == 3:
score += 300 * (level + 1)
elif lines == 4:
score += 1200 * (level + 1)
lines_total += lines
level = lines_total // 10
down_countdown = downWait
updatedisplay = True
else:
down_countdown -= 1
if fallDelay <= 0:
if can_tetromino_fall(gBoard, activeTetromino, tetrominoProps):
tetrominoProps[0][1] += 1
else:
gBoard, gameOver = solidify_tetromino(gBoard, activeTetromino, tetrominoProps)
if gameOver:
game_out_animation(gBoard)
go_to_main_menu = game_over_screen(level, lines_total, score)
if go_to_main_menu:
return True
activeTetromino = nextTetromino
nextTetromino = random_tetromino()
tetrominoProps = [[4, 0], 0]
if activeTetromino == "I":
tetrominoProps[0][0] = 3
nextTetrominoPos = [
displayDimensions[0] - (startPosOutline[0] - blockSize) // 2 - bsbg - blockSize,
startPosOutline[1] + 3 * bsbg]
if nextTetromino == "I":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 \
- 2 * bsbg
elif nextTetromino == "O":
nextTetrominoPos[0] = displayDimensions[0] - (startPosOutline[0] - blockGap) // 2 - bsbg
gBoard, lines = line_checker(gBoard, level, lines_total, score)
if lines == 1:
score += 40 * (level + 1)
elif lines == 2:
score += 100 * (level + 1)
elif lines == 3:
score += 300 * (level + 1)
elif lines == 4:
score += 1200 * (level + 1)
lines_total += lines
level = lines_total // 10
if level < 10:
fallDelay = (11 - level) * 3
else:
fallDelay = 3
updatedisplay = True
else:
fallDelay -= 1
# Render everything
if updatedisplay:
updatedisplay = False
gameDisplay.blit(bgTexture, (0, 0))
draw_board_outline()
draw_board(gBoard)
if showGhostPiece:
draw_ghost_tetromino(gBoard, activeTetromino, tetrominoProps)
draw_tetromino(activeTetromino, tetrominoProps)
draw_sidebar(score, lines_total, level, nextTetromino, nextTetrominoPos)
mainDisplay.blit(gameDisplay, (0, 0))
pygame.display.update()
clock.tick(60)
def main_menu():
pygame.mouse.set_visible(True)
in_menu = True
while in_menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.MOUSEBUTTONUP:
mpos = pygame.mouse.get_pos()
if button1pos[0][0] <= mpos[0] <= button1pos[1][0] and button1pos[0][1] <= mpos[1] <= button1pos[1][1]:
mm_out_animation()
game_in_animation()
mm = game()
if mm:
mm_in_animation()
main_menu()
elif button2pos[0][0] <= mpos[0] <= button2pos[1][0] and button2pos[0][1] <= mpos[1] <= button2pos[1][1]:
mm_out_animation()
settings_menu()
mm_in_animation()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
def pause_menu(unpause_on_focus):
pygame.mouse.set_visible(True)
themeplaytime = pygame.mixer.music.get_pos()
pygame.mixer.music.fadeout(250)
paused = True
pauseDisplay.fill((255, 255, 255, 100))
mainDisplay.blit(pauseDisplay, (0, 0))
pygame.display.update()
while paused:
if unpause_on_focus and pygame.key.get_focused():
paused = False
pygame.mouse.set_visible(False)
pygame.mixer.music.play(-1, themeplaytime)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitplz()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = False
pygame.mouse.set_visible(False)
pygame.mixer.music.play(-1, themeplaytime)
if event.key == pygame.K_LALT:
alt_down = True
elif event.key == pygame.K_F4 and alt_down:
quitplz()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LALT:
alt_down = False
def draw_settings():
mainDisplay.blit(bgTexture, (0, 0))
mainDisplay.blit(settingsText, settingsTextRect)
mainDisplay.blit(sgpText, sgpTextPos)