【问题标题】:How can I make four sprites looped so it behaves like a GIF act as ONE sprite?我怎样才能让四个精灵循环起来,让它像一个 GIF 一样充当一个精灵?
【发布时间】:2019-06-03 12:22:19
【问题描述】:

所以我有四个我在绘画中编辑过的精灵,所以当循环它们时,它看起来像一个 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()

我怎样才能让这些精灵在按左时向左,按右时向左?

【问题讨论】:

    标签: python image pygame sprite


    【解决方案1】:

    只需改变你 blit a.image 的位置。

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

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

    这是一个简单的例子:

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

    【讨论】:

      【解决方案2】:

      嗨,我创建了这个函数来帮助我的 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))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多