Beispiel #1
0
    def update(self):
        self.generative_model.process()

        self.rect.x += SurvivalPeepo.SPEED * math.cos(math.radians(self.rotation))
        self.rect.y += SurvivalPeepo.SPEED * math.sin(math.radians(self.rotation))

        if self.motor[LEFT]:
            self.rotation -= 10
            if self.rotation < 0:
                self.rotation = 360
        if self.motor[RIGHT]:
            self.rotation += 10
            if self.rotation > 360:
                self.rotation = 0
        self.edge_middle = end_line(SurvivalPeepo.RADIUS, self.rotation, self.rect.center)
        self.calculate_obstacles()

        if self.graphical:
            self.image = pg.transform.rotate(self.image_original, -self.rotation)
            self.rect = self.image.get_rect(center=self.rect.center)

        self.edge_right = end_line(SurvivalPeepo.RADIUS, self.rotation + 30, self.rect.center)
        self.edge_left = end_line(SurvivalPeepo.RADIUS, self.rotation - 30, self.rect.center)

        if self.rect.x < 0 or self.rect.y < 0 or self.rect.x > 800 or self.rect.y > 800:
            self.rect.x, self.rect.y = 400, 400
Beispiel #2
0
    def __init__(self, name, network, graphical, pos=(0, 0), obstacles=None):
        super().__init__(network)

        self.graphical = graphical
        self.name = name
        self.rect = pg.Rect(pos, SurvivalPeepo.SIZE)
        self.rect.center = pos
        self.rotation = 0
        self.edge_right = end_line(SurvivalPeepo.RADIUS, self.rotation + 30, self.rect.center)
        self.edge_left = end_line(SurvivalPeepo.RADIUS, self.rotation - 30, self.rect.center)
        self.edge_middle = end_line(SurvivalPeepo.RADIUS, self.rotation, self.rect.center)

        if self.graphical:
            self.image = self.make_image()
            self.image_original = self.image.copy()

        self.health = 0

        self.obstacles = obstacles or []
        self.motor = {
            LEFT: False,
            RIGHT: False
        }
        self.view = {
            '1': False,
            '2': False,
            '3': False,
            '4': False,
            '5': False,
            '6': False,
        }
        self.generative_model = GenerativeModel(self, n_jobs=1)
Beispiel #3
0
    def update(self, screen_rect):
        if self.run:
            self.peepo.process()

            self.rect.x += PeepoActor.SPEED * math.cos(
                math.radians(self.rotation))
            self.rect.y += PeepoActor.SPEED * math.sin(
                math.radians(self.rotation))

            if self.peepo.motor_output[pg.K_LEFT]:
                self.rotation -= 10
                if self.rotation < 0:
                    self.rotation = 360
            if self.peepo.motor_output[pg.K_RIGHT]:
                self.rotation += 10
                if self.rotation > 360:
                    self.rotation = 0

            self.image = pg.transform.rotate(self.image_original,
                                             -self.rotation)
            self.rect = self.image.get_rect(center=self.rect.center)

            self.edge_right = end_line(WanderingPeepo.RADIUS,
                                       self.rotation + 30, self.rect.center)
            self.edge_left = end_line(WanderingPeepo.RADIUS,
                                      self.rotation - 30, self.rect.center)

            self.rect.clamp_ip(screen_rect)
            self.peepo.update()
Beispiel #4
0
 def __init__(self, pos, actors):
     self.peepo = WanderingPeepo(self, actors)
     self.rect = pg.Rect((0, 0), PeepoActor.SIZE)
     self.rect.center = pos
     self.image = self.make_image()
     self.image_original = self.image.copy()
     self.rotation = 0
     self.edge_right = end_line(WanderingPeepo.RADIUS, self.rotation + 30,
                                self.rect.center)
     self.edge_left = end_line(WanderingPeepo.RADIUS, self.rotation - 30,
                               self.rect.center)
     self.run = True
Beispiel #5
0
    def calculate_obstacles(self):
        self.view = {x: False for x in self.view}

        peepo_vec = pg.math.Vector2(self.rect.center)
        min_distance = 1000
        closest_obstacle = None

        for obstacle in self.obstacles:
            if self.rect.colliderect(obstacle.rect):
                self.health += 1
                self.obstacles.remove(obstacle)

            if collision(obstacle.rect, peepo_vec, self.edge_left, self.edge_right, SurvivalPeepo.RADIUS):
                distance = math.hypot(obstacle.rect.x - self.rect.x, obstacle.rect.y - self.rect.y)
                if distance <= min_distance:
                    closest_obstacle = obstacle
                    min_distance = distance

        if closest_obstacle:
            edge1 = end_line(SurvivalPeepo.RADIUS, self.rotation - 30, self.rect.center)
            edge2 = end_line(SurvivalPeepo.RADIUS, self.rotation - 20, self.rect.center)
            edge3 = end_line(SurvivalPeepo.RADIUS, self.rotation - 10, self.rect.center)
            edge4 = end_line(SurvivalPeepo.RADIUS, self.rotation, self.rect.center)
            edge5 = end_line(SurvivalPeepo.RADIUS, self.rotation + 10, self.rect.center)
            edge6 = end_line(SurvivalPeepo.RADIUS, self.rotation + 20, self.rect.center)
            edge7 = end_line(SurvivalPeepo.RADIUS, self.rotation + 30, self.rect.center)

            self.view['1'] = collision(closest_obstacle.rect, peepo_vec, edge1, edge2, SurvivalPeepo.RADIUS)
            self.view['2'] = collision(closest_obstacle.rect, peepo_vec, edge2, edge3, SurvivalPeepo.RADIUS)
            self.view['3'] = collision(closest_obstacle.rect, peepo_vec, edge3, edge4, SurvivalPeepo.RADIUS)
            self.view['4'] = collision(closest_obstacle.rect, peepo_vec, edge4, edge5, SurvivalPeepo.RADIUS)
            self.view['5'] = collision(closest_obstacle.rect, peepo_vec, edge5, edge6, SurvivalPeepo.RADIUS)
            self.view['6'] = collision(closest_obstacle.rect, peepo_vec, edge6, edge7, SurvivalPeepo.RADIUS)
    def calculate_obstacles(self):
        for key in self.obstacle_input:
            self.obstacle_input[key] = False

        for actor in self.actors:
            peepo_vec = vec(self.peepo_actor.rect.center)
            collided = collision(actor.rect, peepo_vec, self.peepo_actor.edge_left,
                                 self.peepo_actor.edge_right, WanderingPeepo.RADIUS)
            if collided:
                if 'wall' in actor.id:
                    edge = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation, self.peepo_actor.rect.center)
                    if 'left' in actor.id:
                        wall_vec = vec((5, self.peepo_actor.rect.y))
                        deg = math.degrees(
                            math.atan2(wall_vec.y - edge.y, wall_vec.x - edge.x)) + self.peepo_actor.rotation
                        if deg < 0:
                            self.obstacle_input['6'] = True
                        else:
                            self.obstacle_input['1'] = True
                    elif 'right' in actor.id:
                        wall_vec = vec((1598, self.peepo_actor.rect.y))
                        deg = math.degrees(
                            math.atan2(wall_vec.y - edge.y, wall_vec.x - edge.x)) + self.peepo_actor.rotation
                        if deg < 0:
                            self.obstacle_input['1'] = True
                        else:
                            self.obstacle_input['6'] = True
                    elif 'up' in actor.id:
                        wall_vec = vec((5, self.peepo_actor.rect.y))
                        deg = math.degrees(
                            math.atan2(wall_vec.y - edge.y, wall_vec.x - edge.x)) + self.peepo_actor.rotation
                        if deg < 90:
                            self.obstacle_input['6'] = True
                        else:
                            self.obstacle_input['1'] = True
                    else:
                        wall_vec = vec((5, self.peepo_actor.rect.y))
                        deg = math.degrees(
                            math.atan2(wall_vec.y - edge.y, wall_vec.x - edge.x)) + self.peepo_actor.rotation
                        if deg < -90:
                            self.obstacle_input['6'] = True
                        else:
                            self.obstacle_input['1'] = True

                else:
                    edge1 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation - 30,
                                     self.peepo_actor.rect.center)
                    edge2 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation - 20,
                                     self.peepo_actor.rect.center)
                    edge3 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation - 10,
                                     self.peepo_actor.rect.center)
                    edge4 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation, self.peepo_actor.rect.center)
                    edge5 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation + 10,
                                     self.peepo_actor.rect.center)
                    edge6 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation + 20,
                                     self.peepo_actor.rect.center)
                    edge7 = end_line(WanderingPeepo.RADIUS, self.peepo_actor.rotation + 30,
                                     self.peepo_actor.rect.center)

                    self.obstacle_input['1'] = collision(actor.rect, peepo_vec, edge1, edge2, WanderingPeepo.RADIUS)
                    self.obstacle_input['2'] = collision(actor.rect, peepo_vec, edge2, edge3, WanderingPeepo.RADIUS)
                    self.obstacle_input['3'] = collision(actor.rect, peepo_vec, edge3, edge4, WanderingPeepo.RADIUS)
                    self.obstacle_input['4'] = collision(actor.rect, peepo_vec, edge4, edge5, WanderingPeepo.RADIUS)
                    self.obstacle_input['5'] = collision(actor.rect, peepo_vec, edge5, edge6, WanderingPeepo.RADIUS)
                    self.obstacle_input['6'] = collision(actor.rect, peepo_vec, edge6, edge7, WanderingPeepo.RADIUS)