-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawElements.py
294 lines (264 loc) · 13.8 KB
/
drawElements.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
import pygame, math
import constants
#This file handles generation of all the sizes of the elements
#The values have been optimised for 1280*720 window area
MARGIN = 2
board_width = 620
board_box_width = 42
piece_box_width = (constants.WINDOW_WIDTH - board_width) / 2 #330
one_piece_box_width = piece_box_width / 2 #165
single_piece_width = 9
info_box_width = constants.WINDOW_WIDTH #1280
board_height = board_width #620
board_box_height = board_box_width
piece_box_height = board_height
one_piece_box_height = math.floor(board_height / 11) #56
single_piece_height = single_piece_width #9
info_box_height = constants.WINDOW_HEIGHT - board_height #100
board_origin = [piece_box_width, info_box_height] #[330, 100]
def draw_title_window():
pass
def grid_to_array_coords(pos):
col = int((pos[0] - (piece_box_width + MARGIN)) // (MARGIN + board_box_width))
row = int((pos[1] - (info_box_height + MARGIN)) // (MARGIN + board_box_height))
return [row, col]
def array_to_grid_coords():
pass
def init_gameboard(board_arr):
rects = []
for row in range(board_arr.shape[0]):
for column in range(board_arr.shape[1]):
box_width = piece_box_width + MARGIN + ((MARGIN + board_box_width) * column)
box_height = info_box_height + MARGIN + ((MARGIN + board_box_height) * row)
dims = [box_width, box_height, board_box_width, board_box_height]
rects.append(pygame.Rect(dims))
#print(rects)
return rects
def draw_gameboard(canvas, board_rects, gameboard, current_piece, player):
mouse_row, mouse_col = get_square_under_mouse()
counter = 0
board_arr = gameboard.board
is_valid_move = False
#This section generates the green margin when a valid move is spotted
if len(current_piece["rects"]) > 0:
if player.is_1st_move:
for rect in current_piece["rects"]:
if grid_to_array_coords([rect.centerx, rect.centery]) == \
constants.STARTING_PTS["player%s"%(player.number)]:
is_valid_move = True
else:
if gameboard.check_is_move_valid(current_piece["arr"], player, \
grid_to_array_coords([current_piece["rects"][0].centerx, current_piece["rects"][0].centery])):
is_valid_move = True
for row in range(board_arr.shape[0]):
for column in range(board_arr.shape[1]):
rect = board_rects[counter]
#Draw the green board box margin if the move is valid
for piece_rect in current_piece["rects"]:
if piece_rect.collidepoint(rect.centerx, rect.centery) and board_arr[row][column] == constants.BOARD_FILL_VALUE\
and is_valid_move:
rect.x -= MARGIN
rect.y -= MARGIN
rect.h += 2 * MARGIN
rect.w += 2 * MARGIN
pygame.draw.rect(canvas, constants.GREEN, rect)
rect.x += MARGIN
rect.y += MARGIN
rect.h -= 2 * MARGIN
rect.w -= 2 * MARGIN
#Board fill color
if board_arr[row][column] == constants.BOARD_FILL_VALUE:
pygame.draw.rect(canvas, constants.WHITE, rect)
elif board_arr[row][column] == constants.PLAYER1_VALUE:
pygame.draw.rect(canvas, constants.PURPLE, rect)
elif board_arr[row][column] == constants.PLAYER2_VALUE:
pygame.draw.rect(canvas, constants.ORANGE, rect)
#Blit the text to mark the starting points
if [row, column] == constants.STARTING_PTS["player1"]:
text = pygame.font.SysFont(None, 15).render("Player 1", True, constants.BLACK)
canvas.blit(text, [rect.x, rect.centery - 2])
elif [row, column] == constants.STARTING_PTS["player2"]:
text = pygame.font.SysFont(None, 15).render("Player 2", True, constants.BLACK)
canvas.blit(text, [rect.x, rect.centery - 2])
else:
#WARNING: This rendering within the loop slows down the game
if constants.VERBOSITY > 1:
text = pygame.font.SysFont(None, 15).render("(%s, %s)" % (row, column), True, constants.BLACK)
canvas.blit(text, [rect.x + 2, rect.centery - 2])
counter += 1
def get_square_under_mouse(rect_coords = None):
if rect_coords is None:
pos = pygame.Vector2(pygame.mouse.get_pos()) - board_origin
else:
pos = rect_coords
x, y = [int(v // (board_box_width + MARGIN)) for v in pos]
try:
if x >= 0 and y >= 0: return [y, x]
except IndexError: pass
return [-1, -1]
def init_piece_rects(p1_remaining_pieces, p2_remaining_pieces):
row, column = 0, 0
for piece in p1_remaining_pieces.keys():
piece_rects = []
for i in range(p1_remaining_pieces[piece]["arr"].shape[0]):
for j in range(p1_remaining_pieces[piece]["arr"].shape[1]):
if p1_remaining_pieces[piece]["arr"][i][j] == 1:
x = (one_piece_box_width * column) + ((MARGIN + single_piece_width) * j) + MARGIN
y = info_box_height + (one_piece_box_height * row) + ((MARGIN + single_piece_height) * i)
piece_rects.append(pygame.Rect([x, y, single_piece_width, single_piece_height]))
p1_remaining_pieces[piece]["rects"] = piece_rects
column += 1
if column == 2:
row += 1
column = 0
row, column = 0, 0
for piece in p2_remaining_pieces.keys():
piece_rects = []
for i in range(p2_remaining_pieces[piece]["arr"].shape[0]):
for j in range(p2_remaining_pieces[piece]["arr"].shape[1]):
if p2_remaining_pieces[piece]["arr"][i][j] == 1:
x = piece_box_width + board_width + (one_piece_box_width * column) + ((MARGIN + single_piece_width) * j) + MARGIN
y = info_box_height + (one_piece_box_height * row) + ((MARGIN + single_piece_height) * i)
piece_rects.append(pygame.Rect([x, y, single_piece_width, single_piece_height]))
p2_remaining_pieces[piece]["rects"] = piece_rects
column += 1
if column == 2:
row += 1
column = 0
def generate_element_offsets(remaining_pieces, event):
offset_list = []
selected = None
for key, val in remaining_pieces.items():
for r in val["rects"]:
r.x -= MARGIN
r.y -= MARGIN
if r.collidepoint(event.pos):
selected = key
r.x += MARGIN
r.y += MARGIN
break
r.x += MARGIN
r.y += MARGIN
if selected is not None:
break
if selected is not None:
for chosen_piece in remaining_pieces[selected]["rects"]:
selected_offset_x = chosen_piece.x - event.pos[0]
selected_offset_y = chosen_piece.y - event.pos[1]
offset_list.append([selected_offset_x, selected_offset_y])
return offset_list, selected
def draw_pieces(canvas, player1, player2, active_player, selected):
p1_pieces, p2_pieces = player1.remaining_pieces, player2.remaining_pieces
p1_color, p2_color = player1.color, player2.color
for key, val in p1_pieces.items():
if not (key == selected and player1.number == active_player.number):
for unit_sq in val["rects"]:
pygame.draw.rect(canvas, p1_color, unit_sq)
unit_sq.x -= MARGIN
unit_sq.y -= MARGIN
pygame.draw.rect(canvas, constants.WHITE, unit_sq, MARGIN)
unit_sq.x += MARGIN
unit_sq.y += MARGIN
for key, val in p2_pieces.items():
if not (key == selected and player2.number == active_player.number):
for unit_sq in val["rects"]:
pygame.draw.rect(canvas, p2_color, unit_sq)
unit_sq.x -= MARGIN
unit_sq.y -= MARGIN
pygame.draw.rect(canvas, constants.WHITE, unit_sq, MARGIN)
unit_sq.x += MARGIN
unit_sq.y += MARGIN
#Draws the selected current piece
def draw_selected_piece(canvas, offset_list, mouse_pos, current_piece, player_color):
counter = 0
rects = current_piece["rects"]
ref_x, ref_y = rects[0].x, rects[0].y
for i in range(current_piece["arr"].shape[0]):
for j in range(current_piece["arr"].shape[1]):
if current_piece["arr"][i][j] == 1:
rects[counter].x = mouse_pos[0] + offset_list[counter][0] + (board_box_width - single_piece_width) * j - MARGIN
rects[counter].y = mouse_pos[1] + offset_list[counter][1] + (board_box_height - single_piece_height) * i - MARGIN
rects[counter].h = board_box_height + (2 * MARGIN)
rects[counter].w = board_box_width + (2 * MARGIN)
pygame.draw.rect(canvas, constants.WHITE, rects[counter], MARGIN)
rects[counter].x += MARGIN
rects[counter].y += MARGIN
rects[counter].h -= 2 * MARGIN
rects[counter].w -= 2 * MARGIN
pygame.draw.rect(canvas, player_color, rects[counter])
counter += 1
def draw_rotated_flipped_selected_piece(current_piece):
ref_x, ref_y = current_piece["rects"][0].x, current_piece["rects"][0].y
current_piece["rects"], offset_list = [], []
mouse_pos = pygame.mouse.get_pos()
counter = 0
for i in range(current_piece["arr"].shape[0]):
for j in range(current_piece["arr"].shape[1]):
if current_piece["arr"][i][j] == 1:
x = ref_x + ((board_box_width - single_piece_width) * j) - MARGIN
y = ref_y + ((board_box_height - single_piece_height) * i) - MARGIN
h = board_box_height
w = board_box_width
current_piece["rects"].append(pygame.Rect(x,y,h,w))
selected_offset_x = x - ((board_box_width/2) * j) - mouse_pos[0] - MARGIN
selected_offset_y = y - ((board_box_height/2) * i) - mouse_pos[1] - MARGIN
offset_list.append([selected_offset_x, selected_offset_y])
counter += 1
return offset_list
#Checks if all of the squares of the current selected piece lie within the board
def are_squares_within_board(current_piece, board_rects):
is_within_board = False
for piece_rect in current_piece["rects"]:
for board_rect in board_rects:
if piece_rect.collidepoint(board_rect.centerx, board_rect.centery):
is_within_board = True
if not is_within_board:
return False
else:
is_within_board = False
return True
def draw_infobox(canvas, player1, player2, active_player):
text_dict = {"p1_score" : "Player 1 Score: %s" % (player1.score),
"p2_score" : "Player 2 Score: %s" % (player2.score),
"title" : "Blokus on Pygame"}
font = pygame.font.SysFont("Trebuchet MS", 30)
if active_player.number == 1:
current_player_rect = pygame.Rect((20, 20, piece_box_width-40, info_box_height-40)) #x=20, y=20, w=290, h=60
else:
current_player_rect = pygame.Rect((piece_box_width+board_width+20, 20, piece_box_width-40, info_box_height-40)) #x=1010, y=20, w=290, h=60
pygame.draw.rect(canvas, constants.GREEN, current_player_rect)
font_dict = {"p1_score" : font.render(text_dict["p1_score"], False, constants.WHITE),
"p2_score" : font.render(text_dict["p2_score"], False, constants.WHITE),
"title" : font.render(text_dict["title"], False, constants.WHITE)}
pos_rect_dict = {"p1_score" : pygame.Rect((0, 0, piece_box_width, info_box_height)), #x=0, y=0, w=330, h=100
"p2_score" : pygame.Rect((piece_box_width + board_width, 0, piece_box_width, info_box_height)), #x=990, y=0, w=330, h=100
"title" : pygame.Rect((0, 0, info_box_width, info_box_height))} #x=0, y=0, w=1280, h=100
pos_dict = {"p1_score" : pos_rect_dict["p1_score"].center,
"p2_score" : pos_rect_dict["p2_score"].center,
"title" : (pos_rect_dict["title"].midtop[0] + 13, pos_rect_dict["title"].midtop[1] + 13)}
for key, val in font_dict.items():
canvas.blit(val, val.get_rect(center = pos_dict[key]))
def draw_infobox_msg(canvas, player1, player2, msg_key):
game_over_text = ""
if msg_key == "game_over":
if player1.score > player2.score:
game_over_text = "Game over. Player %s wins!" % (player1.number)
elif player1.score < player2.score:
game_over_text = "Game over. Player %s wins!" % (player2.number)
else:
game_over_text = "Game over. It's a tie!"
text_dict = {"not_valid_move" : "Invalid move. This piece cannot be placed there",
"game_over" : game_over_text,
"ai_turn" : "AI's turn. Currently evaluating next move..."}
font = pygame.font.SysFont("Trebuchet MS", 25)
font_18 = pygame.font.SysFont("Trebuchet MS", 18)
font_dict = {"not_valid_move" : font.render(text_dict["not_valid_move"], False, constants.RED),
"game_over" : font.render(text_dict["game_over"], False, constants.GREEN),
"ai_turn" : font.render(text_dict["ai_turn"], False, constants.GREEN)}
pos_rect_dict = {"not_valid_move" : pygame.Rect((0, 0, info_box_width, info_box_height)), #x=0, y=0, w=1280, h=100
"game_over" : pygame.Rect((0, 0, info_box_width, info_box_height)), #x=0, y=0, w=1280, h=100
"ai_turn" : pygame.Rect((0, 0, info_box_width, info_box_height))} #x=10, y=0, w=1280, h=70
pos_dict = {"not_valid_move" : pos_rect_dict["not_valid_move"].center,
"game_over" : pos_rect_dict["game_over"].center,
"ai_turn" : pos_rect_dict["ai_turn"].center}
canvas.blit(font_dict[msg_key], font_dict[msg_key].get_rect(center = pos_dict[msg_key]))