I'm having issues with sprites in Pygame. I want them to change them so it seems like a animation and I really have no idea how to do this

Viewed 23
def redraw():

    global walkCount
    global x

    night(0, 0)

    street(0, -800)

    if walkCount + 1 >= 27:
        walkCount = 0
    if left:
        gameDisplay.blit(walkLeft(walkCount//3), (x, y))
        walkCount += 1
    elif right:
        gameDisplay.blit(walkRight(walkCount//3), (x, y))
        walkCount += 1
    else:
        gameDisplay.blit(char(x, y))

    pygame.display.flip()

so this is the definition for redrawing the Frame. walkLeft and walkRight are lists with pictures in them.

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT or event.key == pygame.K_a:
        x_change = -vel
        left = True
        right = False
    elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
        x_change = +vel
        right = True
        left = False
    else:
        right = False
        left = False
        walkCount = 0
if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_d or event.key == pygame.K_a:
        x_change = 0
        right = False
        left = False

and this is the logic for walking side to side.

Can you help me please, I'm an noob and I would like to learn.

1 Answers

walkLeft(walkCount//3) is a function call statement. However, walkLeft and walkRight are lists, but not functions. You have to get an element of the list with subscription (() -> []):

gameDisplay.blit(walkLeft(walkCount//3), (x, y))

gameDisplay.blit(walkLeft[walkCount//3], (x, y))

gameDisplay.blit(walkRight(walkCount//3), (x, y))

gameDisplay.blit(walkRight[walkCount//3], (x, y))
Related