【问题标题】:How to make a pygame sprite group move?如何使 pygame 精灵组移动?
【发布时间】:2016-05-29 06:05:53
【问题描述】:

我为我的敌人创建了一个 pygame.sprite.group。 我可以将它们显示在屏幕上,但我不知道如何让它们全部移动? 我想让他们在平台上来回踱步(比如不断地左右踱步)。 我知道如何处理一个精灵的动作,但不知道如何处理一组精灵的动作。

这是我现在拥有的:

class Platform(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('levoneplatform.png')
        self.rect = self.image.get_rect()

class Enemy(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('enemy.png')
        self.rect = self.image.get_rect()

class LevOne():
    def __init__(self):

        self.background_image = pygame.image.load('night.png').convert_alpha()


        platforms_one = [ (200,300),
                        (50,500),
                        (550,650),
                        (300,200),
                        (120,100)
                   ]
        for k,v in platforms_one:
            platform = Platform()
            enemy = Enemy()
            platform.rect.x = k
            enemy.rect.x = k
            platform.rect.y = v
            enemy.rect.y = v - 44
            platform_list.add(platform)
            enemy_list.add(enemy)

    def update(self):
         screen.blit(self.background_image, [0, 0])

screen = pygame.display.set_mode((800,600))
enemy_list = pygame.sprite.Group()
platform_list = pygame.sprite.Group()

其余的基本上就像我的状态变化和更新。 我不知道如何移动整个精灵组。我知道如何移动一个精灵,但不是一个列表中的一堆精灵。

【问题讨论】:

    标签: python pygame sprite move


    【解决方案1】:

    我将为您的 Enemy 类引入 2 个新属性:dxplatformdx 是 x 方向的当前速度,platform 是敌人所在的平台。您必须将平台作为参数传递给您的敌人对象,这应该不是问题(只需在创建平台和敌人时将其传递)。你的 Enemy 类现在看起来像这样:

    class Enemy(pygame.sprite.Sprite):
    
        def __init__(self, platform):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('enemy.png')
            self.rect = self.image.get_rect()
            self.dx = 1  # Or whatever speed you want you enemies to walk in.
            self.platform = platform
    

    由于你的敌人继承了 pygame.sprite.Sprite,你可以用这样的东西覆盖 update() 方法:

    class Enemy(pygame.sprite.Sprite):
    
        def __init__(self, platform):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('enemy.png')
            self.rect = self.image.get_rect()
            self.dx = 1  # Or whatever speed you want you enemies to walk in.
            self.platform = platform
    
        def update(self):
            # Makes the enemy move in the x direction.
            self.rect.move(self.dx, 0)
    
            # If the enemy is outside of the platform, make it go the other way.
            if self.rect.left > self.platform.rect.right or self.rect.right < self.platform.rect.left:
                    self.dx *= -1
    

    你现在可以做的就是在你的游戏循环中调用精灵组enemy_list.update(),它会调用你所有敌人的更新方法,让他们移动。

    我不知道您项目的其余部分如何,但考虑到您提供的代码,这应该可以工作。你以后可以做的是在更新方法中传递时间,这样你的敌人就不会根据 fps 而是根据时间移动。 pygame.sprite.Group.update() 方法接受任何参数,只要该组中的所有对象都具有具有相同参数的更新方法。

    有关更多信息,请查看此talk 或阅读pygame docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多