Exemplo n.º 1
0
    def test_get_direction_towards(self):
        direction = Helper.get_direction_towards((0,0), (0,10))
        self.assertEqual(90, direction)

        direction = Helper.get_direction_towards((0,0), (1,1))
        self.assertEqual(45, direction)

        direction = Helper.get_direction_towards((0,0), (-1,-1))
        eps = math.sin(math.radians(225)) - math.sin(math.radians(direction))
        self.assertTrue(eps < 0.001)
Exemplo n.º 2
0
    def move(self):
        '''Predator is pretty dumb: if it sees a pray, it follows it to
        autodestruction.
        '''
        # Can always do indexing on filtered instances, as it should be a pray
        # always, else the game should have finished (bug?).
        pray = filter(lambda x: isinstance(x, Pray), self.game.instances)[0]
        # In case it sees the pray, change direction to follow it.
        if BaseObject.object_sees_object(self, pray):
            self.direction = Helper.get_direction_towards(self.coord, pray.coord)

            # Try and find close predators and sync with them to arrive at the
            # prey in the same time.
            #
            # The sync works the following way:
            # - each predator finds the max speed and max distance to pray
            #   (from all predators visible from him that they follow the pray)
            # - they update their speed v' = v_max * d / d_max
            #   where d = own distance to pray
            max_speed = self.speed
            own_dist = max_dist = Helper.euclidian_distance(self.coord, pray.coord)
            for instance in self.game.instances:
                if instance == self or not isinstance(instance, Predator):
                    continue
                # Look only for visible predators other than myself.
                if BaseObject.object_sees_object(self, instance):
                    dist = Helper.euclidian_distance(instance.coord, pray.coord)
                    max_dist = max(max_dist, dist)
                    max_speed = max(max_speed, instance.speed)
            # Sync speed with other predators.
            self.speed = max_speed * own_dist / float(max_dist)
        super(Predator, self).move()