pythonanimationpygame

Displaying image in Pygame and overwriting the filename to next image


I have been working on a project using Python 3.2 and Pygame. Everything has been going fine, except for this issue regarding loading images.

Originally, I wanted to display an animation I made in the main menu of my game, using a video format, but I could not get Pygame.movie working properly, and I am not comfortable in installing multiple plugins to get things working.

I decided to try out a different method, which involves the saved image sequence from my animation (with over 300+ images). While I have created another python application to automatically generate the code by itself (rather than me duplicating a piece of code many times), I do not think it is efficient to have the program have 1000+ lines of code to display everything.

My problem is that I am not quite sure how to create a loop in which the displayed image (examplefilename0001.jpg) is overwritten so that it displays the next file (examplefilename0002.jpg, examplefilename0003, etc.).

The code that I have written is below, but it gives me errors stating that there are unsupported operand types.

counter=1
for num in range(0,774):
    mainmenu=pygame.image.load('Imgsequence/examplefilename000' + str(counter)+ '.jpg')
    screen.blit(mainmenu +str(counter)+ (100,140))
    pygame.display.flip()
    counter=counter+1
    time.sleep(0.041)

Is there anyway to fix the code above? Or is there an alternative method of achieving the same/similar result I want?


Solution

  • The function screen.blit takes two arguments: the image (pygame.Surface) and the position (usually pygame.Rect but tuple/list etc. works as well). I'm not sure what you're doing but you're only passing in one argument where you're trying to add an image, a string and a tuple together, which result in an error. My guess is that you want screen.blit(mainmenu, (100,140)).

    However, your program won't work because you need to process events ever so often to ensures your program can internally interact with the rest of the operating system. If you don't the operating system will think pygame has crashed.

    You can create a main loop that resembles the following structure:

    counter = 0
    image = pygame.image.load('Imgsequence/examplefilename000' + str(counter) + '.jpg')
    last_time = pygame.time.get_ticks()
    running = True
    
    while running:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        if pygame.time.get_ticks() - last_time >= 41 and counter < 774:  # 41 milliseconds instead of 0.041 seconds.
            counter += 1
            image = pygame.image.load('Imgsequence/examplefilename000' + str(counter) + '.jpg')
            last_time = pygame.time.get_ticks()
    
        screen.blit(image, (100, 140))
        pygame.display.update()
    

    All GUI programs needs a main loop. Pygame also have tutorials at their website.