Esempio n. 1
0
    def __init__(self, spritesheet_resource_name, single_sprite_size, frame_duration, repeat_animation, component_texture_colourkey = None, spritesheet_size = None, component_bounding_rect_size = None, component_bounding_rect_position = None, on_animation_over = None):
        super(InternalClockHolder, self).__init__()
        # Initialise the component's sprite data object
        self.sprite_data = game_resources.SpriteData()
        # Set the component's bounding rect size
        self.sprite_data.set_bounding_rect_dimensions(single_sprite_size)

        # Load the texture for the sprite sheet (the one main image, within which all the images for different stages of
        # the animation can be found)
        self._sprite_sheet = game_resources.load_texture(spritesheet_resource_name, component_texture_colourkey, spritesheet_size)

        # Set variables
        self._sprite_frame_duration = frame_duration
        self._sprite_rect = pygame.Rect((0, 0), single_sprite_size)
        self._sprite_colourkey = component_texture_colourkey
        self._repeated = repeat_animation
        self.on_animation_over = on_animation_over

        self._current_frame = 0

        # Calculate the number of frames (using the width of the sprite sheet and the width of the individual sprites)
        # The sprite sheet must be correctly sized beforehand
        self._total_frame_count = self._sprite_sheet.get_width() / self._sprite_rect.width

        # Load the first texture
        self.sprite_data.texture = game_resources.load_sub_texture(self._sprite_sheet, self._sprite_rect, self._sprite_colourkey)
Esempio n. 2
0
    def update(self):
        # Increase the internal count of the animation
        self._internal_count += 1

        # If the internal count has reached the point of the next frame
        if self._internal_count == (self._current_frame + 1) * self._sprite_frame_duration:
            # Updated the position of the dimensions of the current sprite
            self._sprite_rect.x = self._sprite_rect.width * self._current_frame
            # Re-load the new texture
            self.sprite_data.texture = game_resources.load_sub_texture(self._sprite_sheet, self._sprite_rect, self._sprite_colourkey)
            # Update the current frame
            self._current_frame += 1

        # If the internal count has reached the final point along set of frames (it has finished)
        if self._internal_count == ((self._total_frame_count + 1) * self._sprite_frame_duration):
            # If the animation is supposed to repeat
            if self._repeated:
                # Reset the animation
                reset()
            # Otherwise
            else:
                # Fire the on_animation_over event
                self.on_animation_over(self)