mail@pastecode.io avatar
a month ago
1.5 kB
def init_animation_frames(file_name, frame_count):
    Separates the frames of the animation and puts them into a list.
    :param file_name: The name of the image file
    :param frame_count: The number of frames in the sprite sheet
    :return: List of the frames from the sprite sheet
    sprite_sheet_image = pygame.image.load(file_name).convert_alpha()   # load the image
    frame_width = sprite_sheet_image.get_width() / frame_count  # set the width of each frame by dividing the total width by the number of frames
    frame_height = sprite_sheet_image.get_height()  # get the height of the image
    animation_frames = []   # set up our frame list

    # loop for the number of frames in our image and fill up our frame list
    for frame in range(frame_count):
        # create a new surface for our image
        frame_surface = pygame.Surface((frame_width, frame_height)).convert_alpha()
        # set the background color that will be removed
        # draw the image onto the new surface, determining the loatation of the frame by frame number * frame width
        frame_surface.blit(sprite_sheet_image, (0, 0), ((frame * frame_width), 0, frame_width, frame_height))
        # the transparent part of the picture will be replaced with blue, so we need to set it back to transparent
        # add the new image to the end of the frame list

    return animation_frames
Leave a Comment