Exemple #1
0
    def regenerate_size_objects(self):
        self.hex_map = hx.HexMap()
        temp_map_list = np.array([*self.board.values()])

        hexes = [
            VisualHex(coords.get_axial_coords(), COLORS[0], self.hex_radius)
            for coords in temp_map_list
        ]

        self.hex_map[np.array([c.get_axial_coords()
                               for c in temp_map_list])] = hexes

        self.movement_hex_image = make_hex_surface(
            (128, 128, 128, 255),  # Highlight color for a hexagon.
            self.hex_radius,  # radius of individual hexagon
            (255, 255, 255),  # Border color white
            hollow=True)

        self.attack_hex_image = make_hex_surface(
            (128, 128, 128, 255),  # Highlight color for a hexagon.
            self.hex_radius,  # radius of individual hexagon
            (255, 0, 0),  # Border color white
            hollow=True)

        self.moved_hex_image = make_hex_surface(
            (128, 128, 128, 255),  # Highlight color for a hexagon.
            self.hex_radius,  # radius of individual hexagon
            (128, 128, 128, 255))

        self.font = pg.font.SysFont("monospace", self.hex_radius + 7, True)
        self.health_font = pg.font.SysFont("arial", self.hex_radius - 7, True)
    def __init__(self, num_pieces, hex_radius=20, caption="Config File Creator"):

        root = Tk()
        size = (root.winfo_screenheight() - 50, root.winfo_screenheight() - 50)
        root.destroy()
        
        self.caption = caption              # Controls window caption
        self.size = np.array(size)          # Controls window size
        self.width, self.height = self.size # Width and height of window
        self.center = self.size / 2         # Should be center of window

        self.num_pieces = num_pieces

        self.hex_radius = int(hex_radius * self.size[0] / 1000)      # Radius of individual hexagons

        self.max_coord = ClampedInteger(13, 1, 13) # Controls the radius of the hex map in hexagon shape.
        self.selection = ClampedInteger(1, 0, 4)  # Controls map style selection in step 1.
        self.piece_selection = CyclicInteger(1, 1, num_pieces) # Controls piece type in step 2.
        self.player_selection = CyclicInteger(1, 1, 2) # Controls player in step 2.
        self.direction_selection = CyclicInteger(0, 0, 5) # Controls direction of pieces in step 2.

        self.old_selection = self.selection.value # Saves old selection, used so a new map isn't generated every turn.
        self.old_max = self.max_coord.value # Holds the old maximum size.
        self.old_axial_held = np.array([]) # Hold the old axial coordinates the mouse is at in custom map mode.

        self.clicked_hex = np.array([0, 0])      # Center hex
        self.direction_hex = np.array([])

        self.selected_hex_image = make_hex_surface(
                (128, 128, 128, 255),               # Highlight color for a hexagon.
                self.hex_radius,                    # radius of individual hexagon
                (255, 255, 255),                    # Border color white
                hollow=True)  

        self.hex_map = Selection.get_selection(self.selection.value, self.max_coord.value, self.hex_radius)

        self.b_map = hx.HexMap()
        b_hexes = []
        b_axial_coordinates = []
        for r in range(-self.max_coord.value, self.max_coord.value + 1):
            r_offset = r >> 1
            for q in range(-self.max_coord.value - r_offset, self.max_coord.value - r_offset):
                c = [q, r]
                b_axial_coordinates.append(c)
                b_hexes.append(ExampleHex(c, [141, 207, 250, 255], self.hex_radius, hollow = False))

        self.b_map[np.array(b_axial_coordinates)] = b_hexes

        self.step = 1
        self.player_list = {1: [], 2: []}
        # pygame specific variables
        self.main_surf = None # Pygame surface
        self.font = None      # Pygame font
        self.clock = None     # Pygame fps
        self.init_pg()        # Starts pygame
Exemple #3
0
def load_tilemap(filename):
    new_hex_map = hx.HexMap()
    with open(filename, 'r') as f:
        loaded_json = json.load(f)

    for item in loaded_json['tiles']:
        pos, hex_radius, tile_id = item
        new_tile = hx.HexTile(np.array(pos), hex_radius, tile_id)
        new_hex_map[new_tile.axial_coordinates] = [new_tile]

    return new_hex_map
Exemple #4
0
    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
Exemple #6
0
    def __init__(self, hex_radius=16):
        self.hex_radius = hex_radius

        self.main_surf, self.font, self.clock = initialize_pygame()

        self.camera = ht.Camera(-SCREEN_SIZE / 2)
        self.first_click = np.array([0, 0])
        self.scrolling = False

        self.cursor = ht.Cursor(hex_radius)
        self.hex_map = hx.HexMap()
        self.selector = ht.TileSelector()

        self.selection_tile = ht.SelectionTile(np.array([0, 0]), (20, 20, 20), self.hex_radius)
        self.clicked_hex = np.array([0, 0, 0])
Exemple #7
0
    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()
Exemple #8
0
    def __init__(self, axial_coordinates: np.array(Vector2),
                 hex_radius: float):
        self.hex_radius = hex_radius
        self.hex_map = hx.HexMap()

        hexes = []
        coords = []
        for i, axial in enumerate(axial_coordinates):
            coords.append((axial.x, axial.y))
            hexes.append(
                hx.HexTile((axial.x, axial.y), hex_radius, hash(axial)))

        self.hex_map[np.array(coords)] = hexes

        columns = max(vec.x for vec in axial_coordinates) - min(
            vec.x for vec in axial_coordinates)
        rows = max(vec.y for vec in axial_coordinates) - min(
            vec.y for vec in axial_coordinates)

        AGrid.__init__(self, rows, columns)
Exemple #9
0
import numpy as np
import hexy as hx

hm = hx.HexMap()
radius = 10
coords = hx.get_spiral(np.array((0, 0, 0)), 1, 10)


def test_hexes_amount_to_radius_conversion():
    hexes = 1 + 3 * radius * (radius + 1)
    found_radius = hx.radius_from_hexes(hexes)

    assert found_radius == radius


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)


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 test_cube_to_pixel_conversion():
Exemple #10
0
    def __init__(self, dirname, hex_radius=20, caption="Hex Battle Simulator"):

        self.size = np.array(size)
        self.width, self.height = self.size
        #self.center = (0 + hex_radius, 0 + hex_radius)
        self.center = (self.size / 2).astype(np.int32)
        self.hex_radius = int(hex_radius *
                              scale)  # Radius of individual hexagons
        self.original_radius = self.hex_radius
        self.caption = caption
        self.board = hxgame.GameBoard(dirname)

        self.win_state = 0
        self.hex_map = hx.HexMap()

        temp_map_list = np.array([*self.board.values()])

        hexes = [
            VisualHex(coords.get_axial_coords(), COLORS[0], self.hex_radius)
            for coords in temp_map_list
        ]

        self.hex_map[np.array([c.get_axial_coords()
                               for c in temp_map_list])] = hexes

        self.movement_hex_image = make_hex_surface(
            (128, 128, 128, 255),  # Highlight color for a hexagon.
            self.hex_radius,  # radius of individual hexagon
            (255, 255, 255),  # Border color white
            hollow=True)

        self.attack_hex_image = make_hex_surface(
            (128, 128, 128, 255),  # Highlight color for a hexagon.
            self.hex_radius,  # radius of individual hexagon
            (255, 0, 0),  # Border color white
            hollow=True)

        self.moved_hex_image = make_hex_surface(
            (128, 128, 128, 255),  # Highlight color for a hexagon.
            self.hex_radius,  # radius of individual hexagon
            (128, 128, 128, 255))
        self.move_or_attack = 0

        self.clicked_hex = None
        self.axial_clicked = None
        self.valid_moves = None
        self.axial_moves = None
        self.temp_axial = None
        self.step = 1

        self.movement_counter = CyclicInteger(1, 1, 3)
        self.selected_movement_directions = []

        self.select_direction = CyclicInteger(0, 0, 5)
        self.turn_button = Button((0, 255, 0), int(750 * scale),
                                  int(20 * scale), int(225 * scale),
                                  int(75 * scale), 'End Turn')

        self.main_surf = None
        self.test_surf = None
        self.font = None
        self.fps_font = None
        self.clock = None
        self.init_pg()