pygame精灵组有哪些方法_如何使pygame精灵组移动?

在pygame中,可以通过创建精灵组并重写精灵的update方法来实现一组敌人的移动。为每个敌人添加dx属性表示移动速度,并在update方法中调整方向。在游戏循环中调用group.update()来更新所有敌人的位置。
摘要由CSDN通过智能技术生成

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.

Here is what I have right now:

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.

解决方案

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

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

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值