예제 #1
0
파일: shard.py 프로젝트: mullevik/vizzard
class ShardSprite(pygame.sprite.Sprite):

    scene: 'GameScene'
    shard: Shard

    image: Surface
    rect: Rect
    hitbox: Rect

    animation: Animation

    def __init__(self, scene: 'GameScene', shard: Shard,
                 *groups: AbstractGroup):
        super().__init__(*groups)
        self.scene = scene
        self.shard = shard

        scale_factor = self.scene.settings.scale_factor

        # load images and create looping animation
        frames = load_scaled_surfaces(ANIM_SHARD_IDLE, scale_factor)
        self.image = frames[0]
        self.animation = Animation(frames, 75, loop=True)
        self.animation.start(pygame.time.get_ticks())

        center_of_first_tile = ((TILE_SIZE_PX // 2) * scale_factor,
                                (TILE_SIZE_PX // 2) * scale_factor)

        # set rect and hitbox
        self.rect = self.image.get_rect(center=center_of_first_tile)
        self.hitbox = self.rect.copy()
        self.hitbox.inflate_ip(-3 * scale_factor, -3 * scale_factor)

    def update(self, *args, **kwargs) -> None:
        self._update_rectangle_based_on_current_position()
        self.image = self.animation.get_image(pygame.time.get_ticks())

    def _update_rectangle_based_on_current_position(self):
        scale_factor = self.scene.settings.scale_factor
        position = self.shard.position

        x = (position.x * TILE_SIZE_PX) + (TILE_SIZE_PX // 2)
        x = x * scale_factor

        y = ((position.y - self.scene.vertical_shift) *
             TILE_SIZE_PX) + (TILE_SIZE_PX // 2)
        y = y * scale_factor

        self.rect.center = (x, y)
        self.hitbox.center = (x, y)
예제 #2
0
    def test_animation_should_loop_forever(self):
        dummy_frames = ["0", "1", "2"]
        dummy_delay = 10
        a = Animation(dummy_frames, dummy_delay, loop=True)

        # start animation at 100-th millisecond
        a.start(100)
        self.assertFalse(a.is_over())
        self.assertEqual("0", a.get_image(100))
        self.assertEqual("0", a.get_image(101))
        self.assertFalse(a.is_over())
        self.assertEqual("2", a.get_image(120))
        self.assertEqual("2", a.get_image(129))
        self.assertFalse(a.is_over())
        self.assertEqual("0", a.get_image(130))
        self.assertFalse(a.is_over())
        self.assertEqual("1", a.get_image(145))
        self.assertFalse(a.is_over())
        self.assertEqual("2", a.get_image(150))
        self.assertFalse(a.is_over())
        self.assertEqual("1", a.get_image(410))
        self.assertFalse(a.is_over())