示例#1
0
    def next_target(self, sprite=None):
        self.prev_x, self.prev_y = self.x, self.y

        scene = self.get_scene()
        if not scene:
            return

        game_rects, game_poly = scene.board.game_rects, scene.board.game_area

        game_lines = [(dot1, dot2) for dot1, dot2 in zip(game_poly, game_poly[1:])]

        delta_angle = 0
        angle_range = 180

        in_area, stuck = False, 0
        while not in_area and stuck < 10:
            stuck += 1
            delta_angle = self.current_angle - math.radians(angle_range / 2) + random.random() * math.radians(angle_range)

            distance = random.randint(self.min_distance, self.max_distance)

            x, y = self.x + distance * math.cos(delta_angle), self.y + distance * math.sin(delta_angle)
            x, y = int(x), int(y)

            dots = [(x, y)]
            in_area = all((game_utils.in_area(dot, game_rects) for dot in dots))

            if in_area:
                # check for overlaps
                line_pairs = ((((x, y), (self.x, self.y)), line) for line in game_lines)
                for pair in line_pairs:
                    if game_utils.intersection(*pair):
                        in_area = False
                        break


            if not in_area:
                angle_range += 60


        self.current_angle = delta_angle % math.radians(360)

        self.steps = random.randint(self.min_steps, self.max_steps)
        self.current_step = 0

        if stuck < 10:
            self.next_x, self.next_y = x, y
            self.next_distance = distance
示例#2
0
    def touching_poly(self, poly):
        poly_lines = [(dot1, dot2) for dot1, dot2 in zip(poly, poly[1:])]
        x1, y1, x2, y2 = int(self.x) - 10, int(self.y) - 10, \
                         int(self.x) + 10, int(self.y) + 10
        qix_box = game_utils._bounding_box((x1, y1), (x2, y2))

        # first do a cheap run on the bounding box
        if len(poly_lines) > 1:
            (xb1, yb1), (xb2, yb2) = game_utils.box_range(poly)
            if not any((xb1 <= x <= xb2 and yb1 <= y <= yb2 for (x, y) in qix_box)):
                return False

        for line1 in zip(qix_box, qix_box[1:]):
            for line2 in poly_lines:
                if game_utils.intersection(line1, line2):
                    return True