Esempio n. 1
0
def create_animation(frames, duration, loop):
    """ Create animation from frames, a list of surfaces

    :param frames:
    :param duration:
    :param loop:
    :return:
    """
    data = [(f, duration) for f in frames]
    animation = pyganim.PygAnimation(data, loop=loop)
    conductor = pyganim.PygConductor({'animation': animation})
    return animation, conductor
Esempio n. 2
0
    def __init__(self,
                 images,
                 position,
                 animation_speed=0.2,
                 animation_loop=False):

        # Check to see what kind of image(s) are being loaded.
        images_type = type(images).__name__

        # Handle loading a single image, multiple images, or surfaces
        if images_type == 'str' or images_type == 'unicode':
            surface = tools.load_and_scale(images)
            self.images = [(surface, animation_speed)]

        elif images_type == 'list' or images_type == 'tuple':
            self.images = []

            for item in images:
                item_type = type(item).__name__

                if item_type == 'str' or item_type == 'unicode':
                    surface = tools.load_and_scale(images)
                else:
                    surface = item
                self.images.append((surface, animation_speed))

        else:
            surface = images
            self.images = [(surface, animation_speed)]

        # Create a pyganimation object using our loaded images.
        self.animation = pyganim.PygAnimation(self.images, loop=animation_loop)
        self.animation.play()
        self.animation.pause()

        self.rect = self.images[0][0].get_rect(topleft=position)

        self.visible = True
        self.state = ""

        self.moving = False
        self.move_destination = (0, 0)
        self.move_delta = [0, 0]
        self.move_duration = 0.
        self.move_time = 0.
        self.fading = False
        self.fade_duration = 0.
        self.shaking = False
Esempio n. 3
0
    def play_map_animation(self, game, action):
        """Plays a map animation at a given position in the world map.

        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters

        :type game: core.control.Control
        :type action: Tuple

        :rtype: None
        :returns: None

        Valid Parameters: animation_name,duration,loop,pos_x,pos_y

        * animation_name - The name of the animation stored under resources/animations/tileset.
            For example, an animation called "grass" will load frames called "grass.xxx.png".
        * duration - The duration of each frame of the animation in seconds.
        * loop - Can be either "loop" or "noloop" to loop the animation.
        * position - Can be either an x,y coordinate or "player" to draw the animation at the
            player's location.

        """

        # ('play_animation', 'grass,1.5,noloop,player', '1', 6)
        # "position" can be either a (x, y) tile coordinate or "player"
        animation_name = action.parameters[0]
        duration = float(action.parameters[1])
        directory = prepare.BASEDIR + "resources/animations/tileset"

        if action.parameters[2] == "loop":
            loop = True
        elif action.parameters[2] == "noloop":
            loop = False

        # Determine the screen position where to draw the animation.
        if action.parameters[3] == "player":
            position = (game.player1.tile_pos[0], game.player1.tile_pos[1])

        else:
            position = (int(action.parameters[3]), int(action.parameters[4]))

        # Check to see if this animation has already been loaded.
        # If it has, play the animation using the animation's conductor.
        if animation_name in game.animations:
            game.animations[animation_name]["position"] = position
            game.animations[animation_name]["conductor"].play()
            return True

        # Loop through our animation resources and find the animation files based on name.
        scale = prepare.SCALE
        images_and_durations = []
        for animation_frame in os.listdir(directory):
            pattern = animation_name + "\.[0-9].*"
            if re.findall(pattern, animation_frame):
                frame = pygame.image.load(directory + "/" +
                                          animation_frame).convert_alpha()
                frame = pygame.transform.scale(
                    frame,
                    (frame.get_width() * scale, frame.get_height() * scale))
                images_and_durations.append((frame, duration))

        # Scale the animations based on our game's scale: world.scale

        # Create an animation object and conductor.
        animation = pyganim.PygAnimation(images_and_durations, loop=loop)
        conductor = pyganim.PygConductor({'animation': animation})
        conductor.play()

        game.animations[animation_name] = {
            "animation": animation,
            "conductor": conductor,
            "position": position,
            "layer": 3
        }
Esempio n. 4
0
    def __init__(self,
                 images,
                 position,
                 screen,
                 scale=True,
                 animation_speed=0.2,
                 animation_loop=False):

        # Handle loading a single image, multiple images, or surfaces
        if type(images) is str or type(images) is unicode:
            surface = pygame.image.load(images).convert_alpha()
            self.original_width = surface.get_width()
            self.original_height = surface.get_height()
            if scale:
                surface = self.scale_surface(surface)
            self.images = [(surface, animation_speed)]

        elif type(images) is list or type(images) is tuple:
            self.images = []
            for item in images:
                if type(item) is str or type(item) is unicode:
                    surface = pygame.image.load(item).convert_alpha()
                    self.original_width = surface.get_width()
                    self.original_height = surface.get_height()
                    if scale:
                        surface = self.scale_surface(surface)
                else:
                    self.original_width = surface.get_width()
                    self.original_height = surface.get_height()
                    if scale:
                        surface = self.scale_surface(surface)
                    else:
                        surface = item
                self.images.append((surface, animation_speed))

        else:
            self.original_width = images.get_width()
            self.original_height = images.get_height()
            if scale:
                surface = self.scale_surface(images)
            else:
                surface = images
            self.images = [(surface, animation_speed)]

        # Create a pyganimation object using our loaded images.
        self.animation = pyganim.PygAnimation(self.images, loop=animation_loop)
        self.animation.play()
        self.animation.pause()

        self.position = position
        self.last_position = position
        self.screen = screen
        self.visible = True
        self.state = ""

        self.width = self.images[0][0].get_width()
        self.height = self.images[0][0].get_height()
        self.moving = False
        self.move_destination = (0, 0)
        self.move_delta = (0, 0)
        self.move_duration = 0.
        self.move_time = 0.
        self.fading = False
        self.fade_duration = 0.
        self.shaking = False