33import pygame
44import random
55
6- colors = [
7- (0 , 0 , 0 ),
6+ colors = [(0 , 0 , 0 ),
87 (120 , 37 , 179 ),
98 (100 , 179 , 179 ),
109 (80 , 34 , 22 ),
1110 (80 , 134 , 22 ),
1211 (180 , 34 , 22 ),
13- (180 , 34 , 122 ),
14- ]
12+ (180 , 34 , 122 ),]
1513
1614
1715class Figure :
1816 x = 0
1917 y = 0
2018
2119 figures = [
22- [[ 1 , 5 , 9 , 13 ], [4 , 5 , 6 , 7 ]],
23- [[4 , 5 , 9 , 10 ], [2 , 6 , 5 , 9 ]],
24- [[ 6 , 7 , 9 , 10 ], [1 , 5 , 6 , 10 ]],
25- [[ 1 , 2 , 5 , 9 ], [ 0 , 4 , 5 , 6 ], [1 , 5 , 9 , 8 ], [4 , 5 , 6 , 10 ]],
26- [[ 1 , 2 , 6 , 10 ], [5 , 6 , 7 , 9 ], [2 , 6 , 10 , 11 ], [ 3 , 5 , 6 , 7 ]],
27- [[1 , 4 , 5 , 6 ], [1 , 4 , 5 , 9 ], [4 , 5 , 6 , 9 ], [1 , 5 , 6 , 9 ]],
28- [[1 , 2 , 5 , 6 ]],
20+ [[ 0 , 1 , 2 , 3 ], [ 1 , 5 , 9 , 13 ], [4 , 5 , 6 , 7 ], [ 2 , 6 , 10 , 14 ]], #I piece
21+ [[4 , 5 , 9 , 10 ], [2 , 6 , 5 , 9 ], [ 0 , 1 , 5 , 6 ], [ 1 , 5 , 4 , 8 ]], #Z piece
22+ [[ 1 , 2 , 4 , 5 ], [ 0 , 4 , 5 , 9 ], [ 5 , 6 , 8 , 9 ], [1 , 5 , 6 , 10 ]], #S piece
23+ [[ 0 , 4 , 5 , 6 ], [1 , 5 , 9 , 8 ], [4 , 5 , 6 , 10 ], [ 1 , 2 , 5 , 9 ]], #J piece
24+ [[ 3 , 5 , 6 , 7 ], [ 1 , 2 , 6 , 10 ], [5 , 6 , 7 , 9 ], [2 , 6 , 10 , 11 ]], #L piece
25+ [[1 , 4 , 5 , 6 ], [1 , 4 , 5 , 9 ], [4 , 5 , 6 , 9 ], [1 , 5 , 6 , 9 ]], #T piece
26+ [[1 , 2 , 5 , 6 ]], #O piece
2927 ]
3028
3129 def __init__ (self , x , y ):
@@ -38,8 +36,10 @@ def __init__(self, x, y):
3836 def image (self ):
3937 return self .figures [self .type ][self .rotation ]
4038
41- def rotate (self ):
39+ def lrotate (self ):
4240 self .rotation = (self .rotation + 1 ) % len (self .figures [self .type ])
41+ def rrotate (self ):
42+ self .rotation = (self .rotation - 1 ) % len (self .figures [self .type ])
4343
4444
4545class Tetris :
@@ -123,9 +123,14 @@ def go_side(self, dx):
123123 if self .intersects ():
124124 self .figure .x = old_x
125125
126- def rotate (self ):
126+ def lrotate (self ):
127127 old_rotation = self .figure .rotation
128- self .figure .rotate ()
128+ self .figure .lrotate ()
129+ if self .intersects ():
130+ self .figure .rotation = old_rotation
131+ def rrotate (self ):
132+ old_rotation = self .figure .rotation
133+ self .figure .rrotate ()
129134 if self .intersects ():
130135 self .figure .rotation = old_rotation
131136
@@ -167,8 +172,10 @@ def rotate(self):
167172 if event .type == pygame .QUIT :
168173 done = True
169174 if event .type == pygame .KEYDOWN :
170- if event .key == pygame .K_UP :
171- game .rotate ()
175+ if event .key == pygame .K_z :
176+ game .lrotate ()
177+ if event .key == pygame .K_x :
178+ game .rrotate ()
172179 if event .key == pygame .K_DOWN :
173180 pressing_down = True
174181 if event .key == pygame .K_LEFT :
@@ -218,3 +225,4 @@ def rotate(self):
218225 clock .tick (fps )
219226
220227pygame .quit ()
228+
0 commit comments