How to put a gif in a program

38 Views Asked by At

I'm trying to put a gif into my program but it doesn't appear animated, i tried to put all the images to make it animated but it didn't work, i tried using also chatGPT but it can't help me.

Is there a way i can put the .gif file to create the animation or do i need to use other libraries like Moviepy?

import pygame
import sys
import ctypes
import time
from moviepy.editor import VideoFileClip

pygame.init()

screen_width = 400
screen_height = 300

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("")

change_size_timer = pygame.time.get_ticks() + 4000

animating = False
animation_duration = 3000
start_size = (screen_width, screen_height)
end_size = (1250, 650)
animation_start_time = 0

clip = VideoFileClip("Misterius.gif")
animation_frames = [pygame.image.fromstring(frame.tostring(), frame.shape[:2], "RGB") for frame in clip.iter_frames()]
current_frame_index = 0
frame_duration = int(clip.duration / len(animation_frames) * 1000)

audio_played = False
background_changed = False
show_hello_text = True

def calculate_current_size(start_size, end_size, duration, elapsed_time):
    progress = min(1, elapsed_time / duration)
    current_width = int(start_size[0] + (end_size[0] - start_size[0]) * progress)
    current_height = int(start_size[1] + (end_size[1] - start_size[1]) * progress)
    return (current_width, current_height)

def resize_surface(surface, new_size, duration, start_time):
    elapsed_time = pygame.time.get_ticks() - start_time
    if elapsed_time >= duration:
        return pygame.transform.scale(surface, new_size)
    else:
        current_size = calculate_current_size(surface.get_size(), new_size, duration, elapsed_time)
        return pygame.transform.scale(surface, current_size)

def set_window_position(win, x, y):
    ctypes.windll.user32.SetWindowPos(win, 0, x, y, 0, 0, 0x0001)

def draw_hello_text_centered(surface, font, color, elapsed_time):
    hello_text = "HELLO"
    text = font.render(hello_text, True, color)
    text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))
    visible_chars = min(len(hello_text), int(elapsed_time / 400) + 1)
    visible_text = hello_text[:visible_chars]
    partial_text = font.render(visible_text, True, color)
    surface.blit(partial_text, text_rect)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    current_time = pygame.time.get_ticks()
    if current_time >= change_size_timer and not animating:
        animating = True
        animation_start_time = current_time

    if animating:
        screen_width, screen_height = resize_surface(screen, end_size, animation_duration,
                                                     animation_start_time).get_size()
        screen = pygame.display.set_mode((screen_width, screen_height))
        set_window_position(pygame.display.get_wm_info()["window"], 50, 50)
        pygame.display.set_caption("HELLO")
        if screen_width == end_size[0] and screen_height == end_size[1]:
            animating = False
            if not background_changed:
                pygame.mixer.Sound("Snap.mp3").play()
                background_changed = True

    if not animating and background_changed:
        screen.fill((255, 255, 255))

    if not animating and background_changed:
        animation_rect = animation_frames[current_frame_index].get_rect(center=(screen_width // 2, screen_height // 2))
        screen.blit(animation_frames[current_frame_index], animation_rect)
        if pygame.time.get_ticks() - animation_start_time >= frame_duration:
            current_frame_index = (current_frame_index + 1) % len(animation_frames)
            animation_start_time = pygame.time.get_ticks()

    if not animating and not audio_played and show_hello_text:
        font = pygame.font.Font(None, 90)
        if not background_changed:
            draw_hello_text_centered(screen, font, (255, 255, 255), current_time)
        else:
            audio_played = True
            show_hello_text = False

    pygame.display.flip()
    time.sleep(0.04)

pygame.quit()
sys.exit()
0

There are 0 best solutions below