forked from ksu-is/Tetris-Game2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
47 lines (39 loc) · 1.63 KB
/
block.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
from colors import Colors
import pygame
from position import Position
class Block:
def __init__(self, id):
self.id = id
self.cells = {}
self.cell_size = 30
self.row_offset = 0
self.column_offset = 0
self.rotation_state = 0
self.colors = Colors.get_cell_colors()
#used to move the blocks
def move(self, rows, columns):
self.row_offset += rows
self.column_offset += columns
def get_cell_positions(self):
tiles = self.cells[self.rotation_state]
moved_tiles = []
for position in tiles:
position = Position(position.row + self.row_offset, position.column + self.column_offset)
moved_tiles.append(position)
return moved_tiles
#rotate blocks
def rotate(self):
self.rotation_state += 1
if self.rotation_state == len(self.cells):
self.rotation_state = 0
#check if block is inside game window during rotation
def undo_rotation(self):
self.rotation_state -= 1
if self.rotation_state == -1:
self.rotation_state = len(self.cells) - 1
#draws the block on the screen, make screen file (position.py) to make it easier to call
def draw(self, screen, offset_x, offset_y):
tiles = self.get_cell_positions()
for tile in tiles:
tile_rect = pygame.Rect(offset_x + tile.column * self.cell_size, offset_y + tile.row * self.cell_size, self.cell_size -1, self.cell_size -1)
pygame.draw.rect(screen, self.colors[self.id], tile_rect)