-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetromino.py
91 lines (73 loc) · 2.92 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
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
"""Module containg Tetromino class
Tetromino class is used in main file - game.py
It is responsible for:
- drawing fields (currently falling tetromino)
- detecting collision with gameboard (with borders and previously fallen blocks)
- moving fields (left/right/rotate/fall)
"""
import random
import pygame
import config
import drawable
class Tetromino(drawable.Drawable):
"""Implemented Tetromino:
4 x 4 array which holds actuall shape of Tetromino
x, y cooridantes of top left element of this array on gameboard
"""
def __init__(self, type, times_rotated=0, x=4, y=0):
"""Initializes falling tetromino."""
# cooridantes of [0][0] (top left element) of fields on gameboard
self.current_x = x
self.current_y = y
# self.fields is 4x4 array which holds current shape and rotation of tetromino
if type in config.TETROMINO_SHAPES:
self.fields = config.TETROMINO_SHAPES[type]
for _ in range(times_rotated):
self.rotate()
@staticmethod
def get_random_tetromino():
return Tetromino(
type=random.choice(list(config.TETROMINO_SHAPES)),
times_rotated=random.randint(0, 3),
x=4, y=0
)
def rotate(self, clockwise=True) -> None:
"""Roates bufor clockwise or counterclockwise"""
rotated_array = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for i in range(0, 4):
for j in range(0, 4):
rotated_array[i][j] = self.fields[3-j][i] if clockwise else self.fields[j][3-i]
self.fields = rotated_array
def change_position(self, pressed_key: int) -> None:
"""Changes position and/or fields coressponding to pressed key
pygame.K_XXX is an int
"""
if pressed_key == pygame.K_UP:
self.rotate()
elif pressed_key == pygame.K_LEFT:
self.current_x -= 1
elif pressed_key == pygame.K_RIGHT:
self.current_x += 1
elif pressed_key == pygame.K_DOWN:
self.current_y += 1
def undo_change_position(self, pressed_key: int) -> None:
"""Changes position and/or fields to previous value(s) coressponding to pressed key"""
if pressed_key == pygame.K_UP:
self.rotate(clockwise=False)
elif pressed_key == pygame.K_LEFT:
self.current_x += 1
elif pressed_key == pygame.K_RIGHT:
self.current_x -= 1
elif pressed_key == pygame.K_DOWN:
self.current_y -= 1
def draw(self, screen : pygame.Surface) -> None:
"""Draws 4 x 4 bufor of currently falling tetromino"""
for i, row in enumerate(self.fields):
for j, elem in enumerate(row):
if elem == config.BUFFER_BLOCK:
self.draw_single_block(screen, elem, self.current_x + j, self.current_y + i)