-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
184 lines (158 loc) · 7.01 KB
/
grid.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from tetriminos import i_tetrimino,j_tetrimino,l_tetrimino,o_tetrimino, s_tetrimino, t_tetrimino, z_tetrimino
from random import randint
import copy
import pygame
from hand_visualizer import HandVisualizer
from mode_switcher import ModeSwitcher
from controls.controls import KeyboardControls, LeapControls, Events
class ScoreBoard:
def __init__(self):
self._points = 0
pygame.font.init()
self._font = pygame.font.SysFont('Arial', 36)
self.POSITION = POSITION_X, POSITIONY = (270, 20)
self._color = (255, 255, 255)
def add_points_from_rows(self, number_of_removed_rows):
self._points += 10 * (number_of_removed_rows ** 2)
def on_render(self, surface):
text_surface = self._font.render(str(self._points), True, self._color)
surface.blit(text_surface, self.POSITION)
class Grid:
def __init__(self):
self.HEIGHT = 24
self.WIDTH = 12
self._score_board = ScoreBoard()
self._grid_structure = self.init_grid_structure()
self._current_tetrimino = self.new_tetrimino()
self._shadowed_tetrimino = copy.deepcopy(self._current_tetrimino)
self._background_image = pygame.image.load('assets/background.png')
self._hand_visualizer = HandVisualizer()
self._paused = False
self._mode_switcher = ModeSwitcher(self._switch_controls_mode)
self._keyboard_controls = KeyboardControls()
self._leap_controls = LeapControls()
self._controls = self._keyboard_controls
self._keyboard_controls.active = True
self._shadowed_tetrimino.set_transparent(True)
def init_grid_structure(self):
"""Returns a grid structure without blocks"""
grid = []
for column in range(0, self.WIDTH):
col = []
for row in range(0, self.HEIGHT):
col.append(None)
grid.append(col)
return grid
def on_render(self, surface):
surface.blit(self._background_image, (0, 0))
self._hand_visualizer.on_render(surface)
for column in range(0, self.WIDTH):
for row in range(0, self.HEIGHT):
if self._grid_structure[column][row] is not None:
x_pos = column * self._grid_structure[column][row].SIZE
y_pos = row * self._grid_structure[column][row].SIZE
surface.blit(self._grid_structure[column][row].get_image(),
(x_pos, y_pos))
self.render_where_to_land(surface)
self._current_tetrimino.on_render(surface)
self._score_board.on_render(surface)
self._mode_switcher.on_render(surface)
if self._paused:
self.render_paused_text(surface)
def render_paused_text(self, surface):
fontsize = 70
font = pygame.font.SysFont('Arial', fontsize)
white = (255, 255, 255)
text_surface = font.render('Paused', True, white)
position = (60, 300)
surface.blit(text_surface, position)
def on_loop(self):
"""Returns if the game is finished and how many points were achieved"""
if not self._paused:
if self._current_tetrimino.is_termino_down(self._grid_structure):
self._current_tetrimino.attach_current_tetrimino_to_grid(
self._grid_structure)
self._current_tetrimino = self.new_tetrimino()
if self._current_tetrimino.is_in_allowed_state(self._grid_structure):
return (True, self._score_board._points)
self._shadowed_tetrimino = copy.deepcopy(self._current_tetrimino)
self._shadowed_tetrimino.set_transparent(True)
number_of_removed_rows = self.remove_full_rows()
self._score_board.add_points_from_rows(number_of_removed_rows)
else:
self._current_tetrimino.on_loop()
self._hand_visualizer.on_loop(self._controls)
return (False, 0)
def _switch_controls_mode(self):
if self._mode_switcher._leap_mode:
self._controls = self._leap_controls
self._leap_controls.active = True
self._keyboard_controls.active = False
else:
self._controls = self._keyboard_controls
self._leap_controls.active = False
self._keyboard_controls.active = True
def on_event(self, event):
if event.type is Events.PAUSE_TOGGLE:
self._pause_toggle(),
elif event.type is Events.PLAY:
self._play(),
elif event.type is Events.PAUSE:
self._pause()
self._controls.on_event(event)
self._mode_switcher.on_event(event)
if not self._paused:
self._current_tetrimino.on_event(event, self._grid_structure)
def new_tetrimino(self):
"""Returns a randomly generated tetrimino"""
random_brick = randint(0, 6)
if random_brick is 0:
return i_tetrimino.I_tetrimino(self, False)
if random_brick is 1:
return j_tetrimino.J_tetrimino(self, False)
if random_brick is 2:
return l_tetrimino.L_tetrimino(self, False)
if random_brick is 3:
return o_tetrimino.O_tetrimino(self, False)
if random_brick is 4:
return s_tetrimino.S_tetrimino(self, False)
if random_brick is 5:
return t_tetrimino.T_tetrimino(self, False)
if random_brick is 6:
return z_tetrimino.Z_tetrimino(self, False)
def remove_full_rows(self):
number_of_removed_rows = 0
for y in range(0, self.HEIGHT):
row_full = True
for x in range(0, self.WIDTH):
if self._grid_structure[x][y] is None:
row_full = False
if row_full:
self.remove_row(y)
number_of_removed_rows += 1
return number_of_removed_rows
def remove_row(self, row):
for x in range(0, self.WIDTH):
self._grid_structure[x][row] = None
for tmp in range(0, row - 1):
'It iterates from the bottom to the top'
y = row - 1 - tmp
for x in range(0, self.WIDTH):
self._grid_structure[x][y + 1] = self._grid_structure[x][y]
self._grid_structure[x][y] = None
def render_where_to_land(self, surface):
"""Renders a shadow showing where the tetrimino will land"""
self._shadowed_tetrimino._position = self._current_tetrimino._position
self._shadowed_tetrimino._x = self._current_tetrimino._x
self._shadowed_tetrimino._y = self._current_tetrimino._y
self._shadowed_tetrimino._rotation = self._current_tetrimino._rotation
while not self._shadowed_tetrimino.is_termino_down(
self._grid_structure):
self._shadowed_tetrimino._y += 1
self._shadowed_tetrimino.on_render(surface)
def _pause_toggle(self):
self._play() if self._paused else self._pause()
def _play(self):
self._paused = False
def _pause(self):
self._paused = True