def __init__(self, axial_coordinates, radius, tile_id): super(HexTile, self).__init__() self.axial_coordinates = np.array([axial_coordinates]) self.cube_coordinates = hx.axial_to_cube(self.axial_coordinates) self.position = hx.axial_to_pixel(self.axial_coordinates, radius) self.radius = radius self.tile_id = tile_id
def __init__(self, axial_coordinates, color, radius, hollow = False): self.axial_coordinates = np.array([axial_coordinates]) self.cube_coordinates = hx.axial_to_cube(self.axial_coordinates) self.position = hx.axial_to_pixel(self.axial_coordinates, radius) self.color = color self.radius = radius self.image = make_hex_surface(color, radius, hollow = hollow)
def __init__(self, axial_coordinates, border_color, radius): self.axial_coordinates = np.array([axial_coordinates]) self.cube_coordinates = hx.axial_to_cube(self.axial_coordinates) self.position = hx.axial_to_pixel(self.axial_coordinates, radius) self.color = border_color self.radius = radius self.image = make_hex_surface((0, 0, 0, 140), self.radius, self.color, hollow=True)
def test_axial_to_pixel_conversion(): axial_coords = hx.cube_to_axial(coords) pixel_coords = hx.axial_to_pixel(axial_coords, radius) pixel_to_axial_coords = hx.pixel_to_axial(pixel_coords, radius) assert np.array_equal(axial_coords, pixel_to_axial_coords)
def draw(self): if self.step == 1: b_hexagons = list(self.b_map.values()) b_hex_positions = np.array([hexagon.get_draw_position() for hexagon in b_hexagons]) b_sorted_indexes = np.argsort(b_hex_positions[:, 1]) for index in b_sorted_indexes: self.main_surf.blit(b_hexagons[index].image, (b_hex_positions[index] + self.center).astype(int)) #Draws the hexagons on #hexagons[index].image uses an image created in example_hex from make_hex_surface if self.selection.value != self.old_selection or self.max_coord.value != self.old_max: self.hex_map = Selection.get_selection(self.selection.value, self.max_coord.value, self.hex_radius) self.old_selection = self.selection.value self.old_max = self.max_coord.value # show all hexes hexagons = list(self.hex_map.values()) hex_positions = np.array([hexagon.get_draw_position() for hexagon in hexagons]) sorted_indexes = np.argsort(hex_positions[:, 1]) for index in sorted_indexes: self.main_surf.blit(hexagons[index].image, (hex_positions[index] + self.center).astype(int)) #Draws the hexagons on #hexagons[index].image uses an image created in example_hex from make_hex_surface selection_type_text = self.font.render( "Board Shape: " + Selection.Type.to_string(self.selection.value), True, (50, 50, 50)) self.main_surf.blit(selection_type_text, (5, 30)) if self.step == 2: hexagons = list(self.hex_map.values()) hex_positions = np.array([hexagon.get_draw_position() for hexagon in hexagons]) sorted_indexes = np.argsort(hex_positions[:, 1]) for index in sorted_indexes: self.main_surf.blit(hexagons[index].image, (hex_positions[index] + self.center).astype(np.int32)) if self.direction_hex.size > 0: pixels = self.hex_map[self.clicked_hex][0].get_draw_position() + self.center self.main_surf.blit(self.selected_hex_image, pixels.astype(np.int32)) # This index is used to find the two angles for the directional triangle. index = self.direction_selection.value # Find the radian angles of the direction, and scale to the hex radius angles_in_radians = np.deg2rad([60 * i + 30 for i in range(index, index + 2)]) x = self.hex_radius * np.cos(angles_in_radians) y = self.hex_radius * np.sin(angles_in_radians) # Merge all points to a single array of a triangle points = np.round(np.vstack([x, y]).T) points = np.round(np.vstack([points, [0, 0]])) # Find pixel coordinates for the triangle, then find the middle point of the far edge, and draw the line. coords = (points + hx.axial_to_pixel(self.clicked_hex, self.hex_radius)) start_point = coords[2] + self.center end_point = (coords[0] + coords[1]) / 2 + self.center pg.draw.line(self.main_surf, [230, 230, 0], start_point.astype(np.int32), end_point.astype(np.int32), 3) player_text = self.font.render( "Current Player: " + str(self.player_selection.value), True, (50, 50, 50)) piece_selection_text = self.font.render( "Piece Type: " + str(self.piece_selection.value), True, (50, 50, 50)) for piece1 in self.player_list[1]: text = self.font.render(str(piece1[0]), False, COLORS[1]) pos = hx.axial_to_pixel(piece1[1], self.hex_radius) text_pos = pos + self.center text_pos -= (text.get_width() / 2, text.get_height() / 2) self.main_surf.blit(text, text_pos.astype(np.int32)) # This index is used to find the two angles for the directional triangle. index = piece1[2] # Find the radian angles of the direction, and scale to the hex radius angles_in_radians = np.deg2rad([60 * i + 30 for i in range(index, index + 2)]) x = self.hex_radius * np.cos(angles_in_radians) y = self.hex_radius * np.sin(angles_in_radians) # Merge all points to a single array of a triangle points = np.round(np.vstack([x, y]).T) points = np.round(np.vstack([points, [0, 0]])) # Find pixel coordinates for the triangle, then find the middle point of the far edge, and draw the line. coords = (points + hx.axial_to_pixel(piece1[1], self.hex_radius)) start_point = coords[2] + self.center end_point = (coords[0] + coords[1]) / 2 + self.center pg.draw.line(self.main_surf, [230, 230, 0], start_point.astype(np.int32), end_point.astype(np.int32), 3) for piece2 in self.player_list[2]: text = self.font.render(str(piece2[0]), False, COLORS[2]) pos = hx.axial_to_pixel(piece2[1], self.hex_radius) text_pos = pos + self.center text_pos -= (text.get_width() / 2, text.get_height() / 2) self.main_surf.blit(text, text_pos.astype(np.int32)) # This index is used to find the two angles for the directional triangle. index = piece2[2] # Find the radian angles of the direction, and scale to the hex radius angles_in_radians = np.deg2rad([60 * i + 30 for i in range(index, index + 2)]) x = self.hex_radius * np.cos(angles_in_radians) y = self.hex_radius * np.sin(angles_in_radians) # Merge all points to a single array of a triangle points = np.round(np.vstack([x, y]).T) points = np.round(np.vstack([points, [0, 0]])) # Find pixel coordinates for the triangle, then find the middle point of the far edge, and draw the line. coords = (points + hx.axial_to_pixel(piece2[1], self.hex_radius)) start_point = coords[2] + self.center end_point = (coords[0] + coords[1]) / 2 + self.center pg.draw.line(self.main_surf, [230, 230, 0], start_point.astype(np.int32), end_point.astype(np.int32), 3) self.main_surf.blit(player_text, (5, 20)) self.main_surf.blit(piece_selection_text, (5, 40)) # Update screen at 30 frames per second pg.display.update() self.main_surf.fill(COLORS[-1]) self.clock.tick(30)
if np.unique(A == B) == [True]: return True return False radius = 10 coords = hx.get_spiral(np.array((0, 0, 0)), 1, 10) # check axial <-> cube conversions work axial_coords = hx.cube_to_axial(coords) cube_coords = hx.axial_to_cube(axial_coords) print same_mat(coords, cube_coords) # check axial <-> pixel conversions work pixel_coords = hx.axial_to_pixel(axial_coords, radius) pixel_to_axial_coords = hx.pixel_to_axial(pixel_coords, radius) print same_mat(axial_coords, pixel_to_axial_coords) # check cube <-> pixel conversions work pixel_coords = hx.cube_to_pixel(cube_coords, radius) pixel_to_cube_coords = hx.pixel_to_cube(pixel_coords, radius) pixel_to_cube_to_axial_coords = hx.cube_to_axial(pixel_to_cube_coords) print same_mat(cube_coords, pixel_to_cube_coords) # check that we can correctly retrieve hexes after conversions hm[axial_coords] = coords retrieved = hm[pixel_to_cube_to_axial_coords] print same_mat(retrieved, cube_coords)
def draw(self): hexagons = list(self.hex_map.values()) hex_positions = np.array( [hexagon.get_draw_position() for hexagon in hexagons]) sorted_indexes = np.argsort(hex_positions[:, 1]) # Draw game hexagons self.test_surf.fill(COLORS[-1]) for index in sorted_indexes: self.test_surf.blit(hexagons[index].image, (hex_positions[index] + self.test_center).astype(np.int32)) if TESTING: v_coords = hexagons[index].get_axial_coords()[0] c_text = self.test_font.render( str(v_coords[0]) + "," + str(v_coords[1]), True, (255, 255, 255)) c_width = self.test_font.size( str(v_coords[0]) + "," + str(v_coords[1]))[0] self.test_surf.blit( c_text, (hexagons[index].get_position() + self.test_center - c_width / 2).astype(np.int32)) #self.test_surf.fill(COLORS[-1]) # Draw pieces that have already moved. if not np.array_equal(self.board.moved_pieces, []): moved_hexes = self.hex_map[np.array(self.board.moved_pieces)] list(map(self.draw_moved, moved_hexes)) # Draw pieces that have already attacked. if not np.array_equal(self.board.fired_pieces, []): attacked_hexes = self.hex_map[np.array(self.board.fired_pieces)] list(map(self.draw_attack, attacked_hexes)) # Draw pieces for piece in self.board.values(): w = piece.piece.p_type if w != 0: # This index is used to find the two angles for the directional triangle. index = np.where(DIRECTIONS == piece.piece.direction)[0][0] # Find the radian angles of the direction, and scale to the hex radius angles_in_radians = np.deg2rad( [60 * i + 30 for i in range(index, index + 2)]) x = self.hex_radius * np.cos(angles_in_radians) y = self.hex_radius * np.sin(angles_in_radians) # Merge all points to a single array of a triangle points = np.round(np.vstack([x, y]).T) points = np.round(np.vstack([points, [0, 0]])) # Find pixel coordinates for the triangle, then find the middle point of the far edge, and draw the line. coords = (points + hx.axial_to_pixel(piece.get_axial_coords(), self.hex_radius)) start_point = coords[2] + self.test_center end_point = (coords[0] + coords[1]) / 2 + self.test_center pg.draw.line(self.test_surf, [230, 230, 0], start_point.astype(np.int32), end_point.astype(np.int32), 3) # Draw piece text = self.font.render(str(w), False, COLORS[piece.piece.player]) #text.set_alpha(160) pos = hx.axial_to_pixel(piece.get_axial_coords(), self.hex_radius) text_pos = pos + self.test_center text_pos -= (text.get_width() / 2, text.get_height() / 2) self.test_surf.blit(text, text_pos.astype(np.int32)) # Draw valid moves if a piece is clicked if not np.array_equal(self.clicked_hex, None): # Get the valid moves for a piece, and display them. axials = np.array([i[0:2] for i in self.axial_moves]) visual_moves = self.hex_map[axials] self.valid_moves = self.axial_moves if self.move_or_attack == 1: list(map(self.draw_movement, visual_moves)) elif self.move_or_attack == 2: list(map(self.draw_attack, visual_moves)) if not np.array_equal(self.selected_movement_directions, []): index = self.selected_movement_directions[ self.select_direction.value] angles_in_radians = np.deg2rad( [60 * i + 30 for i in range(index, index + 2)]) x = self.hex_radius * np.cos(angles_in_radians) y = self.hex_radius * np.sin(angles_in_radians) # Merge all points to a single array of a triangle points = np.round(np.vstack([x, y]).T) points = np.round(np.vstack([points, [0, 0]])) # Find pixel coordinates, and draw the triangle. coords = (points.astype(np.int32) + hx.axial_to_pixel( self.temp_axial[0][0:2], self.hex_radius)).astype(np.int32) pg.draw.polygon(self.test_surf, COLORS[1], coords + self.test_center, 0) # Draw health bars for piece in self.board.values(): w = piece.piece.p_type if w != 0: mh = piece.get_piece().max_health ch = piece.get_piece().health health_string = str(ch) + "/" + str(mh) health_text = self.health_font.render(health_string, True, (50, 50, 50)) piece_pixel_coords = hx.axial_to_pixel( piece.get_axial_coords(), self.hex_radius) piece_centered_coords = piece_pixel_coords + self.test_center piece_centered_coords -= (health_text.get_width() / 2, 1.4 * health_text.get_height()) piece_centered_coords = (int(piece_centered_coords[0]), int(piece_centered_coords[1])) self.test_surf.blit(health_text, piece_centered_coords) # Display current FPS fps_text = self.fps_font.render( " FPS: " + str(int(self.clock.get_fps())), True, (50, 50, 50)) player_text = self.player_font.render( " Player " + str(self.board.player) + " turn", True, COLORS[self.board.player]) self.main_surf.blit(fps_text, (5, 0)) # Display current player's turn player_width = self.player_font.size(" Player " + str(self.board.player) + " turn")[0] self.main_surf.blit( player_text, (int(self.center[0] - player_width / 2), int(20 * scale))) self.turn_button.draw(self.main_surf, (0, 0, 0)) self.main_surf.blit(self.test_surf, (0, int(100 * scale))) # Update window and keep background pg.display.update() self.main_surf.fill(COLORS[-1]) self.clock.tick(60)
def __init__(self, axial_coordinates, color, radius): self.axial_coordinates = np.array([axial_coordinates]) self.position = hx.axial_to_pixel(self.axial_coordinates, radius) self.color = color self.radius = radius self.image = make_hex_surface(color, radius, border=True)