我怎样才能使四个精灵循环,使其表现得像一个 GIF 一样充当一个精灵?

How can I make four sprites looped so it behaves like a GIF act as ONE sprite?

所以我有四个精灵,我在绘画中编辑过它们,所以当循环遍历它们时,它看起来像一个 GIF。我怎样才能使这 4 个精灵不断循环,使它们看起来像一个 GIF,表现得像一个精灵?

怎样才能让四个精灵在按左右键时都像一个精灵一样左右移动?

这是 "GIF" 的代码:

import pygame
from pygame.sprite import Sprite
import sys

pygame.init()


class Flame(Sprite):

    def __init__(self, images, time_interval):
        super().__init__()
        self.image_1 = pygame.image.load('images/a.bmp')
        self.image_2 = pygame.image.load('images/b.bmp')
        self.image_3 = pygame.image.load('images/c.bmp')
        self.image_4 = pygame.image.load('images/d.bmp')

        self.images = [self.image_1, self.image_2, self.image_3, self.image_4]
        self.image = self.images[0]
        self.time_interval = time_interval
        self.index = 0
        self.timer = 0

    def update(self, seconds):
        self.timer += seconds
        if self.timer >= self.time_interval:
            self.image = self.images[self.index]
            self.index = (self.index + 1) % len(self.images)
            self.timer = 0


def create_images():
    images = []
    for i in range(4):
        image = pygame.Surface((256, 256))
        images.append(image)
    return images


def main():
    images = create_images()
    a = Flame(images, 0.25)

    # Main loop.
    while True:
        seconds = clock.tick(FPS) / 500.0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        a.update(seconds)
        screen.fill((230, 230, 230))
        screen.blit(a.image, (250, 100))
        pygame.display.update()


if __name__ == '__main__':
    screen = pygame.display.set_mode((720, 480))
    clock = pygame.time.Clock()
    FPS = 60
    main()

如何让这些精灵在按左时向左循环,按右时向右循环?

嗨,我创建了这个函数来帮助我的 pygame 动画

def animate(animation_tick, animation_speed, animation_counter, animations, pause=False):

# increment the tick so animations happen at certain points - this the same as the game tick
animation_tick += 1
# change the image to the next in the list
if animation_tick % animation_speed == 0:
    if not pause:
        animation_counter += 1
    # animation list has finished, reset it so it starts again.

    if animation_counter > len(animations) - 1:
        animation_counter = 0
        animation_tick = 0
return animation_counter, animation_tick, animations[animation_counter]

然后在我的敌人中调用那个函数 class

def animate(self, animations, flip = None):
    """
    :param animations:
    :param animation_state:
    :return:
    """

    self.moving_animation_counter, self.animation_tick, image = \
        animate(self.animation_tick, self.animation_speed, self.moving_animation_counter, animations)
    if flip:
        image = pygame.transform.flip(image, True, False)
    return pygame.transform.scale(image, (self.rect.width, self.rect.height))

改变blit的位置即可a.image

您硬编码 (250, 100) 并使用 screen.blit 在屏幕上绘制精灵,但通常,当使用 pygame 时,您应该使用 Group 代替。

A Sprite 应该有一个包含其大小和位置的 rect 属性。因此,要在按下某个键时实际更改 Sprite 的位置,您需要更改 Rect.

的 x/y-attributes

这是一个简单的例子:

import pygame
from pygame.sprite import Sprite
import sys

pygame.init()


class Flame(Sprite):

    def __init__(self, time_interval):
        super().__init__()
        self.image_1 = pygame.image.load('images/a.bmp')
        self.image_2 = pygame.image.load('images/b.bmp')
        self.image_3 = pygame.image.load('images/c.bmp')
        self.image_4 = pygame.image.load('images/d.bmp')

        self.images = [self.image_1, self.image_2, self.image_3, self.image_4]
        self.image = self.images[0]
        self.time_interval = time_interval
        self.index = 0
        self.timer = 0
        self.rect = self.image.get_rect(topleft=(250, 100))

    def update(self, seconds):
        self.timer += seconds
        if self.timer >= self.time_interval:
            self.image = self.images[self.index]
            self.index = (self.index + 1) % len(self.images)
            self.timer = 0
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_LEFT]: self.rect.move_ip((-1, 0))
        if pressed[pygame.K_RIGHT]: self.rect.move_ip((1, 0))

def main():
    sprites = pygame.sprite.Group(Flame(0.25))
    # Main loop.
    while True:
        seconds = clock.tick(FPS) / 500.0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        sprites.update(seconds)
        screen.fill((230, 230, 230))
        sprites.draw(screen)
        pygame.display.update()


if __name__ == '__main__':
    screen = pygame.display.set_mode((720, 480))
    clock = pygame.time.Clock()
    FPS = 60
    main()