Пример #1
0
    def __init__(self, images, frame_info,
                 all_groups: typing.Dict[str, pg.sprite.Group],
                 *groups: pg.sprite.Group):
        """

        :param images: List of all filenames used to animate this sprite.
        :param frame_info: List of dictionaries. Each dictionary has a 'start_frame' and a 'num_frames' key, one for
                           each animation for this sprite.
        :param all_groups: A dictionary of all the game world's sprite groups.
        :param groups: A possibly empty sequence of sprite groups that the sprite should be added to.
        """
        # Pass in default image to draw.
        BaseSprite.__init__(self, images[0], all_groups, *groups)

        # Load all images for this sprite.
        self._images = [self.image]
        self._images.extend(
            [image_loader.get_image(img) for img in images[1:]])
        for image in self._images:
            image.set_colorkey(cfg.BLACK)

        # Store animation data.
        self._frame_info = frame_info
        self._anim_num = 0  # Animation index.
        self._current_frame = 0  # Frame number of current animation.
        self._frame_time = 0  # Amount of time in current frame.
        self._anim_fps = 24.0  # Animation frame rate.

        # Animation 0 is the default
        self.change_anim(0)
Пример #2
0
 def __init__(self, title, size, color, buttons, ui_group):
     BaseSprite.__init__(self, Menu.IMAGE, ui_group)
     self.buttons = [
         Button(b['action'], b['text'], b['size'], b['color'], _BTN_IMAGES,
                ui_group) for b in buttons
     ]
     self._make(title, size, color)
Пример #3
0
 def rotate_image(sprite: BaseSprite, image: pg.Surface,
                  angle: float) -> None:
     """Rotates the sprite's image while keeping it centered at the same center-coordinates."""
     old_center = sprite.rect.center
     sprite.image = pg.transform.rotate(image, angle)
     sprite.rect = sprite.image.get_rect()
     sprite.rect.center = old_center
     sprite.hit_rect.center = sprite.rect.center
Пример #4
0
 def __init__(self, x: float, y: float, all_groups):
     BaseSprite.__init__(self, Barricade._IMAGE, all_groups,
                         all_groups['all'], all_groups['obstacles'])
     # Shrink hit-box and recenter.
     self.image = pg.transform.scale(
         self.image, (4 * self.rect.w // 5, 4 * self.rect.h // 5))
     self.rect = self.image.get_rect()
     self.hit_rect = self.rect
     self.rect.center = (x, y)
Пример #5
0
 def __init__(self, x: float, y: float, rot: float, all_groups):
     """Aligns the MuzzleFlash so that it starts at the tip of the Barrel nozzle."""
     self._layer = cfg.EFFECTS_LAYER
     BaseSprite.__init__(self, MuzzleFlash.IMAGE, all_groups, all_groups['all'])
     RotateMixin.__init__(self)
     self.rect.center = (x, y)
     self.rot = rot
     self.rotate()
     self._spawn_timer = Timer()
Пример #6
0
 def __init__(self, x, y, scale_h, scale_w, rot, groups: typing.Dict[str, pg.sprite.Group]):
     """Sets the sprite's position and angle value so that it matches and trails the tank's path."""
     self._layer = cfg.TRACKS_LAYER
     BaseSprite.__init__(self, Tracks.IMAGE, groups, groups['all'])
     # Transform and recenter.
     self.image = pg.transform.rotate(self.image, rot - Tracks.IMG_ROT)
     self.image = pg.transform.scale(self.image, (scale_h, scale_w))
     self.rect = self.image.get_rect()
     self.rect.center = (x, y)
     self._alpha = 255
Пример #7
0
 def __init__(self, x: float, y: float, image: str, sound: str,
              groups: typing.Dict[str, pg.sprite.Group]):
     BaseSprite.__init__(self, image, groups, groups['all'],
                         groups['items'])
     self.rect.center = (x, y)
     self._sfx = sound
     self._spawn_pos = pg.math.Vector2(x, y)
     self._effect_timer = Timer()
     # Default duration is 0.
     self._duration = 0
     # Tween function maps integer steps to values between 0 and 1.
     self._tween = tween.easeInOutSine
     self._step = 0
     self._direction = 1
Пример #8
0
 def __init__(self, x: float, y: float, angle: float, color: str,
              category: str, owner,
              all_groups: typing.Dict[str, pg.sprite.Group]):
     """Creates a bullet object, rotating it to face the correct direction."""
     self._layer = cfg.ITEM_LAYER
     BaseSprite.__init__(self, _IMAGES[category][color], all_groups,
                         all_groups['all'], all_groups['bullets'])
     MoveMixin.__init__(self, x, y)
     self.vel = pg.math.Vector2(_STATS[category]["speed"], 0).rotate(-angle)
     self._damage = _STATS[category]["damage"]
     self._lifetime = _STATS[category]["lifetime"]
     self._spawn_timer = Timer()
     self._owner = owner
     RotateMixin.rotate_image(self, self.image, angle - Bullet.IMAGE_ROT)
Пример #9
0
    def __init__(self, tank, offset: pg.math.Vector2, image: str, color: str,
                 category: str, all_groups: typing.Dict[str, pg.sprite.Group]):
        """Fills up the Barrel's ammo and centers its position on its parent."""
        self._layer = cfg.BARREL_LAYER
        BaseSprite.__init__(self, image, all_groups, all_groups['all'])
        RotateMixin.__init__(self)
        # Bullet parameters.
        self._category = category
        self._color = color
        self._ammo_count = _STATS[self._category]["max_ammo"]

        # Parameters used for barrel position.
        self._parent = tank
        self.rect.center = tank.rect.center
        self._offset = offset

        self._fire_delay = _STATS[self._category]["fire_delay"]
        self._fire_timer = Timer()
Пример #10
0
    def __init__(self, x: float, y: float, img: str,
                 all_groups: typing.Dict[str, pg.sprite.Group]):
        """Initializes the tank's sprite with no barrels to shoot from.

        :param x: x coordinate for centering the sprite's position.
        :param y: y coordinate for centering the sprite's position.
        :param img: filename for the sprite's tank image.
        :param all_groups: A dictionary of all of the game world's sprite groups.
        """
        self._layer = cfg.TANK_LAYER
        BaseSprite.__init__(self, img, all_groups, all_groups['all'],
                            all_groups['tanks'], all_groups['damageable'])
        MoveNonlinearMixin.__init__(self, x, y)
        RotateMixin.__init__(self)
        DamageMixin.__init__(self, self.hit_rect)
        self.rect.center = (x, y)
        self.MAX_ACCELERATION = 768
        self._barrels = []
        self._items = []
        self._track_timer = Timer()
Пример #11
0
 def __init__(self, x, y, max_durability, image, groups: typing.Dict[str, pg.sprite.Group]):
     BaseSprite.__init__(self, image, groups, groups['item_boxes'], groups['obstacles'], groups['all'])
     self.rect.center = (x, y)
     self._durability = max_durability
     self._disappear_alpha = itertools.chain(ItemBox._DISAPPEAR_ALPHA * 2)
Пример #12
0
 def __init__(self, x: float, y: float, all_groups):
     self._layer = cfg.ITEM_LAYER
     BaseSprite.__init__(self, Tree._IMAGE, all_groups, all_groups['all'],
                         all_groups['obstacles'])
     self.rect.center = (x, y)