如何使四个子画面循环播放,使其表现得像GIF一样充当一个子画面?

时间:2019-06-03 12:22:19

标签: python image pygame 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()

如何使这些循环的精灵在向左按下时向左移动而在向右按下时向右移动?

2 个答案:

答案 0 :(得分:1)

只需更改b a.image的位置即可。

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

Sprite的{​​{1}}属性应包含其大小和位置。因此,要在按下键时实际更改rect的位置,您需要更改该Sprite的x / y属性。

这是一个简单的例子:

Rect

答案 1 :(得分:0)

嗨,我创建了此功能来帮助我的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]

然后在我的敌人课堂中调用该函数

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