def update(self): self.turn_towards(self.target.position) self.image = Surface(self.rect.size) rot = lambda point: Vector(point).rotate(self.direction ) + self.center_point polygon(self.image, self.color, [rot(p) for p in self.points]) MovingSprite.update(self)
def __init__(self, x, y, mouse): MovingSprite.__init__(self, x, y) Sprite.__init__(self, Game.current.sprites) Game.current.sprites.change_layer(self, Game.LAYER_PLAYER) self.target = mouse self.color = pygame.Color("white") self.points = [(12, 0), (-12, 10), (-4, 0), (-12, -10)] self.center_point = Vector(12, 12)
def __init__(self, x, y, direction): Sprite.__init__(self, game.sprites) MovingSprite.__init__(self, x, y, self.low_speed, direction) BoxedInSprite.__init__(self, game.screen_rect.inflate(-20, -20)) neighborhood.observe(self) self._normal_image = self.make_image(self.color) self._bright_image = self.make_image(self.running_color) self._accel = (self.high_speed - self.low_speed) / self.accel_sluggish self._decel = self._accel * 0.27 self.max_turning_speed = 90 / self.turn_sluggish self._turn_min = -self.max_turning_speed self._turn_max = self.max_turning_speed self._nearest_animal = None self._nearest_animal_distance = None
def test_constructor(): thing = MovingSprite(9, 5) assert thing.x == 9 assert thing.y == 5 thing = MovingSprite(x=9, y=5, direction=45, speed=1000) assert thing.x == 9 assert thing.y == 5 assert thing.direction == 45 assert thing.speed == 1000 mag, deg = thing.motion.as_polar() assert deg == 45 assert mag == 1000 thing = MovingSprite(9, 5, 1000, 45) assert thing.x == 9 assert thing.y == 5 assert thing.direction == 45 assert thing.speed == 1000
def test_vector_copy(): thing1 = MovingSprite(100, 100) thing2 = MovingSprite(0, 0) thing2.position = thing1.position assert thing2.x == thing1.x assert thing2.y == thing1.y thing1 = MovingSprite(80, 80) thing2 = MovingSprite() thing2.position = thing1.position assert thing2.x == thing1.x assert thing2.y == thing1.y
def test_basic_move(): thing1 = MovingSprite(x=100, y=100) thing1.speed = 10 thing1.direction = DEGREES_EAST thing1.cartesian_motion() assert thing1.x == 110 assert thing1.y == 100 thing1.x = 100 assert thing1.x == 100 assert thing1.direction == 0.0
def test_direction_to(): tolerance = 0.0001 thing = MovingSprite(0.0, 0.0) for degree in range(0, 720): target = MovingSprite(0, 0, degree, 10) target.cartesian_motion() dirc = normal_degrees(thing.direction_to_thing(target)) ndeg = normal_degrees(degree) assert dirc - ndeg < tolerance \ or dirc - 360 - ndeg < tolerance \ or dirc + 360 - ndeg < tolerance
def __init__(self, box): MovingSprite.__init__(self) BoxedInSprite.__init__(self, box)
def do_not_test_side_facing(): thing1 = MovingSprite(x=100, y=100) thing1.direction = 0 thing1.speed = 10 thing2 = MovingSprite() thing2.speed = 10 thing2.position = thing1.position thing2.degrees = 0 thing2.cartesian_motion() assert thing1.side_facing(thing2) == FACE_FORWARD thing2.position = thing1.position thing2.degrees = 90 thing2.cartesian_motion() assert thing1.side_facing(thing2) == FACE_RIGHT thing2.position = thing1.position thing2.degrees = 180 thing2.cartesian_motion() assert thing1.side_facing(thing2) == FACE_BEHIND thing2.position = thing1.position thing2.degrees = 270 thing2.cartesian_motion() assert thing1.side_facing(thing2) == FACE_LEFT
def test_motion(): thing = MovingSprite(0, 0) thing.set_motion_polar(100, 90) assert thing.direction == 90 assert thing.speed == 100
def test_turn_towards(): tolerance = 0.0001 # Setup subject in the middle of imaginary space subject = MovingSprite(x=0.0, y=0.0, direction=DEGREES_EAST, speed=1.0) subject.max_turning_speed = 22.5 # Setup target: target = MovingSprite(x=0.0, y=0.0) # Move target due south from center target.position = Vector(0.0, 0.0) target.direction = DEGREES_SOUTH target.cartesian_motion() # Check that subject turned south for stop in [22.5, 45.0, 67.5, 90.0]: subject.turn_towards(target.position) assert abs(subject.turning_speed - subject.max_turning_speed) < tolerance assert abs(subject.direction - stop) < tolerance # Reset the subject subject.direction = DEGREES_EAST subject.turning_speed = 0.0 # Move target due north from center target.position = Vector(0.0, 0.0) target.direction = DEGREES_NORTH target.cartesian_motion() # Check that subject turned north for stop in [-22.5, -45.0, -67.5, -90.0]: subject.turn_towards(target.position) assert abs(subject.turning_speed + subject.max_turning_speed) < tolerance assert abs(subject.direction - stop) < tolerance # Reset the subject subject.direction = DEGREES_EAST subject.turning_speed = 0.0 # Move target southwest from center target.position = Vector(0.0, 0.0) target.direction = DEGREES_SOUTHWEST target.cartesian_motion() # Check that subject turned south for stop in [22.5, 45.0, 67.5, 90.0]: subject.turn_towards(target.position) assert abs(subject.turning_speed - subject.max_turning_speed) < tolerance assert abs(subject.direction - stop) < tolerance # Reset the subject subject.direction = DEGREES_EAST subject.turning_speed = 0.0 # Move target northwest from center target.position = Vector(0.0, 0.0) target.direction = DEGREES_NORTHWEST target.cartesian_motion() # Check that subject turned north for stop in [-22.5, -45.0, -67.5, -90.0]: subject.turn_towards(target.position) assert abs(subject.turning_speed + subject.max_turning_speed) < tolerance assert abs(subject.direction - stop) < tolerance # Reset the subject subject.direction = DEGREES_EAST subject.turning_speed = 0.0 # Move target due west from center target.position = Vector(0.0, 0.0) target.direction = DEGREES_WEST target.cartesian_motion() # Check that subject turned north subject.turn_towards(target.position) subject.update() assert abs(subject.turning_speed) == subject.max_turning_speed assert abs(subject.direction) == abs(subject.direction)
def __init__(self, x, y): MovingSprite.__init__(self, x, y) Sprite.__init__(self, Game.current.sprites) Game.current.sprites.change_layer(self, Game.LAYER_ABOVE_PLAYER) self.color = pygame.Color("yellow")
def __init__(self, x, y): MovingSprite.__init__(self, x, y) Flipper.__init__(self, FlipThrough("appear"), FlipNone()) Sprite.__init__(self, Game.current.sprites) Game.current.sprites.change_layer(self, Game.LAYER_PLAYER)
def __init__(self, area, x, y): MovingSprite.__init__(self, x, y) area.observe(self)