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