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