def test_cube_to_pixel_conversion(): axial_coords = hx.cube_to_axial(coords) cube_coords = hx.axial_to_cube(axial_coords) 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) assert np.array_equal(cube_coords, pixel_to_cube_coords)
def test_the_converted_coords_and_dataset_coords_retrieve_the_same_data(): axial_coords = hx.cube_to_axial(coords) cube_coords = hx.axial_to_cube(axial_coords) 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) # check that we can correctly retrieve hexes after conversions hm[axial_coords] = coords retrieved = hm[pixel_to_cube_to_axial_coords] assert np.array_equal(retrieved, coords)
def draw(self): # show all hexes hexagons = list(self.hex_map.values()) hex_positions = np.array( [hexagon.get_draw_position() for hexagon in hexagons]) sorted_idxs = np.argsort(hex_positions[:, 1]) for idx in sorted_idxs: self.main_surf.blit(hexagons[idx].image, hex_positions[idx] + self.center) # draw values of hexes for hexagon in list(self.hex_map.values()): text = self.font.render(str(hexagon.value), False, (0, 0, 0)) text.set_alpha(160) text_pos = hexagon.get_position() + self.center text_pos -= (text.get_width() / 2, text.get_height() / 2) self.main_surf.blit(text, text_pos) mouse_pos = np.array([np.array(pg.mouse.get_pos()) - self.center]) cube_mouse = hx.pixel_to_cube(mouse_pos, self.hex_radius) # choose either ring or disk rad_hex = Selection.get_selection(self.selection_type, cube_mouse, self.rad, self.clicked_hex) rad_hex_axial = hx.cube_to_axial(rad_hex) hexes = self.hex_map[rad_hex_axial] list( map( lambda hexagon: self.main_surf.blit( self.selected_hex_image, hexagon.get_draw_position() + self.center), hexes)) # draw hud self.selection_type selection_type_text = self.font.render( "(Right Click To Change) Selection Type: " + Selection.Type.to_string(self.selection_type), True, (50, 50, 50)) radius_text = self.font.render( "(Scroll Mouse Wheel To Change) Radius: " + str(self.rad), True, (50, 50, 50)) fps_text = self.font.render(" FPS: " + str(int(self.clock.get_fps())), True, (50, 50, 50)) self.main_surf.blit(fps_text, (5, 0)) self.main_surf.blit(radius_text, (5, 15)) self.main_surf.blit(selection_type_text, (5, 30)) # Update screen pg.display.update() self.main_surf.fill(COLORS[-1]) self.clock.tick(30)
def __init__(self, size=(600, 600), hex_radius=22, caption="ExampleHexMap"): self.caption = caption self.size = np.array(size) self.width, self.height = self.size self.center = self.size / 2 self.hex_radius = hex_radius self.hex_apothem = hex_radius * np.sqrt(3) / 2 self.hex_offset = np.array( [self.hex_radius * np.sqrt(3) / 2, self.hex_radius]) self.hex_map = hx.HexMap() self.max_coord = 6 self.rad = 3 self.selected_hex_image = make_hex_surface((128, 128, 128, 160), self.hex_radius, (255, 255, 255), hollow=True) self.selection_type = 3 self.clicked_hex = np.array([0, 0, 0]) # Get all possible coordinates within `self.max_coord` as radius. coords = hx.get_spiral(np.array((0, 0, 0)), 1, self.max_coord) # Convert coords to axial coordinates, create hexes and randomly filter out some hexes. hexes = [] num_hexes = np.random.binomial(len(coords), .9) axial_coords = hx.cube_to_axial(coords) axial_coords = axial_coords[np.random.choice(len(axial_coords), num_hexes, replace=False)] for i, axial in enumerate(axial_coords): colo = list(COLORS[COL_IDX[i]]) colo.append(255) hexes.append(ExampleHex(axial, colo, hex_radius)) hexes[-1].set_value(i) # the number at the center of the hex self.hex_map[np.array(axial_coords)] = hexes self.main_surf = None self.font = None self.clock = None self.init_pg()
def get_selection(selection_type, max_range, hex_radius): hex_map = hx.HexMap() hexes = [] axial_coordinates = [] if selection_type == Selection.Type.RECT: for r in range(-max_range, max_range + 1): r_offset = r >> 1 for q in range(-max_range - r_offset, max_range - r_offset): c = [q, r] axial_coordinates.append(c) hexes.append(ExampleHex(c, [141, 207, 104, 255], hex_radius)) elif selection_type == Selection.Type.HEX: spiral_coordinates = hx.get_spiral(np.array((0, 0, 0)), 1, max_range) # Gets spiral coordinates from center axial_coordinates = hx.cube_to_axial(spiral_coordinates) for i, axial in enumerate(axial_coordinates): hex_color = list(COLORS[3]) # set color of hex hex_color.append(255) #set alpha to 255 hexes.append(ExampleHex(axial, hex_color, hex_radius)) elif selection_type == Selection.Type.TRIANGLE: top = int(max_range / 2) for q in range(0, max_range + 1): for r in range(0, max_range - q + 1): c = [q, r] axial_coordinates.append(c) hexes.append(ExampleHex(c, [141, 207, 104, 255], hex_radius)) elif selection_type == Selection.Type.RHOMBUS: q1 = int(-max_range / 2) q2 = int(max_range / 2) r1 = -max_range r2 = max_range # Parallelogram for q in range(q1, q2 + 1): for r in range(r1, r2 + 1): axial_coordinates.append([q, r]) hexes.append(ExampleHex([q, r], [141, 207, 104, 255], hex_radius)) elif selection_type == Selection.Type.CUSTOM: axial_coordinates.append([0, 0]) hexes.append(ExampleHex([0, 0], [141, 207, 104, 255], hex_radius)) hex_map[np.array(axial_coordinates)] = hexes # create hex map return hex_map
def __init__(self, size=(600, 600), hex_radius=22, caption="ExampleHexMap"): self.caption = caption self.size = np.array(size) self.width, self.height = self.size self.center = self.size / 2 self.hex_radius = hex_radius self.hex_map = hx.HexMap() self.max_coord = 6 self.rad = ClampedInteger(3, 1, 5) self.selected_hex_image = make_hex_surface((128, 128, 128, 160), self.hex_radius, (255, 255, 255), hollow=True) self.selection_type = CyclicInteger(3, 0, 3) self.clicked_hex = np.array([0, 0, 0]) # Get all possible coordinates within `self.max_coord` as radius. spiral_coordinates = hx.get_spiral(np.array((0, 0, 0)), 1, self.max_coord) # Convert `spiral_coordinates` to axial coordinates, create hexes and randomly filter out some hexes. hexes = [] num_shown_hexes = np.random.binomial(len(spiral_coordinates), .9) axial_coordinates = hx.cube_to_axial(spiral_coordinates) axial_coordinates = axial_coordinates[np.random.choice( len(axial_coordinates), num_shown_hexes, replace=False)] for i, axial in enumerate(axial_coordinates): hex_color = list(COLORS[COL_IDX[i]]) hex_color.append(255) hexes.append(ExampleHex(axial, hex_color, hex_radius)) hexes[-1].set_value(i) # the number at the center of the hex self.hex_map[np.array(axial_coordinates)] = hexes # pygame specific variables self.main_surf = None self.font = None self.clock = None self.init_pg()
def test_axial_to_cube_conversion(): axial_coords = hx.cube_to_axial(coords) cube_coords = hx.axial_to_cube(axial_coords) assert np.array_equal(coords, cube_coords)
import hexy as hx hm = hx.HexMap() def same_mat(A, B): 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)