-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetromino.py
62 lines (47 loc) · 1.59 KB
/
tetromino.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
"""
This module is the parent of the all blocks
"""
import pygame
from pygame.math import Vector2
import constants
class Block:
"""Class represent one block"""
def __init__(self, id):
"""id: categorie of the tetromino (L, I, J, O, T, S, Z)"""
self.id = id
self.colors = constants.COLORS
self.cells = {}
self.CELL_SIZE = 30
self.rotation_state = 0
self.row_offset = 0
self.col_offset = 0
def move(self, row, col):
"""Method to move the tetromino"""
self.row_offset += row
self.col_offset += col
def get_cell_pos(self):
"""Get the current position of one tetromino"""
tiles = self.cells[self.rotation_state % 4]
moved_tiles = []
for pos in tiles:
moved_tiles.append(
Vector2(pos.x + self.row_offset, pos.y + self.col_offset)
)
return moved_tiles
def rotate(self):
"""Change the rotation of the tetromino 90 deg"""
self.rotation_state += 1
def undo_rotation(self):
"""undo the rotation in invalid position"""
self.rotation_state -= 1
def draw(self, SCREEN, offset_x, offset_y):
"""Draw the block with his main color"""
tiles = self.get_cell_pos()
for tile in tiles:
tile_rect = pygame.Rect(
tile.y * self.CELL_SIZE + offset_x,
tile.x * self.CELL_SIZE + offset_y,
self.CELL_SIZE - 1,
self.CELL_SIZE - 1
)
pygame.draw.rect(SCREEN, self.colors[self.id], tile_rect)