-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex45_piece.py
More file actions
80 lines (62 loc) · 2.35 KB
/
ex45_piece.py
File metadata and controls
80 lines (62 loc) · 2.35 KB
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
from curses import KEY_RIGHT, KEY_LEFT, KEY_DOWN
import ex45_config
# Base representation for each tetromino
# Pieces can be moved and rotated
CHAR = b'\xe2\x96\xa0'
class Piece(object):
tetromino_attributes = {
"I": ("cyan", "@", 4),
"J": ("blue", "#", 3),
"L": ("white", "$", 3),
"O": ("yellow", "%", 2),
"S": ("green", "&", 3),
"T": ("magenta", "+", 3),
"Z": ("red", "=", 3)
}
symbol_to_color = {
"@": "cyan",
"#": "blue",
"$": "white",
"%": "yellow",
"&": "green",
"+": "magenta",
"=": "red",
"default": "default"
}
tetromino_orientations = {
"I": ex45_config.i_rotate_map,
"J": ex45_config.j_rotate_map,
"L": ex45_config.l_rotate_map,
"O": ex45_config.o_rotate_map,
"S": ex45_config.s_rotate_map,
"T": ex45_config.t_rotate_map,
"Z": ex45_config.z_rotate_map
}
def __init__(self, tetromino, location=None):
self.tetromino = tetromino
self.color, self.symbol, self.extent = self.tetromino_attributes[tetromino]
self.orientation = 0
self.location = location # [[y,x], [y,x], [y,x], [y,x]]
# Takes in a key_press, returns coords
def move(self, key_press):
if key_press == KEY_RIGHT:
return [[point[0], point[1] + 1] for point in self.location]
elif key_press == KEY_LEFT:
return [[point[0], point[1] - 1] for point in self.location]
elif key_press == KEY_DOWN:
return [[point[0] + 1, point[1]] for point in self.location]
# http://tetris.wikia.com/wiki/SRS
def rotate_clockwise(self):
orientation_transform = self._next_orientation()
return [[point[0] + transform[0], point[1] + transform[1]]
for point, transform in zip(self.location, orientation_transform)]
# https://stackoverflow.com/a/1996601 &
# https://stackoverflow.com/a/1996506
# return [[1 - (point[1] - (self.extent - 2)), point[0]]
# for point in self.location]
# return [[point[1], 1 - (point[0] - (self.extent - 2))]
# for point in self.location]
def _next_orientation(self):
mapping = self.tetromino_orientations[self.tetromino]
transform = mapping[self.orientation]
return transform