def update(self): self.generative_model.process() self.rect.x += GoLPeepo.SPEED * math.cos(math.radians(self.rotation)) self.rect.y += GoLPeepo.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.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(GoLPeepo.RADIUS, self.rotation + 30, self.rect.center) self.edge_left = end_line(GoLPeepo.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
def __init__(self, name, network, graphical, pos=(0, 0), ennemies=None, food=None): self.graphical = graphical self.name = name self.network = network self.rect = pg.Rect(pos, Peepo.SIZE) self.rect.center = pos self.rotation = 0 self.edge_right = end_line(Peepo.RADIUS, self.rotation + 30, self.rect.center) self.edge_left = end_line(Peepo.RADIUS, self.rotation - 30, self.rect.center) self.edge_middle = end_line(Peepo.RADIUS, self.rotation, self.rect.center) if self.graphical: self.image = self.make_image() self.image_original = self.image.copy() self.stomach = 0 self.bang = 0 self.ennemies = ennemies or [] self.food = food or [] self.obstacles = [] self.assemble_obstacles() self.motor = {LEFT: False, RIGHT: False} self.view = { '1': False, '2': False, '3': False, '4': False, } self.is_an_enemy = False self.is_food = False self.generative_model = GenerativeModel(network, SensoryInputPeepo(self), n_jobs=1)
def calculate_obstacles(self): self.view = {x: False for x in self.view} self.is_an_enemy = False self.is_food = False observations = self.obstacles for obstacle in observations: if self.rect.colliderect(obstacle[0].rect): if obstacle[1] == 0: self.stomach += 1 self.obstacles.remove(obstacle) else: self.bang += 1 # self.obstacles.remove(obstacle) peepo_vec = pg.math.Vector2(self.rect.center) if collision(obstacle[0].rect, peepo_vec, self.edge_left, self.edge_right, GoLPeepo.RADIUS): edge1 = end_line(GoLPeepo.RADIUS, self.rotation - 30, self.rect.center) edge2 = end_line(GoLPeepo.RADIUS, self.rotation - 20, self.rect.center) edge3 = end_line(GoLPeepo.RADIUS, self.rotation - 10, self.rect.center) edge4 = end_line(GoLPeepo.RADIUS, self.rotation, self.rect.center) edge5 = end_line(GoLPeepo.RADIUS, self.rotation + 10, self.rect.center) edge6 = end_line(GoLPeepo.RADIUS, self.rotation + 20, self.rect.center) edge7 = end_line(GoLPeepo.RADIUS, self.rotation + 30, self.rect.center) self.view['1'] = collision(obstacle[0].rect, peepo_vec, edge1, edge2, GoLPeepo.RADIUS) self.view['2'] = collision(obstacle[0].rect, peepo_vec, edge2, edge3, GoLPeepo.RADIUS) self.view['3'] = collision(obstacle[0].rect, peepo_vec, edge3, edge4, GoLPeepo.RADIUS) self.view['4'] = collision(obstacle[0].rect, peepo_vec, edge4, edge5, GoLPeepo.RADIUS) self.view['5'] = collision(obstacle[0].rect, peepo_vec, edge5, edge6, GoLPeepo.RADIUS) self.view['6'] = collision(obstacle[0].rect, peepo_vec, edge6, edge7, GoLPeepo.RADIUS) if obstacle[1] == 1: self.is_an_enemy = True if obstacle[1] == 0: self.is_food = True self.food = [] [self.food.append(x[0]) for x in self.obstacles if x[1] == 0]
def calculate_obstacles(self): self.view = {x: False for x in self.view} self.is_an_enemy = False self.is_food = False for obstacle in self.obstacles: if self.rect.colliderect(obstacle[0].rect): if obstacle[1] == 0: self.stomach += 1 self.obstacles.remove(obstacle) else: self.bang += 1 self.obstacles.remove(obstacle) self.food = [] [self.food.append(x[0]) for x in self.obstacles if x[1] == 0] self.ennemies = [] [self.ennemies.append(x[0]) for x in self.obstacles if x[1] == 1] self.assemble_obstacles() observations = [] for obstacle in self.obstacles: distance = math.sqrt( (obstacle[0].rect.center[0] - self.rect.center[0])**2 + (obstacle[0].rect.center[1] - self.rect.center[1])**2) if distance <= Peepo.RADIUS: observations.append(obstacle) edge1 = end_line(Peepo.RADIUS, self.rotation - 30, self.rect.center) edge2 = end_line(Peepo.RADIUS, self.rotation - 15, self.rect.center) edge3 = end_line(Peepo.RADIUS, self.rotation, self.rect.center) edge4 = end_line(Peepo.RADIUS, self.rotation + 15, self.rect.center) edge5 = end_line(Peepo.RADIUS, self.rotation + 30, self.rect.center) sectors = [[edge1, edge2], [edge2, edge3], [edge3, edge4], [edge4, edge5]] peepo_vec = pg.math.Vector2(self.rect.center) relevant_sector = ["0", self.rect.center, Peepo.RADIUS] closest_distance = 10000. for index, sector in enumerate(sectors): lower_edge = sector[0] upper_edge = sector[1] for obstacle in observations: is_collision = collision(obstacle[0].rect, peepo_vec, lower_edge, upper_edge, Peepo.RADIUS) if is_collision: distance = math.sqrt( (obstacle[0].rect.center[0] - peepo_vec[0])**2 + (obstacle[0].rect.center[1] - peepo_vec[1])**2) if distance <= closest_distance: closest_distance = distance relevant_sector[0] = str(index + 1) relevant_sector[1] = obstacle[0].rect.center relevant_sector[2] = closest_distance if obstacle[1] == 1: self.is_an_enemy = True self.is_food = False if obstacle[1] == 0: self.is_food = True self.is_an_enemy = False self.view = {x: False for x in self.view} self.view[relevant_sector[0]] = True sight_angle = 0 only_true = relevant_sector[0] if only_true == '0': sight_angle = self.rotation if only_true == '1': sight_angle = self.rotation - 22.5 if only_true == '2': sight_angle = self.rotation - 7.5 if only_true == '3': sight_angle = self.rotation + 7.5 if only_true == '4': sight_angle = self.rotation + 22.5 self.edge_middle = end_line(relevant_sector[2] / 20, sight_angle, relevant_sector[1])