How to change an image of a Sprite during the animation?

前端 未结 1 1752
春和景丽
春和景丽 2021-01-26 21:51

I want to change an image of the object worker each time when it stops.

The class Worker is created based on the answer of @sloth in this threa

1条回答
  •  一整个雨季
    2021-01-26 21:59

    I think you should introduce some kind of state to indicate that the worker is running or not. Here's an example. Note the comments:

    class Worker(pygame.sprite.Sprite):
    
        # we introduce to possible states: RUNNING and IDLE
        RUNNING = 0
        IDLE = 1
    
        def __init__(self, location, *groups):
    
            # each state has it's own image
            self.images = {
                Worker.RUNNING: pygame.transform.scale(get_image("worker.png"), (40, 40)),
                Worker.IDLE: pygame.transform.scale(get_image("worker_stopped.png"), (40, 40))
            }
    
            self._layer = 1
            pygame.sprite.Sprite.__init__(self, groups)
    
            # let's keep track of the state and how long we are in this state already            
            self.state = Worker.IDLE
            self.ticks_in_state = 0
    
            self.image = self.images[self.state]
            self.rect = self.image.get_rect(topleft=location)
    
            self.direction = pygame.math.Vector2(0, 0)
            self.speed = random.randint(2, 4)
            self.set_random_direction()
    
        def set_random_direction(self):
            # random new direction or standing still
            vec = pygame.math.Vector2(random.randint(-100,100), random.randint(-100,100)) if random.randint(0, 5) > 1 else pygame.math.Vector2(0, 0)
    
            # check the new vector and decide if we are running or fooling around
            length = vec.length()
            speed = sum(abs(int(v)) for v in vec.normalize() * self.speed) if length > 0 else 0
    
            if length == 0 or speed == 0:
                new_state = Worker.IDLE
                self.direction = pygame.math.Vector2(0, 0)
            else:
                new_state = Worker.RUNNING
                self.direction = vec.normalize()
    
            self.ticks_in_state = 0
            self.state = new_state
    
            # use the right image for the current state
            self.image = self.images[self.state]
    
        def update(self, screen):
            self.ticks_in_state += 1
            # the longer we are in a certain state, the more likely is we change direction
            if random.randint(0, self.ticks_in_state) > 30:
                self.set_random_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, change direction
            if not screen.get_rect().contains(self.rect):
                self.direction = self.direction * -1
    
            self.rect.clamp_ip(screen.get_rect())
    

    0 讨论(0)
提交回复
热议问题