0

I'm animating a character in Pygame, and currently, what is happening is the animating images are being laid on top of each other, with the ones on the bottom still being visible (they are slightly misaligned).

This is not a sprite sheet, which I know are popular, so the guidance I've received online will not work for me. Each image is independent from every other.

When I keyup, the images do disappear after the animation; I'm just trying to have Pygame remove the images in a sequence, as opposed to all at once after all have been processed.

Any advice would be appreciated!

note: I've tried iterating through a list, with the j1-j8 variables being a list of the pictures, and either Pygame was moving to fast for the movement to be perceived, or it otherwise did not seem to work at all.

#####example#######

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_b:
           
          
            SCREEN.blit(j1, (char_x, char_y))
            time.sleep(.1)
            pygame.display.update()
            SCREEN.blit(j2, (char_x, char_y))
            time.sleep(.1)
            pygame.display.update()
            SCREEN.blit(j7, (char_x, char_y))
            time.sleep(.1)
            pygame.display.update()
            SCREEN.blit(j8, (char_x, char_y))
            time.sleep(.1)
            pygame.display.update()

1 Answers1

0

A typical pygame main application looks something like:

Handle Events → Update Game State → Draw Background → Draw sprites → Update display

By redrawing the background, you clear away previous images so your animation is cleaner.

If you take too long handling events, your window manager will think your application has crashed. This is why calls to time.sleep() are discouraged.

If your sprite is animating, then you'll want it to be cycling through a set of images when its animation condition is met.

Here's a sample script that has a single sprite that changes its image every two seconds.

import itertools
import random
import time
import pygame

# initialise screen
pygame.init()
screen = pygame.display.set_mode((320, 240))
pygame.display.set_caption("Animation Demo")
# load / create initial images
color = pygame.Color("blueviolet")
background = pygame.Color("white")
image_size = (50, 50)
square_img = pygame.Surface(image_size)
square_img.fill(color)
circle_img = pygame.Surface(image_size)
circle_img.fill(background)
pygame.draw.circle(circle_img, color, circle_img.get_rect().center, image_size[0] // 2)
triangle_img = pygame.Surface(image_size)
triangle_img.fill(background)
points = (0, image_size[0]), image_size, (image_size[1], 0)
pygame.draw.polygon(triangle_img, color, points)

images = itertools.cycle((square_img, circle_img, triangle_img))

class Shifter(pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.pos = pos
        # select the initial image
        self.update_image()

    def update(self):
        """Cycle image if enough time has passed"""
        if time.time() - self.updated > 2:
            self.update_image()

    def update_image(self):
        """Change image and recenter"""
        self.image = next(images)
        self.rect = self.image.get_rect()
        self.rect.center = self.pos
        self.updated = time.time()

    def draw(self, screen):
        # Add this draw function so we can draw individual sprites
        screen.blit(self.image, self.rect)


# create a shape sprite in the center of the screen
shape = Shifter(screen.get_rect().center)
clock = pygame.time.Clock()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # clear the screen
    screen.fill(background)
    # update sprites
    shape.update()
    # draw sprites
    shape.draw(screen)
    # refresh display
    pygame.display.update()
    clock.tick(60)  # limit to 60 FPS

pygame.quit()

EX

import random
  • 3,054
  • 1
  • 17
  • 22
  • This is exactly what I needed. I have not jumped into Sprites, yet this is clearly the time. Thanks! – Internet User May 27 '22 at 17:57
  • I'm glad you found this answer helpful, you can [accept it](https://stackoverflow.com/help/someone-answers) if you like, even though the question was closed. Once you start looking at sprites, don't overlook [Groups](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group). They make it much easier to manage all the sprites you'll create. – import random May 28 '22 at 03:59