pygame with gif(image sequence) and control play speed(code)

_

:) Ideas

the pygame redraw map in a while loop, so we just need calculate time and then using different image path.

so we use image sequence to simulate gif.

use

imgGif = Gif('./gif/talks/')

write your own image sequence folder in the code.

use function build_gif(screen, x, y, gif, million_seconds) in the while loop of pygame.
for example:

build_gif(screen, x, y, imgGif, 500)

and this is the source file.

Gif.py

import time
import os
import pygame


class Gif:
    def __init__(self, gif_path):
        self.current_index = 0
        self.sync_time = int(round(time.time() * 1000))
        self.gif_path = gif_path
        self.image_path_arr = os.listdir(gif_path)


imgGif = Gif('./gif/talks/')
# first input Gif folder path here

ux = 25
uy = 25


# use this funtion in 'creation_niveau' to build gif
def build_gif(screen, x, y, gif, million_seconds):
    img = pygame.image.load(get_gif_sequence(gif, million_seconds))
    img = pygame.transform.scale(img, (ux, uy))
    screen.blit(img, (x * ux, y * uy))


def get_gif_sequence(gif, time_between_gifs):
    current_index = gif.current_index
    sync_time = gif.sync_time
    gif_path = gif.gif_path
    image_path_arr = gif.image_path_arr
    all_length = len(image_path_arr)
    img_path = gif_path + image_path_arr[current_index]
    new_time = int(round(time.time() * 1000))
    if new_time - sync_time > time_between_gifs:
        gif.sync_time = new_time
        if current_index < all_length - 1:
            gif.current_index += 1
        else:
            gif.current_index = 0
    else:
        pass
    return img_path

已发布

分类

, ,

来自

标签:

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注