Skip to content

Commit e6cf83e

Browse files
committed
Rotation attribute added. Now is possible to define which blocks make
sense to rotate.
1 parent 6d6c852 commit e6cf83e

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

block.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Block(object):
3030
Class for handling of tetris block
3131
"""
3232

33-
def __init__(self,shape,x,y,screen,color):
33+
def __init__(self,shape,x,y,screen,color,rotate_en):
3434
"""
3535
Initialize the tetris block class
3636
@@ -41,6 +41,7 @@ def __init__(self,shape,x,y,screen,color):
4141
- y - Y coordinate of first tetris shape block
4242
- screen - screen to draw on
4343
- color - the color of each shape block in RGB notation
44+
- rotate_en - enable or disable the rotation
4445
"""
4546
# The initial shape (convert all to Rect objects)
4647
self.shape = []
@@ -49,6 +50,8 @@ def __init__(self,shape,x,y,screen,color):
4950
by = sh[1]*constants.BHEIGHT + y
5051
block = pygame.Rect(bx,by,constants.BWIDTH,constants.BHEIGHT)
5152
self.shape.append(block)
53+
# Setup the rotation attribute
54+
self.rotate_en = rotate_en
5255
# Setup the rest of variables
5356
self.x = x
5457
self.y = y
@@ -134,8 +137,10 @@ def rotate(self):
134137
Setup the rotation value to 90 degrees.
135138
"""
136139
# Setup the rotation and update coordinates of all shape blocks.
137-
self.diff_rotation = 90
138-
self._update()
140+
# The block is rotated iff the rotation is enabled
141+
if self.rotate_en:
142+
self.diff_rotation = 90
143+
self._update()
139144

140145
def _update(self):
141146
"""

tetris.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ def __init__(self,bx,by):
5252
self.start_x = math.ceil(self.resx/2.0)
5353
self.start_y = constants.BOARD_UP_MARGIN + constants.BOARD_HEIGHT + constants.BOARD_MARGIN
5454
# Blocka data (shapes and colors). The shape is encoded in the list of [X,Y] points. Each point
55-
# represents the relative position.
55+
# represents the relative position. The true/false value is used for the configuration of rotation where
56+
# False means no rotate and True allows the rotation.
5657
self.block_data = (
57-
([[0,0],[1,0],[2,0],[3,0]],constants.RED), # I block
58-
([[0,0],[1,0],[0,1],[-1,1]],constants.GREEN), # S block
59-
([[0,0],[1,0],[2,0],[2,1]],constants.BLUE), # J block
60-
([[0,0],[0,1],[1,0],[1,1]],constants.ORANGE), # O block
61-
([[-1,0],[0,0],[0,1],[1,1]],constants.GOLD), # Z block
62-
([[0,0],[1,0],[2,0],[1,1]],constants.PURPLE), # T block
63-
([[0,0],[1,0],[2,0],[0,1]],constants.CYAN), # J block
58+
([[0,0],[1,0],[2,0],[3,0]],constants.RED,True), # I block
59+
([[0,0],[1,0],[0,1],[-1,1]],constants.GREEN,True), # S block
60+
([[0,0],[1,0],[2,0],[2,1]],constants.BLUE,True), # J block
61+
([[0,0],[0,1],[1,0],[1,1]],constants.ORANGE,False), # O block
62+
([[-1,0],[0,0],[0,1],[1,1]],constants.GOLD,True), # Z block
63+
([[0,0],[1,0],[2,0],[1,1]],constants.PURPLE,True), # T block
64+
([[0,0],[1,0],[2,0],[0,1]],constants.CYAN,True), # J block
6465
)
6566
# Compute the number of blocks. When the number of blocks is even, we can use it directly but
6667
# we have to decrese the number of blocks in line by one when the number is odd (because of the used margin).
@@ -303,7 +304,7 @@ def get_block(self):
303304
# Get the block and add it into the block list(static for now)
304305
tmp = random.randint(0,len(self.block_data)-1)
305306
data = self.block_data[tmp]
306-
self.active_block = block.Block(data[0],self.start_x,self.start_y,self.screen,data[1])
307+
self.active_block = block.Block(data[0],self.start_x,self.start_y,self.screen,data[1],data[2])
307308
self.blk_list.append(self.active_block)
308309
self.new_block = False
309310

0 commit comments

Comments
 (0)