def __init__(self, pos, actors): self.model = PeepoModel(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(PeepoModel.RADIUS, self.rotation + 30, self.rect.center) self.edge_left = end_line(PeepoModel.RADIUS, self.rotation - 30, self.rect.center) self.peepo = Peepo(self, GOAL_POS)
def update(self, screen_rect): self.model.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.model.motor_output[pg.K_LEFT]: self.rotation -= random.randint(10, 30) if self.model.motor_output[pg.K_RIGHT]: self.rotation += random.randint(10, 30) self.rotation = self.rotation % 360 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(PeepoModel.RADIUS, self.rotation + 30, self.rect.center) self.edge_left = end_line(PeepoModel.RADIUS, self.rotation - 30, self.rect.center) self.rect.clamp_ip(screen_rect) self.peepo.update(self.model)
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, PeepoModel.RADIUS) if collided: if 'wall' in actor.id: edge = end_line(PeepoModel.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(PeepoModel.RADIUS, self.peepo_actor.rotation - 30, self.peepo_actor.rect.center) edge2 = end_line(PeepoModel.RADIUS, self.peepo_actor.rotation - 20, self.peepo_actor.rect.center) edge3 = end_line(PeepoModel.RADIUS, self.peepo_actor.rotation - 10, self.peepo_actor.rect.center) edge4 = end_line(PeepoModel.RADIUS, self.peepo_actor.rotation, self.peepo_actor.rect.center) edge5 = end_line(PeepoModel.RADIUS, self.peepo_actor.rotation + 10, self.peepo_actor.rect.center) edge6 = end_line(PeepoModel.RADIUS, self.peepo_actor.rotation + 20, self.peepo_actor.rect.center) edge7 = end_line(PeepoModel.RADIUS, self.peepo_actor.rotation + 30, self.peepo_actor.rect.center) self.obstacle_input['1'] = collision( actor.rect, peepo_vec, edge1, edge2, PeepoModel.RADIUS) self.obstacle_input['2'] = collision( actor.rect, peepo_vec, edge2, edge3, PeepoModel.RADIUS) self.obstacle_input['3'] = collision( actor.rect, peepo_vec, edge3, edge4, PeepoModel.RADIUS) self.obstacle_input['4'] = collision( actor.rect, peepo_vec, edge4, edge5, PeepoModel.RADIUS) self.obstacle_input['5'] = collision( actor.rect, peepo_vec, edge5, edge6, PeepoModel.RADIUS) self.obstacle_input['6'] = collision( actor.rect, peepo_vec, edge6, edge7, PeepoModel.RADIUS)