forked from a-kipp/TuxTris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
figure.py
125 lines (111 loc) · 4.28 KB
/
figure.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import random
class Figure:
# (x, y)
figures = {
1: [(-1, 0), (0, 0), (1, 0), (1, 1)], # L-shape left-handed
2: [(-1, 0), (0, 0), (1, 0), (1, -1)], # L-shape right-handed
3: [(0, 0), (0, -1), (1, -1), (-1, 0)], # S-shape left-handed
4: [(0, 0), (0, 1), (1, 1), (-1, 0)], # S-shape right-handed
5: [(0, 0), (0, -1), (0, 1), (1, 0)], # T-shape
6: [(0, 0), (0, 1), (1, 0), (1, 1)], # square
7: [(0, -1), (0, 0), (0, 1), (0, 2)], # straight bar
}
figure = None
is_falling = None
def __init__(self, max_x, max_y, config):
self.max_x = max_x
self.max_y = max_y
self.figure = self.figures[random.randint(1, len(self.figures))]
self.figure_height = self.figure[len(self.figure) - 1][1] + 1
self.figure_width = self.figure[len(self.figure) - 1][0] + 1
self.config = config
self.is_dead = False
self.x = random.randint(0, max_x - self.figure_width)
self.y = 0
self.pushback_to_grid()
if self.is_colliding():
raise Exception("Initial collision")
def update_dimensions(self):
self.figure_height = self.figure[len(self.figure) - 1][1] + 1
self.figure_width = self.figure[len(self.figure) - 1][0] + 1
def destroy_me(self):
self.y -= 1
for block in self.figure:
self.config.grid[self.y + block[1]][self.x + block[0]] = "X"
self.is_dead = True
def pushback_to_grid(self):
"""push the tetromino back to the grid if partially or fully outside of boundaries"""
is_outside_boundaries = True
while is_outside_boundaries:
for block in self.figure:
if self.x + block[0] < 0:
self.x += 1
continue
if self.x + block[0] >= self.max_x:
self.x -= 1
continue
if self.y + block[1] >= self.max_y:
self.y -= 1
continue
if self.y + block[1] < 0:
self.y += 1
continue
is_outside_boundaries = False
def is_colliding(self):
"""check for collision with settled tetrominos on the grid"""
for block in self.figure:
if self.config.grid[self.y + block[1]][self.x + block[0]] == "X":
return True
return False
def move_step_down(self):
if not self.is_dead:
self.pushback_to_grid()
self.y += 1
for block in self.figure:
if self.y + block[1] >= self.max_y:
self.destroy_me()
break
if not self.is_dead:
if self.is_colliding():
self.destroy_me()
def move_to_bottom(self):
while not self.is_dead:
self.move_step_down()
def move_right(self):
self.x += 1
self.pushback_to_grid()
if self.is_colliding():
self.x -= 1
def move_left(self):
self.x -= 1
self.pushback_to_grid()
if self.is_colliding():
self.x += 1
def rotate_right(self):
if self.y + 1 >= self.figure[len(self.figure) - 1][0] + 1:
self.figure = [
(self.figure[i][1], -self.figure[i][0]) for i in range(len(self.figure))
]
self.pushback_to_grid()
if self.is_colliding():
# force rotate left
self.figure = [
(-self.figure[i][1], self.figure[i][0]) for i in range(len(self.figure))
]
self.pushback_to_grid()
def rotate_left(self):
if self.y + 1 >= self.figure[len(self.figure) - 1][0] + 1:
self.figure = [
(-self.figure[i][1], self.figure[i][0]) for i in range(len(self.figure))
]
self.pushback_to_grid()
if self.is_colliding():
# force rotate right
self.figure = [
(self.figure[i][1], -self.figure[i][0]) for i in range(len(self.figure))
]
self.pushback_to_grid()
def draw(self, grid):
for pos in self.figure:
grid[self.y + pos[1]][self.x + pos[0]] = "X"
return grid