2

I'm throwing myself at the mercy of the SO community, having looked at the below now for considerable time. I'm attempting to create an animated background for my title screen which loops through eight elements in a list called background_images. By calling the function bg_start_animation in the rocket_settings class, I am expecting to have self.background be populated with the first element of the list, for this to be passed to the main body of the code using return self.background, for this be blit to the screen, have the screen refreshed at display.flip, and then have the process repeated by the other seven elements of the list to create the animation effect.

I store the latest iteration of background image using:

animated_bg = rocket_settings.bg_start_animation()

and blit to the screen:

screen.blit(animated_bg, (0, 0))

please note: rocket_settings calls the Class for which I have copied below the main body of the code.

while True:
    pygame.time.delay(5)
    
    gf.check_events(rocket_settings, screen, character)
    character.update()
    gf.update_screen(rocket_settings, screen, character)

    # Redraw the screen during each pass through the loop
    character.update()
    #screen.blit(rocket_settings.background, (0, 0))
    animated_bg = rocket_settings.bg_start_animation()
    screen.blit(animated_bg, (0, 0))
    screen.blit(rocket_settings.title, (300, 180))
    character.blitme()
    #print(character.rect.bottom, ", ", character.rect.y) - to identify the coords
    #of the desired spawn point by experimenting in runtime.
    
    # Make the most recently drawn screen visible
    pygame.display.flip()

relevant part of rocket settings:

def bg_start_animation(self):
    # Screen animation loop
    background_images = ["images/wick_title_screen_1.bmp", 
    "images/wick_title_screen_2.bmp", "images/wick_title_screen_3.bmp",
    "images/wick_title_screen_4.bmp", "images/wick_title_screen_5.bmp",
    "images/wick_title_screen_6.bmp", "images/wick_title_screen_7.bmp",
    "images/wick_title_screen_8.bmp"]
    
    for background in background_images:
        self.background = pygame.image.load(background)
        return self.background

I must be overlooking something. Thanks in advance, I appreciate the assistance.

Rahman
  • 21
  • 4
  • Thanks for the time taken to provide link and the advice, I appreciate it. I'll have a more detailed look shortly. From a brief look however, I am struggling to apply that to my example given the specificity of my request, hoping for some feedback in relation to my example. I suppose I could ask why my code doesn't work. – Rahman May 22 '22 at 13:11

1 Answers1

1

See Animated sprite from few images. You can use the approach on both a sprite and the background. It is even possible to load the images from a GIF.

Create a list of images before the application loop and draw one image in the application loop. Use a counter (background_index) to iterate through the images:

def load_background():
    background_images = ["images/wick_title_screen_1.bmp", 
    "images/wick_title_screen_2.bmp", "images/wick_title_screen_3.bmp",
    "images/wick_title_screen_4.bmp", "images/wick_title_screen_5.bmp",
    "images/wick_title_screen_6.bmp", "images/wick_title_screen_7.bmp",
    "images/wick_title_screen_8.bmp"]
    
    surface_list = []
    for background in background_images:
        surface_list.append(pygame.image.load(background))
    return surface_list

background_surface = load_background()
background_index = 0

clock = pygame.time.Clock()
while True:
    clock.tick(100)
    gf.check_events(rocket_settings, screen, character)
    
    if background_index >= len(background_surface):
        background_index = 0
    screen.blit(background_surface[background_index], (0, 0))  
    background_index += 1

    # [...]  
Rabbid76
  • 202,892
  • 27
  • 131
  • 174