예제 #1
0
    def boundaries(self):
        desired = None

        #if x value is on frame of settings['pygame_settings']['boundary'] or visable window
        if self.position[0] < settings['pygame_settings']['boundary']:
            desired = numpy.array([self.max_vel, self.velocity[1]])
            steer = desired - self.velocity
            steer = normalise(steer) * self.max_force
            self.apply_force(steer)

        #if x value is larger than the window or outside
        elif self.position[0] > settings['pygame_settings'][
                'window_width'] - settings['pygame_settings']['boundary']:
            desired = numpy.array([-self.max_vel, self.velocity[1]])
            steer = desired - self.velocity
            steer = normalise(steer) * self.max_force
            self.apply_force(steer)
        #y value on boarder frame
        if self.position[1] < settings['pygame_settings']['boundary']:
            desired = numpy.array([self.velocity[0], self.max_vel])
            steer = desired - self.velocity
            steer = normalise(steer) * self.max_force
            self.apply_force(steer)
        # y value outside of window range
        elif self.position[1] > settings['pygame_settings'][
                'window_height'] - settings['pygame_settings']['boundary']:
            desired = numpy.array([self.velocity[0], -self.max_vel])
            steer = desired - self.velocity
            steer = normalise(steer) * self.max_force
            self.apply_force(steer)
예제 #2
0
    def find(self, target):
        desired_vel = numpy.add(target.position, -self.position)
        desired_vel = normalise(desired_vel) * self.max_vel

        steering_force = numpy.add(desired_vel, -self.velocity)
        steering_force = normalise(steering_force) * self.max_force

        return (steering_force)
예제 #3
0
    def fight(self, itemList):
        closest = None

        closest_distance = max(settings['pygame_settings']['window_width'],
                               settings['pygame_settings']['window_height'])

        posx = self.position[0]
        posy = self.position[1]

        foodIndex = len(itemList) - 1

        for i in itemList[::-1]:
            item_x = i.position[0]
            item_y = i.position[1]

            distance = math.hypot(posx - item_x, posy - item_y)

            if distance < 4:
                i.health = i.health - self.power
                if (i.health < 0):
                    self.health += 40

            if distance < closest_distance:
                closest_distance = distance
                closest = i
            foodIndex -= 1

        prey = self.find(closest)
        seek = normalise(prey) * self.max_force
        self.apply_force(seek)
예제 #4
0
    def eat(self,itemList, index):
        if index == 0:
            hp = 40
        else:
            hp = -30

        closest = None

        closest_distance = settings['pygame_settings']['window_width']

        posx = self.position[0]
        posy = self.position[1]

        foodIndex = len(itemList)-1

        for i in itemList[::-1]:
            item_x = i[0]
            item_y = i[1]

            distance = math.hypot(posx-item_x, posy-item_y)

            if distance < 5:
                itemList.pop(foodIndex)
                self.health += hp

            if distance < closest_distance:
                closest_distance = distance
                closest = i
            foodIndex -= 1

        if closest_distance < self.dna[2]:
            prey = self.find(closest)
            prey *= self.dna[index]
            seek = normalise(prey)*self.max_force
            self.apply_force(seek)
예제 #5
0
    def update(self):
        self.velocity += self.acceleration
        self.velocity = normalise(self.velocity) * self.max_vel

        self.position += self.velocity
        self.acceleration *= 0
        self.health -= 1
        self.health = min(settings['predator']['health'], self.health)
예제 #6
0
    def update(self):
        self.velocity += self.acceleration
        self.velocity = normalise(self.velocity)*self.max_vel

        self.position += self.velocity 
        self.acceleration *= 0 #resets acceleration to 0
        self.health -= 0.2
        self.health = min(settings['organism']['health'], self.health)

        self.fitness += 1