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
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
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()
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
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)
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
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
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)
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