コード例 #1
0
    def cohesion(self):

        if self.prey is not None:

            if self.creature.position[0] > self.prey.position[
                    0] and self.creature.position[1] > self.prey.position[1]:
                self.creature.direction = Calculations.rotate_vector(
                    self.creature.direction, -self.creature.turn_speed,
                    -self.creature.turn_speed)

            elif self.creature.position[0] > self.prey.position[
                    0] and self.creature.position[1] < self.prey.position[1]:
                self.creature.direction = Calculations.rotate_vector(
                    self.creature.direction, -self.creature.turn_speed,
                    self.creature.turn_speed)

            elif self.creature.position[0] < self.prey.position[
                    0] and self.creature.position[1] > self.prey.position[1]:
                self.creature.direction = Calculations.rotate_vector(
                    self.creature.direction, self.creature.turn_speed,
                    -self.creature.turn_speed)

            elif self.creature.position[0] < self.prey.position[
                    0] and self.creature.position[1] < self.prey.position[1]:
                self.creature.direction = Calculations.rotate_vector(
                    self.creature.direction, -self.creature.turn_speed,
                    -self.creature.turn_speed)
コード例 #2
0
    def get_closest_pillar(self):

        self.closest_pillar = None
        for each in self.world.object_container:
            if each.type is "Obstacle":
                if self.closest_pillar is None:
                    self.closest_pillar = each
                elif Calculations.get_distance(self.creature, self.closest_pillar) > Calculations.get_distance(self.creature, each):
                    self.closest_pillar = each
コード例 #3
0
    def get_prey(self):

        self.prey = None
        for each in self.objects_in_range:
            if each.type is "Creature":
                if each.species is not self.species:
                    if self.prey is None:
                        self.prey = each
                    elif Calculations.get_distance(
                            self.creature, each) < Calculations.get_distance(
                                self.creature, self.prey):
                        self.prey = each
コード例 #4
0
    def avoid_object(self, object, turning_speed):

        if self.creature.position[0] > object.position[0] and self.creature.position[1] > object.position[1]:
            self.creature.direction = Calculations.rotate_vector(self.creature.direction, turning_speed, turning_speed)

        elif self.creature.position[0] > object.position[0] and self.creature.position[1] < object.position[1]:
            self.creature.direction = Calculations.rotate_vector(self.creature.direction, turning_speed, -turning_speed)

        elif self.creature.position[0] < object.position[0] and self.creature.position[1] > object.position[1]:
            self.creature.direction = Calculations.rotate_vector(self.creature.direction, -turning_speed, turning_speed)

        elif self.creature.position[0] < object.position[0] and self.creature.position[1] < object.position[1]:
            self.creature.direction = Calculations.rotate_vector(self.creature.direction, -turning_speed, -turning_speed)
コード例 #5
0
    def boid_flocking(self):

        self.avoid_collision = False
        for each in self.objects_in_range:
            if each.type is "Obstacle":
                if Calculations.get_distance(self.creature, each) < self.creature.vision:
                    self.avoid_collision = True
            elif each.type is "Creature":
                if Calculations.get_distance(self.creature, each) < self.creature.distance:
                    self.avoid_collision = True

        if self.avoid_collision:
            self.separation()
        else:
            self.cohesion()
コード例 #6
0
    def move(self):
        print(Calculations.get_distance(self.creature, self.closest_pillar))
        if Calculations.get_distance(self.creature, self.closest_pillar) > self.creature.distance + 20:

            new_position = [self.creature.position[0] + self.creature.movement_speed * self.creature.direction[0],
                            self.creature.position[1] + self.creature.movement_speed * self.creature.direction[1]]
            if new_position[0] > self.world.width:
                new_position[0] = 0
            elif new_position[0] < 0:
                new_position[0] = self.world.width
            if new_position[1] > self.world.height:
                new_position[1] = 0
            elif new_position[1] < 0:
                new_position[1] = self.world.height
            self.creature.position = new_position
コード例 #7
0
 def get_close_objects(self):
     self.objects_in_range = []
     for each in self.world.object_container:
         if each is not self.creature:
             if Calculations.get_distance(self.creature,
                                          each) <= self.creature.vision:
                 self.objects_in_range.append(each)
コード例 #8
0
    def separation(self):

        #if self.target is not None:
        for each in self.world.object_container:
            if each.type is "Creature" or each.type is "Obstacle":
                if Calculations.get_distance(self.creature,
                                             each) < self.creature.distance:

                    if self.creature.position[0] > each.position[
                            0] and self.creature.position[0] - each.position[
                                0] < self.creature.distance:
                        self.creature.position = self.creature.position[
                            0] + self.creature.turn_speed * 10, self.creature.position[
                                1]
                    elif self.creature.position[0] < each.position[
                            0] and each.position[0] - self.creature.position[
                                0] < self.creature.distance:
                        self.creature.position = self.creature.position[
                            0] - self.creature.turn_speed * 10, self.creature.position[
                                1]

                    if self.creature.position[1] > each.position[
                            1] and self.creature.position[1] - each.position[
                                1] < self.creature.distance:
                        self.creature.position = self.creature.position[
                            0], self.creature.position[
                                1] + self.creature.turn_speed * 10
                    elif self.creature.position[1] < each.position[
                            1] and each.position[1] - self.creature.position[
                                1] < self.creature.distance:
                        self.creature.position = self.creature.position[
                            0], self.creature.position[
                                1] - self.creature.turn_speed * 10
コード例 #9
0
    def move(self):
        if not self.target_reached and self.target is not None:

            new_direction = (self.target.position[0] -
                             self.creature.position[0],
                             self.target.position[1] -
                             self.creature.position[1])

            self.creature.direction = Calculations.get_vector(new_direction)

            new_position = [
                self.creature.position[0] +
                self.creature.movement_speed * self.creature.direction[0],
                self.creature.position[1] +
                self.creature.movement_speed * self.creature.direction[1]
            ]

            if new_position[0] > self.world.width:
                new_position[0] = 0
            elif new_position[0] < 0:
                new_position[0] = self.world.width
            if new_position[1] > self.world.height:
                new_position[1] = 0
            elif new_position[1] < 0:
                new_position[1] = self.world.height
            self.creature.position = new_position
コード例 #10
0
    def reach_target(self):

        if self.target is not None and not self.target_reached:
            if Calculations.get_distance(self.creature, self.target) < 5:
                self.target = None
                self.target_reached = True
                self.creature.species = Species.Goldfinch

            for each in self.world.object_container:

                if each.type is "Creature":
                    if Calculations.get_distance(
                            self.creature,
                            each) < 10 and each.behaviour.target_reached:
                        self.target = None
                        self.target_reached = True
                        self.creature.species = Species.Goldfinch
コード例 #11
0
    def cohesion(self):

        total_positions = [
            self.creature.position[0], self.creature.position[1]
        ]
        number_of_boids = 1

        for each in self.objects_in_range:
            if each.type is "Creature":
                if each.species is self.species:
                    number_of_boids += 1
                    total_positions[0] += each.position[0]
                    total_positions[1] += each.position[1]

        if number_of_boids == 1:
            return None

        average_position = total_positions[
            0] / number_of_boids, total_positions[1] / number_of_boids

        if self.creature.position[0] > average_position[
                0] and self.creature.position[1] > average_position[1]:
            self.creature.direction = Calculations.rotate_vector(
                self.creature.direction, -self.creature.turn_speed,
                -self.creature.turn_speed)

        elif self.creature.position[0] > average_position[
                0] and self.creature.position[1] < average_position[1]:
            self.creature.direction = Calculations.rotate_vector(
                self.creature.direction, -self.creature.turn_speed,
                self.creature.turn_speed)

        elif self.creature.position[0] < average_position[
                0] and self.creature.position[1] > average_position[1]:
            self.creature.direction = Calculations.rotate_vector(
                self.creature.direction, self.creature.turn_speed,
                -self.creature.turn_speed)

        elif self.creature.position[0] < average_position[
                0] and self.creature.position[1] < average_position[1]:
            self.creature.direction = Calculations.rotate_vector(
                self.creature.direction, -self.creature.turn_speed,
                -self.creature.turn_speed)
コード例 #12
0
    def separation(self):

        distance = 0

        for each in self.world.object_container:

            if each.type is "Obstacle":
                distance = self.creature.vision
            elif each.type is "Creature":
                if each.species is self.species:
                    distance = self.creature.distance
                else:
                    distance = self.creature.vision

            if Calculations.get_distance(self.creature,
                                         each) <= self.creature.radius * 2:
                self.avoid_object(each, 1)
            elif Calculations.get_distance(self.creature, each) < distance / 5:
                self.avoid_object(each, self.creature.turn_speed)
            elif Calculations.get_distance(self.creature, each) < distance / 4:
                self.avoid_object(each, self.creature.turn_speed / 2)
            elif Calculations.get_distance(self.creature, each) < distance / 3:
                self.avoid_object(each, self.creature.turn_speed / 3)
            elif Calculations.get_distance(self.creature, each) < distance / 2:
                self.avoid_object(each, self.creature.turn_speed / 4)
            elif Calculations.get_distance(self.creature,
                                           each) < self.creature.vision:
                self.avoid_object(each, self.creature.turn_speed / 5)
コード例 #13
0
    def alignment(self):

        total_direction = [
            self.creature.direction[0], self.creature.direction[1]
        ]
        number_of_boids = 1

        for each in self.objects_in_range:
            if each.type is "Creature":
                if each.species is self.species:
                    total_direction[0] += each.direction[0]
                    total_direction[1] += each.direction[1]
                    number_of_boids += 1

        average_direction = total_direction[
            0] / number_of_boids, total_direction[1] / number_of_boids
        average_direction = Calculations.get_vector(average_direction)
        if self.creature.direction != average_direction:
            self.change_direction(average_direction)
コード例 #14
0
    def __init__(self, world, species):

        self.world = world
        self.type = "Creature"

        self.species = species

        self.radius = CREATURE['radius']
        self.vision = CREATURE['vision']
        self.movement_speed = float(CREATURE['movement_speed']) / 10
        self.turn_speed = float(CREATURE['turn_speed']) / 100
        self.distance = CREATURE['distance']

        self.position = (random.uniform(0, self.world.width),
                         random.uniform(0, self.world.height))
        self.direction = (random.uniform(-1, 1), random.uniform(-1, 1)
                          )  # initialize random direction
        self.direction = Calculations.get_vector(self.direction)

        self.behaviour = Wandering(self)
コード例 #15
0
 def eat_prey(self):
     if self.prey is not None:
         if Calculations.get_distance(self.creature,
                                      self.prey) <= self.reach:
             self.world.object_container.remove(self.prey)
             self.prey = None