Como alterar a imagem de um Sprite durante a animação?

Eu quero mudar uma imagem do objetoworker sempre que parar.

A classeWorker é criado com base na resposta de@preguiça noesta discussão.

class Worker(pygame.sprite.Sprite):
    def __init__(self, image_file, location, *groups):
        # we set a _layer attribute before adding this sprite to the sprite groups
        # we want the workers on top
        self._layer = 1
        pygame.sprite.Sprite.__init__(self, groups)
        self.image = pygame.transform.scale(pygame.image.load(image_file).convert_alpha(), (40, 40))
        self.rect = self.image.get_rect(topleft=location)
        # let's call this handy function to set a random direction for the worker
        self.change_direction()
        # speed is also random
        self.speed = random.randint(1, 3)

    def change_direction(self):
        # let's create a random vector as direction, so we can move in every direction
        self.direction = pygame.math.Vector2(random.randint(-1,1), random.randint(-1,1))

        # we don't want a vector of length 0, because we want to actually move
        # it's not enough to account for rounding errors, but let's ignore that for now
        while self.direction.length() == 0:
            self.direction = pygame.math.Vector2(random.randint(-1,1), random.randint(-1,1)) 

        # always normalize the vector, so we always move at a constant speed at all directions
        self.direction = self.direction.normalize()

    def update(self, screen):
        # there is a less than 1% chance every time that direction is changed
        if random.uniform(0,1)<0.005:
            self.change_direction()

        # now let's multiply our direction with our speed and move the rect
        vec = [int(v) for v in self.direction * self.speed]
        self.rect.move_ip(*vec)

        # if we're going outside the screen, move back and change direction
        if not screen.get_rect().contains(self.rect):
            self.change_direction()
        self.rect.clamp_ip(screen.get_rect())

Eu tento criar um cache de imagens pré-carregadas

image_cache = {}
def get_image(key):
    if not key in image_cache:
        image_cache[key] = pygame.image.load(key)
    return image_cache[key]

Então, suponho que seja necessário adicionar o seguinte código nodef __init__:

images = ["worker.png", "worker_stopped.png"]
for i in range(0,len(images)):
   self.images[i] = get_image(images[i])

e o código a seguir emdef update(self):

if self.direction.length() == 0:
    self.image = self.images[1]
else:
    self.image = self.images[0]

No entanto, parece não funcionar corretamente. A imagem antigaworker.png não desaparece e toda a animação é bloqueada.

questionAnswers(1)

yourAnswerToTheQuestion