-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.py
304 lines (280 loc) · 9.4 KB
/
graphics.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import pygame as pg
import numpy as np
PIECES = [
[
np.array([
[1, 1, 1, 1]
]),
np.array([
[1],
[1],
[1],
[1],
]),
],
[
np.array([
[1, 1],
[1, 1]
])
],
[
np.array([
[1, 1, 1],
[0, 0, 1],
]),
np.array([
[0, 1],
[0, 1],
[1, 1],
]),
np.array([
[1, 0, 0],
[1, 1, 1],
]),
np.array([
[1, 1],
[1, 0],
[1, 0],
]),
],
[
np.array([
[1, 1, 1],
[1, 0, 0],
]),
np.array([
[1, 1],
[0, 1],
[0, 1],
]),
np.array([
[0, 0, 1],
[1, 1, 1],
]),
np.array([
[1, 0],
[1, 0],
[1, 1],
]),
],
[
np.array([
[0, 1, 1],
[1, 1, 0],
]),
np.array([
[1, 0],
[1, 1],
[0, 1],
]),
],
[
np.array([
[1, 1, 1],
[0, 1, 0],
]),
np.array([
[0, 1],
[1, 1],
[0, 1],
]),
np.array([
[0, 1, 0],
[1, 1, 1],
]),
np.array([
[1, 0],
[1, 1],
[1, 0],
]),
],
[
np.array([
[1, 1, 0],
[0, 1, 1],
]),
np.array([
[0, 1],
[1, 1],
[1, 0],
]),
],
]
ROTATION_TABLE = [
[(2, -2), (-2, 2)], # I
[(0, 0)], # O
[(0, -1), (0, 0), (1, 0), (-1, 1)], # J
[(0, -1), (0, 0), (1, 0), (-1, 1)], # L
[(1, -1), (-1, 1)], # S
[(0, -1), (0, 0), (1, 0), (-1, 1)], # T
[(1, -1), (-1, 1)], # Z
]
SPAWNS = [(3, 1), (4, 0), (3, 0), (3, 0), (3, 0), (3, 0), (3, 0)]
class Graphic():
def __init__(self, width, bg_color_1, bg_color_2, grid_color, board, fps=10):
pg.init()
pg.font.init()
self.clock = pg.time.Clock()
self.fps = fps
self.board = board
self.current_piece = -1
self.next_pieces = []
self.action_list = []
self.score = 0
self.num_columns, self.num_rows = board.shape[1], board.shape[0]
self.width = width
self.height = self.width * self.num_rows / self.num_columns
self.side_panel_cols = 6
self.bg_color_1 = bg_color_1
self.bg_color_2 = bg_color_2
self.grid_color = grid_color
self.block_width = self.width / self.num_columns
self.block_height = self.height / self.num_rows
self.display = pg.display.set_mode((self.width + self.side_panel_cols*self.block_width, self.height))
pg.display.set_caption('NEAT Tetris')
self.font = pg.font.SysFont("CMU Serif Roman", int(self.block_width*0.7))
self.ID_2_RGB = {
1 : (49, 199, 239),
2 : (247, 211, 8),
3 : (90, 101, 173),
4 : (239, 121, 33),
5 : (66, 182, 66),
6 : (173, 77, 156),
7 : (239, 32, 41)
}
def fill_gradient(self, surface, color, gradient, rect=None, vertical=True, forward=True):
"""fill a surface with a gradient pattern
Parameters:
color -> starting color
gradient -> final color
rect -> area to fill; default is surface's rect
vertical -> True=vertical; False=horizontal
forward -> True=forward; False=reverse
"""
if rect is None: rect = surface.get_rect()
x1,x2 = rect.left, rect.right
y1,y2 = rect.top, rect.bottom
if vertical: h = y2-y1
else: h = x2-x1
if forward: a, b = color, gradient
else: b, a = color, gradient
rate = (
float(b[0]-a[0])/h,
float(b[1]-a[1])/h,
float(b[2]-a[2])/h
)
fn_line = pg.draw.line
if vertical:
for line in range(y1,y2):
color = (
min(max(a[0]+(rate[0]*(line-y1)),0),255),
min(max(a[1]+(rate[1]*(line-y1)),0),255),
min(max(a[2]+(rate[2]*(line-y1)),0),255)
)
fn_line(surface, color, (x1,line), (x2,line))
else:
for col in range(x1,x2):
color = (
min(max(a[0]+(rate[0]*(col-x1)),0),255),
min(max(a[1]+(rate[1]*(col-x1)),0),255),
min(max(a[2]+(rate[2]*(col-x1)),0),255)
)
fn_line(surface, color, (col,y1), (col,y2))
def draw_grid(self):
for row in range(2, self.num_rows+1):
pg.draw.line(self.display, self.grid_color, (0, row * self.block_height), (self.width, row * self.block_height), 2)
for col in range(self.num_columns+1):
pg.draw.line(self.display, self.grid_color, (col * self.block_width, 2*self.block_height), (col * self.block_width, self.height), 2)
def draw_board(self):
for row in range(self.num_rows):
for col in range(self.num_columns):
if self.board[row][col] != 0:
color = self.ID_2_RGB[self.board[row][col]]
pg.draw.rect(self.display, color, (col * self.block_width, row * self.block_height, self.block_width, self.block_height), 0)
def draw_piece(self, piece, pos, rotation):
"""Draws a piece on the board
Args:
piece (int): piece to draw
pos (tuple): row and columns where to draw the piece on the board
rotation (int): rotation of the piece
"""
color = self.ID_2_RGB[piece]
row, col = pos
p = PIECES[piece-1]
rotation = rotation%len(p)
p = p[rotation]
for i in range(p.shape[0]):
for j in range(p.shape[1]):
if p[i][j] != 0:
pg.draw.rect(self.display, color, ((col+j) * self.block_width, (row+i) * self.block_height, self.block_width, self.block_height), 0)
def draw_side_panel_pieces(self):
"""Draws current piece and next pieces on the side panel
"""
self.draw_piece(self.current_piece, (1, self.num_columns+1), rotation=0)
pg.draw.rect(self.display, self.grid_color, ((self.num_columns+1)*self.block_width, self.block_height, (self.side_panel_cols-2)*self.block_width, 2*self.block_height), 2)
pg.draw.rect(self.display, self.grid_color, ((self.num_columns+1)*self.block_width, 4*self.block_height, (self.side_panel_cols-2)*self.block_width, 11*self.block_height), 2)
for i, piece in enumerate(self.next_pieces):
self.draw_piece(piece, (3*i+4, self.num_columns+1), rotation=1)
def show_score(self):
score_text = self.font.render(f'{self.score:012}', True, (255, 255, 255))
pos = ((self.num_columns + 0.8)*self.block_width, 5)
self.display.blit(score_text, pos)
def animate_piece(self):
"""Animates the piece falling down the board
Args:
action_list (list): list of actions to perform
"""
x_offset, y_offset = SPAWNS[self.current_piece-1]
rot = 0
self.draw_piece(self.current_piece, (y_offset, x_offset), rotation=rot)
pg.display.update()
for action in self.action_list:
if action == "SoftDrop":
self.clock.tick(self.fps)
self.clock.tick(self.fps)
y_offset += 1
elif action == "MoveLeft":
x_offset -= 1
elif action == "MoveRight":
x_offset += 1
elif action == "RotateCounterclockwise":
rot_num = len(ROTATION_TABLE[self.current_piece - 1])
offsets = ROTATION_TABLE[self.current_piece - 1][rot - 1]
rot = (rot - 1) % rot_num
x_offset -= offsets[0]
y_offset -= offsets[1]
elif action == "RotateClockwise":
rot_num = len(ROTATION_TABLE[self.current_piece - 1])
offsets = ROTATION_TABLE[self.current_piece - 1][rot]
rot = (rot + 1) % rot_num
x_offset += offsets[0]
y_offset += offsets[1]
else:
assert False, "Unknown action!"
self.fill_gradient(self.display, self.bg_color_1, self.bg_color_2, vertical=False, forward=True)
self.draw_piece(self.current_piece, (y_offset, x_offset), rotation=rot)
self.draw_board()
self.draw_grid()
self.show_score()
self.draw_side_panel_pieces()
pg.display.update()
self.clock.tick(self.fps)
def draw(self):
self.animate_piece()
def tick(self):
self.clock.tick(self.fps)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
if __name__ == "__main__":
board = np.zeros((22, 10))
graphic = Graphic(200, (0, 0, 0), (0, 0,0), (255, 255, 255), board, fps=10)
graphic.current_piece = 3
graphic.score = 123456789000
graphic.next_pieces = [3]
graphic.action_list = [(0, 0, 0), (0, 1, 0), (0, 2, 0), (0, 3, 1), (0, 4, 1), (0, 5, 1), (0, 6, 1), (0, 7, 1), (0, 8, 1), (0, 9, 1)]
while True:
graphic.draw()
graphic.tick()