def step(self, dt: float) -> None: super(Mob, self).step(dt) self.player.step() if self.__repr_counter < 0: self.__repr_counter += 1 # Update position coefficient_friction = 0.5 if self.__velocity > 0: self.__velocity -= self.acceleration_constant * coefficient_friction old_pos = self.position self.position = Position(self.position.x + math.cos(self.position.angle) * self.__velocity * dt, self.position.y + math.sin(self.position.angle) * self.__velocity * dt, self.position.angle) self.energy -= 0.001 contact = not self.is_in_bounds() if not contact: e = self.is_contact_any_entity() if e: self.on_contact(e) contact = True if contact: self.stop() self.position = Position(old_pos.x, old_pos.y, self.position.angle) if self.energy < 0: self.kill()
def __convert_position(parent: Position, relative: Position): try: return Position( parent.x + math.sqrt(relative.x ** 2 + relative.y ** 2) * math.cos(parent.angle + relative.angle + math.atan(relative.y / relative.x)), parent.y + math.sqrt(relative.x ** 2 + relative.y ** 2) * math.sin(parent.angle + relative.angle + math.atan(relative.y / relative.x)), parent.angle + relative.angle ) except ZeroDivisionError: return Position( parent.x + relative.y * math.cos(parent.angle + relative.angle + math.pi / 2), parent.y + relative.y * math.sin(parent.angle + relative.angle + math.pi / 2), parent.angle + relative.angle )
def __add__(self, other): assert isinstance(other, type(self)) x = int((self.position.x + other.position.x) / 2) y = int((self.position.y + other.position.y) / 2) a = (self.position.angle + other.position.angle) / 2 new = copy.copy(self) new.position = Position(x, y, a) return new
def __init__(self, parent: entity.Entity, relative_x: float, relative_y: float, relative_angle: float, width: float, world: World): self.__relative_position = Position(relative_x, relative_y, relative_angle) self.__parent_entity = parent ''' super(ParentedDetectorSquare, self).__init__(parent.position.x + relative_x, parent.position.y + relative_y, parent.position.angle + relative_angle, width, world) ''' abs_position = self.__convert_position(parent.position, self.__relative_position) super(ParentedDetectorSquare, self).__init__(abs_position.x, abs_position.y, abs_position.angle, width, world)
def __init__(self, parent: entity.Entity, offset: Position, num_x: int, num_y: int, per_width: float, world: World): buffer = 0.1 delta = per_width + buffer position_array = [ [ Position( x * delta, - ((num_y - 1) / 2 * delta) + (y * delta), 0. ) + offset for y in range(num_y) ] for x in range(num_x) ] super(ParentedDetectorArrayAuto, self).__init__( parent, [i for j in position_array for i in j], per_width, world)
def reproduce(e_1: Entity, e_2: Entity) -> None: try: assert e_1.world == e_2.world assert type(e_1) == type(e_2) new = e_1 + e_2 e_1.world.add_entity(new) margin = 1 while new.is_contact_any_entity() or not new.is_in_bounds(): new.position = Position(new.position.x + random.randint(-int(margin), int(margin)), new.position.y + random.randint(-int(margin), int(margin)), new.position.angle) margin += 0.1 except AssertionError: pass
def __init__(self, x: float, y: float, angle: float, image: pg.image.AbstractImage, world: World, tangible: bool): """ Creates an Entity instance. :param x: Starting x position :param y: Starting y position :param angle: Starting angle :param image: Image for sprite :param world: World instance for sprite """ self.__position = Position(x, y, angle) self.__sprite = pg.sprite.Sprite(image, self.position.x, self.position.y, batch=world.batch) self.__sprite.rotation = -math.degrees(self.position.angle) + 90 self.__sprite.scale = self.__sprite.scale / 4 self.__window = world.window self.__world = world self.__tangible = tangible
def __init__(self, entity: Mob): super(PlayablePlayer, self).__init__(entity) from predatorprey.ai import ParentedDetectorArrayAuto self.detector = ParentedDetectorArrayAuto(entity, Position(15., 0., 0.), 5, 10, 5., entity.world)
def rotate_counterclockwise(self) -> None: self.position = Position(self.position.x, self.position.y, self.position.angle + self.rotation_constant)