Best way to show multiple images in quick succession

I’m trying to use pygame to flash multiple images in quick succession, but the below code shows a white screen for some time, before showing a single image for a split second, and then closes.

Where am I going wrong?

import glob import pygame import os import pathlib import time  max_idx = 276 imgs_dir = 'path/to/imgs/...'  pygame.init() w = 480 h = 480 size=(w,h) screen = pygame.display.set_mode(size)  c = pygame.time.Clock()  idx = 0 while idx < max_idx:     idx+=1     print(idx)      screen.fill((255,255,255))      img_path = f'{imgs_dir}/{idx}.png'     img = pygame.image.load(img_path)     img_rect = img.get_rect()          screen.blit(img, img_rect)     pygame.display.update()          c.tick(1000) 

In my case, the filenames are the indexes, and they exist. (e.g. 'path/to/imgs/0.png', …, 'path/to/imgs/275.png')

Updated attempt

With the feedback, I’ve updated it to this, but it hasn’t resolved the issue:

pygame.init() w = 300 h = 300 size=(w,h) screen = pygame.display.set_mode(size)  c = pygame.time.Clock()  images = [] for idx in range(0, max_idx):     img_path = f'{new_folder_path}/{idx}.png'     img = pygame.image.load(img_path)     img = pygame.transform.scale(img, size)     images.append(img)  for img in images:  # I want it to close as soon as all images have been shown     img_rect = img.get_rect()     screen.blit(img, img_rect)     pygame.display.update()     c.tick(0.5)  pygame.quit() 
Add Comment
1 Answer(s)

Try loading images outside the main_loop since loading takes some time. Also the program will exit when the idx become True and that in your case is after 276 iterations, now your frame rate is set to 1000, reduce your number of frames. If you want to prevent program from exiting remove idx from the while condition and put some other variable which will become True when you close the window (and optionally add some other triggers) i.e. :

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

Also put pygame.quit() at the end.

EDIT: Solution code

import pygame as pg import os   WIN_W = 500 WIN_H = 500 FPS = 2  clock = pg.time.Clock()  window = pg.display.set_mode((WIN_W, WIN_H))  images_list = []  def load_images(images_list):     curr_dir = os.path.dirname(__file__)     dir_images = os.path.join(curr_dir, "images")      for i in range(2, 11, 2):            images_list.append(pg.image.load(os.path.join(dir_images, f"{i}c.png")))   load_images(images_list)   def redraw(i):      window.blit(images_list[i], (0,0))      pg.display.update()   run = True while run:      clock.tick(1)      for event in pg.event.get():         if event.type == pg.QUIT:             run = False      for i in range(len(images_list)):         pg.time.wait(100)         redraw(i)  pg.quit() 

I got the desired result with pygame.time.wait(). I’m not 100% sure but I think the problem with our earlier tries was that no matter the FPS the whole code in for loop gets executed at once, hence we see only last image. Using this approach only ONE blitting happens each pass. I know you want your program to exit when displaying is finished but this is easier for testing.

NOTE: don’t forget to change image paths and variable names.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.