コード例 #1
0
 def update(self, model):
     Pulsator.update(self, model)
     nearbyPrey = model.find(lambda x: isinstance(x, Prey) and self.
                             distance((x.get_location())) <= 200)
     toSort = []
     for prey in nearbyPrey:
         toSort.append((prey, self.distance((prey.get_location()))))
     if toSort != []:
         prey_to_chase = min(toSort, key=lambda x: x[1])
         xp, yp = Prey.get_location(prey_to_chase[0])
         xh, yh = self.get_location()
         diff_y = yp - yh
         diff_x = xp - xh
         self.set_angle(atan2(diff_y, diff_x))
     Mobile_Simulton.move(self)
コード例 #2
0
    def update(self, model):
        Pulsator.update(self, model)
        Mobile_Simulton.move(self)

        prey_set = model.find(lambda x: isinstance(x, Prey))
        can_see = set()
        for prey in prey_set:
            distance = ((self._x - prey._x)**2 + (self._y - prey._y)**2)**0.5
            if distance < self.vision:
                can_see.add(prey)

        nearby = []
        for prey in can_see:
            distance = ((self._x - prey._x)**2 + (self._y - prey._y)**2)**0.5
            nearby.append((prey, distance))
        if len(nearby) > 0:
            closest = sorted(nearby, key=lambda x: x[1])[0]
            self.set_angle(
                atan2((closest[0]._y - self._y), (closest[0]._x - self._x)))