如何使pygame精灵组移动? [英] How to make a pygame sprite group move?

查看:544
本文介绍了如何使pygame精灵组移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为敌人制作了一个pygame.sprite.group. 我可以在屏幕上显示它们,但是我不知道如何使它们全部移动? 我想让它们在平台上来回移动(就像不断地左右移动). 我知道如何处理一个精灵的运动,而不是一群人的运动.

I made a pygame.sprite.group for my enemies. I can display them on the screen, but I don't know how to make them all move? I want to have them pacing back and forth on the platforms (like left and right constantly). I know how to work with the movements of one sprite, but not a bunch in a 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()

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

The rest is basically like my state changes and updates. I don't know how to move the entire sprite group. I know how to move one sprite, but not a bunch of sprites in one list.

推荐答案

我将为您的Enemy类引入2个新属性: dx platform . dx 是x方向上的当前速度, platform 是敌人所在的平台.您必须将平台作为参数传递给您的敌人对象,这应该不成问题(只需在创建平台和敌人时将其传递).您的敌人课程现在将如下所示:

I would introduce 2 new attributes to your Enemy class: dx and platform. dx being the current speed in the x direction and platform being the platform the enemy is on. You'll have to pass the platform as an argument to your enemy object, which shouldn't be a problem (just pass it in when creating the platform and the enemy). Your Enemy class will now look like this:

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()方法:

Since your enemies inherit pygame.sprite.Sprite you could overwrite the update() method with something like this:

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(),它将调用所有敌人的update方法,使它们移动.

What you can do now is just call the sprite group enemy_list.update() in your game loop which will call the update method of all your enemies, making them move.

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

I don't know how the rest of your project look like but this should work considering the code you've provided. What you could do later on is passing the time in the update method, making it so your enemies doesn't move based on the fps but rather by time. The pygame.sprite.Group.update() method takes any arguments as long as all objects in that group have an update method with the same arguments.

有关更多信息,请查看此对话或阅读

For more information, check out this talk or read the pygame docs.

这篇关于如何使pygame精灵组移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆