[Python] Displaying image in Pygame and overwriting the filename to next image

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página