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