Trying to play animation in pygame causes error

Viewed 27

I am trying to make a game where in the startscreen I want to have an animation play when press start. It gives me the error "TypeError: argument 1 must be pygame.Surface, not list". This is how I load the images for the animation:

for i in range(3, 53):
    menuanimation.append(pygame.image.load("menu/menu" + str(i) +".jpg"))

Here is where it is called from the button

    if event.type == pygame.MOUSEBUTTONDOWN:
        mouse_pos = event.pos
        if button1.collidepoint(mouse_pos):
            menuanimations()
            player.run = True
            player.start = False

And here is where the function is

def menuanimations():
    win.blit(menuanimation, (0, 0))
    print("testing")
1 Answers

The problem is that your menuanimation variable is a list of Surfaces and not a Surface. The Pygame function Surface.blit() expects a single surface (image) as a parameter, you're giving it a list. Probably you mean it to only paint a single image from the list, so the list-variable needs to be indexed.

You probably want something like:

def menuanimations():
    global animation_frame, menuanimation
    if ( animation_frame >= len( menuanimation ) ):         # Stay within limit
        animation_frame = 0
    win.blit( menuanimation[ animation_frame ], ( 0, 0 ) )  # Index into the list
    animation_frame += 1

And obviously you need to initialise animation_frame=0

Related