示例#1
0
文件: cursor.py 项目: RedFT/HexTiler
 def update(self, camera):
     self.mouse_pos = np.array([camera.follow_camera(pg.mouse.get_pos())])
     self.mouse_cube = hx.pixel_to_cube(self.mouse_pos, self.hex_radius)
     self.mouse_axial = hx.pixel_to_axial(self.mouse_pos, self.hex_radius)
     self.mouse_hex = hx.cube_to_pixel(self.mouse_cube, self.hex_radius)
示例#2
0
文件: cursor.py 项目: RedFT/HexTiler
 def __init__(self, radius):
     self.hex_radius = radius
     self.mouse_pos = np.array([pg.mouse.get_pos()])
     self.mouse_cube = hx.pixel_to_cube(self.mouse_pos, self.hex_radius)
     self.mouse_axial = hx.pixel_to_axial(self.mouse_pos, self.hex_radius)
     self.mouse_hex = hx.cube_to_pixel(self.mouse_cube, self.hex_radius)
示例#3
0
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 handle_events(self):
        running = True # Program is running
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONUP:
                if event.button == 1:# Left mouse
                    self.clicked_hex = hx.pixel_to_axial(
                            np.array([pg.mouse.get_pos() - self.center]), 
                            self.hex_radius)[0].astype(np.int32)

                    if self.step == 2:
                        if np.array_equal(self.hex_map[self.clicked_hex], []):
                            self.clicked_hex = self.old_axial_held
                            continue

                        if not np.array_equal(self.clicked_hex, self.old_axial_held):
                            self.direction_hex = np.array([])

                        index = 0
                        repeat = False
                        for piece in self.player_list[1]:
                            if np.array_equal(piece[1], self.clicked_hex):
                                del self.player_list[1][index]
                                repeat = True
                            else:
                                index += 1

                        index = 0
                        for piece in self.player_list[2]:
                            if np.array_equal(piece[1], self.clicked_hex):
                                del self.player_list[2][index]
                                repeat = True
                            else:
                                index += 1

                        if repeat:
                            continue

                        if self.clicked_hex.size > 0:
                            if self.direction_hex.size <= 0:
                                self.direction_hex = self.clicked_hex
                            else:
                                self.direction_hex = np.array([])

                                if not repeat:
                                    self.player_list[self.player_selection.value].append([self.piece_selection.value, 
                                                                                        self.clicked_hex, 
                                                                                        self.direction_selection.value])
                                self.direction_selection.value = 0

                        self.old_axial_held = self.clicked_hex
                        
                
            if event.type == pg.MOUSEBUTTONDOWN:
                if self.step == 1:
                    if event.button == 4: #Scroll up
                        self.selection.increment()
                    if event.button == 5: #scroll down
                        self.selection.decrement()
                elif self.step == 2:
                    if event.button == 3:
                        self.player_selection.increment()
                    if event.button == 4: #Scroll up
                        self.direction_selection.increment()
                    if event.button == 5: #scroll down
                        self.direction_selection.decrement()

            if event.type == pg.KEYUP:
                if self.step == 1:
                    if event.key == pg.K_RIGHT:
                        self.max_coord.increment()
                    elif event.key == pg.K_LEFT:
                        self.max_coord.decrement()
                    elif event.key == pg.K_a:
                        self.selection.decrement()
                    elif event.key == pg.K_d:
                        self.selection.increment()
                elif self.step == 2:
                    if event.key == pg.K_RIGHT:
                        self.piece_selection.increment()
                    elif event.key == pg.K_LEFT:
                        self.piece_selection.decrement()
                    elif event.key == pg.K_a:
                        self.direction_selection.decrement()
                    elif event.key == pg.K_d:
                        self.direction_selection.increment()

            if event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN:
                    self.step += 1
                if event.key == pg.K_ESCAPE or self.step > 2:
                    running = False

            # Check if we are in the map-making step, and it is a custom map.
            if self.step == 1 and self.selection.value == Selection.Type.CUSTOM:

                # Check that the left mouse is being held down and the mouse is moving.
                if event.type == pg.MOUSEMOTION and event.buttons == (1, 0, 0):
                    mouse_pos = np.array([np.array(pg.mouse.get_pos()) - self.center])
                    axial_clicked = hx.pixel_to_axial(mouse_pos, self.hex_radius).astype(np.int32)[0]

                    # Check if within the map boundaries and the mouse is not on the same tile as before.
                    if not np.array_equal(self.b_map[axial_clicked], []) and not np.array_equal(axial_clicked, self.old_axial_held):

                            # Check if a tile already exists. If not, create one. If one does, delete it.
                            if np.array_equal(self.hex_map[axial_clicked], []):
                                self.hex_map[[axial_clicked]] = [ExampleHex(axial_clicked, [141, 207, 104, 255], self.hex_radius)]
                            else:
                                del self.hex_map[axial_clicked]

                            # Save the axial coordinates so the tile is not added and delete every other frame.
                            self.old_axial_held = axial_clicked
        return running
示例#5
0
 def set_position(self, position):
     self.position = position
     self.axial_coordinates = hx.pixel_to_axial(self.position, self.radius)
     self.cube_coordinates = hx.pixel_to_cube(self.position, self.radius)
示例#6
0
        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)
示例#7
0
    def handle_events(self):
        running = True
        for event in pg.event.get():
            keys = pg.key.get_pressed()
            pos = pg.mouse.get_pos()
            rel_pos = pg.mouse.get_rel()

            if keys[pg.K_c]:
                self.test_center = (self.center[0],
                                    self.center[1] - int(100 * scale))
                self.hex_radius = self.original_radius
                self.regenerate_size_objects()

            if event.type == pg.MOUSEBUTTONUP:  # Attacking or moving

                # Stops clicking on tiles that are hidden up top.
                if pos[1] <= int(100 * scale):
                    continue

                mouse_pos = np.array([
                    np.array(pos) - self.test_center - [0, int(100 * scale)]
                ])
                self.axial_clicked = hx.pixel_to_axial(
                    mouse_pos, self.hex_radius).astype(np.int32)
                axial_player = self.board[self.axial_clicked]
                if event.button == 1:  # Left Click (Move)
                    if self.step == 3:
                        if not np.array_equal(self.axial_clicked,
                                              self.temp_axial):
                            self.step = 2
                            self.temp_axial = self.axial_clicked
                            self.temp_axial = None
                            self.selected_movement_directions = []

                    # Selecting a piece to move.
                    if self.step == 1:
                        if not np.array_equal(axial_player, []):
                            axial_player = self.board[
                                self.axial_clicked][0].piece.player
                        else:
                            pass

                        found = False
                        for moved in self.board.moved_pieces:
                            if np.array_equal(moved, self.axial_clicked[0]):
                                found = True
                                break
                        if found:
                            continue
                        if (np.array_equal(
                                self.valid_moves,
                                None)) and axial_player == self.board.player:
                            self.clicked_hex = self.axial_clicked
                            self.axial_moves = self.board.get_valid_moves(
                                self.board[self.clicked_hex][0])
                            self.move_or_attack = 1
                            self.step = 2

                    # Moving the selected piece to a valid coordinate.
                    elif self.step == 2:
                        if (not np.array_equal(self.board[self.axial_clicked],
                                               [])
                                and self.board[self.axial_clicked][0]
                                in self.board[self.valid_moves]):
                            if np.array_equal(
                                    self.selected_movement_directions, []):
                                dirs = []
                                for axial in self.axial_moves:
                                    if np.array_equal(self.axial_clicked[0],
                                                      axial[0:2]):
                                        dirs.append(
                                            np.where(
                                                DIRECTIONS == axial[3])[0][0])
                                self.select_direction = CyclicInteger(
                                    0, 0,
                                    len(dirs) - 1)
                                self.selected_movement_directions = np.sort(
                                    dirs)
                                self.temp_axial = self.axial_clicked
                                self.step = 3
                                continue
                        else:
                            self.clicked_hex = None
                            self.axial_moves = None
                            self.valid_moves = None
                            self.axial_clicked = None
                            self.temp_axial = None
                            self.selected_movement_directions = []
                            self.step = 1

                    # Choosing a valid direction for the piece at the new coordinates.
                    elif self.step == 3:
                        if (not np.array_equal(self.board[self.axial_clicked],
                                               [])
                                and self.board[self.axial_clicked][0]
                                in self.board[self.valid_moves]):
                            new_dir = DIRECTIONS[
                                self.selected_movement_directions[
                                    self.select_direction.value]]
                            self.board.move_piece(self.clicked_hex[0],
                                                  self.temp_axial[0], new_dir)
                        self.clicked_hex = None
                        self.axial_moves = None
                        self.valid_moves = None
                        self.axial_clicked = None
                        self.temp_axial = None
                        self.selected_movement_directions = []
                        self.step = 1

                if event.button == 3:  # Right click (Attack)
                    if not np.array_equal(axial_player, []):
                        axial_player = self.board[
                            self.axial_clicked][0].piece.player
                    else:
                        pass

                    found = False
                    for fired in self.board.fired_pieces:
                        if np.array_equal(fired, self.axial_clicked[0]):
                            found = True
                            break
                    if found:
                        continue

                    if (np.array_equal(
                            self.valid_moves,
                            None)) and axial_player == self.board.player:
                        self.clicked_hex = self.axial_clicked
                        self.axial_moves = self.board.get_valid_attacks(
                            self.board[self.clicked_hex][0])
                        self.move_or_attack = 2

                    if not np.array_equal(self.clicked_hex,
                                          None) and not np.array_equal(
                                              self.valid_moves, None):
                        if (not np.array_equal(
                                self.board[self.axial_clicked], []
                                and self.board[self.axial_clicked][0]
                                in self.board[self.valid_moves])):
                            self.board.attack_piece(self.clicked_hex[0],
                                                    self.axial_clicked[0])
                        self.clicked_hex = None
                        self.axial_moves = None
                        self.valid_moves = None
                        self.axial_clicked = None

            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 4:
                    if keys[pg.K_LCTRL]:  #Scroll up
                        self.hex_radius += 1
                        self.regenerate_size_objects()
                    else:
                        self.select_direction.increment()
                if event.button == 5:
                    if keys[pg.K_LCTRL]:  #scroll down
                        self.hex_radius -= 1
                        if self.hex_radius < 1:
                            self.hex_radius = 1
                        self.regenerate_size_objects()
                    else:
                        self.select_direction.decrement()

                if self.turn_button.isOver(pos):
                    self.win_state = self.board.end_turn()
                    self.clicked_hex = None
                    self.axial_moves = None
                    self.valid_moves = None
                    self.axial_clicked = None
                    self.temp_axial = None
                    self.selected_movement_directions = []
                    self.step = 1

            # Alternate direction rotation if the scroll wheel is not working
            if event.type == pg.KEYUP:
                if event.key == pg.K_a:
                    self.select_direction.decrement()
                elif event.key == pg.K_d:
                    self.select_direction.increment()
                elif event.key == pg.K_w:
                    self.hex_radius += 2
                    self.regenerate_size_objects()
                elif event.key == pg.K_s:
                    self.hex_radius -= 2
                    if self.hex_radius < 1:
                        self.hex_radius = 1
                    self.regenerate_size_objects()

            if event.type == pg.MOUSEMOTION:
                if event.buttons == (1, 0, 0) and keys[pg.K_LCTRL]:
                    self.test_center = (self.test_center[0] + rel_pos[0],
                                        self.test_center[1] + rel_pos[1])
                if self.turn_button.isOver(pos):
                    self.turn_button.color = (0, 194, 0)
                else:
                    self.turn_button.color = (0, 255, 0)

            if event.type == pg.QUIT or (event.type == pg.KEYDOWN
                                         and event.key == pg.K_ESCAPE):
                running = False

            if self.win_state != 0:
                print("Player", self.win_state, "has won!")
                running = False

        return running