Python 如何在动画期间更改精灵的图像?

Python 如何在动画期间更改精灵的图像?,python,pygame,Python,Pygame,每次停止时,我都要更改对象工作者的图像 类Worker是基于中的答案创建的 然后我假设有必要将以下代码添加到def\uuuu init\uuu中: images = ["worker.png", "worker_stopped.png"] for i in range(0,len(images)): self.images[i] = get_image(images[i]) 并将以下代码转换为def update(self): 然而,它似乎不能正常工作。旧图像worker.png不会消失

每次停止时,我都要更改对象
工作者的图像

Worker
是基于中的答案创建的

然后我假设有必要将以下代码添加到
def\uuuu init\uuu
中:

images = ["worker.png", "worker_stopped.png"]
for i in range(0,len(images)):
   self.images[i] = get_image(images[i])
并将以下代码转换为
def update(self)


然而,它似乎不能正常工作。旧图像
worker.png
不会消失,整个动画会被锁定。

我认为应该引入某种状态来指示worker是否正在运行。这里有一个例子。请注意以下评论:

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())

伟大的几个问题:1)为什么在这里使用
30
if random.randint(0,self.ticks处于\u状态)>30
?。2) 如果是随机的,为什么在
中使用
(0,5)
而不是
(0,4)
。只要在这些数字和条件下玩一玩,直到你得到你喜欢的行为(什么时候改变方向,什么时候站着等等)。我不知道你对工人的最终目标是什么;也许你想要一些简单的人工智能?好的,谢谢。我明白了。不,现在我不需要人工智能,只是随机行走。
images = ["worker.png", "worker_stopped.png"]
for i in range(0,len(images)):
   self.images[i] = get_image(images[i])
if self.direction.length() == 0:
    self.image = self.images[1]
else:
    self.image = self.images[0]
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())