How to move multiple grouped Sprite images simultaneously in Pygame Sprite
Question:
I am making a game with groups of birds, when I press a key I want all the birds to move at the same time but only one is moving.
There are five birds that I create with a loop, but only one of them is moving.
How can I move all of them at the same time
class Birds(pygame.sprite.Sprite):
def __init__(self, image, width, height, pos_x, pos_y):
super().__init__()
self.width = width
self.height = height
self.pos_x = pos_x
self.pos_y = pos_y
self.image = pygame.image.load(
os.path.join("Assets", image)).convert_alpha()
self.image = pygame.transform.scale(self.image, (80, 70))
self.rect = self.image.get_rect()
self.rect.center = [self.pos_x, self.pos_y]
def movement(self):
keys = pygame.key.get_pressed()
if(keys[pygame.K_u]):
if self.pos_x > 400 or self.pos_x < 0:
pos_x, pos_y = self.rect.center
pos_x -= 5
self.rect.center = (pos_x, pos_y)
birds_sprite = pygame.sprite.Group()
for target in range(5):
new_target = Birds("birds.png", 100, 100,
random.randrange(0, BACKGROUND_WIDTH),
random.randrange(0, BACKGROUND_HEIGHT // 3), 3, 3)
birds_sprite.add(new_target)
pygame.init()
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
picture = pygame.transform.scale(BACKGROUND_IMAGE, (1000, 540))
pygame.display.flip()
# gunhunter.shoot()
WIN.blit(picture, (0, 0))
birds_sprite.draw(WIN)
new_target.movement()
birds_sprite.update()
clock.tick(60)
pygame.quit()
Answers:
You have to call the method movement
in a loop for each object in the group birds_sprite
:
while run:
# [...]
for target in birds_sprite:
target.movement()
# [...]
Alter natively you can implement an update
method. Just rename movement
to update
. See pygame.sprite.Group.update
:
Calls the update()
method on all Sprites in the Group.
class Birds(pygame.sprite.Sprite):
# [...]
def update(self):
# [...]
while run:
# [...]
birds_sprite.update()
# [...]
``ยด
Thank you everyone, but i have managed to make it work.
enemyList = []
for target in range(5):
new_target = Birds("birds.png", 100, 100,
random.randrange(0, BACKGROUND_WIDTH), random.randrange(0, BACKGROUND_HEIGHT // 3), 2, 4)
enemyList.append(new_target)
birds_sprite.add(new_target)
then a function
def movement(self, thegroup):
for i in thegroup:
pos_x, pos_y = i.rect.center
pos_x += i.speed_x
pos_y += i.speed_y
i.rect.center = (pos_x, pos_y)
if(pos_x >= 900 or pos_x < 0):
i.speed_x = - i.speed_x
print(pos_x)
if(pos_y >= 300 or pos_y < 0):
i.speed_y = - i.speed_y
print(pos_y)
Then inside while loop
new_target.movement(enemyList)
I am making a game with groups of birds, when I press a key I want all the birds to move at the same time but only one is moving.
There are five birds that I create with a loop, but only one of them is moving.
How can I move all of them at the same time
class Birds(pygame.sprite.Sprite):
def __init__(self, image, width, height, pos_x, pos_y):
super().__init__()
self.width = width
self.height = height
self.pos_x = pos_x
self.pos_y = pos_y
self.image = pygame.image.load(
os.path.join("Assets", image)).convert_alpha()
self.image = pygame.transform.scale(self.image, (80, 70))
self.rect = self.image.get_rect()
self.rect.center = [self.pos_x, self.pos_y]
def movement(self):
keys = pygame.key.get_pressed()
if(keys[pygame.K_u]):
if self.pos_x > 400 or self.pos_x < 0:
pos_x, pos_y = self.rect.center
pos_x -= 5
self.rect.center = (pos_x, pos_y)
birds_sprite = pygame.sprite.Group()
for target in range(5):
new_target = Birds("birds.png", 100, 100,
random.randrange(0, BACKGROUND_WIDTH),
random.randrange(0, BACKGROUND_HEIGHT // 3), 3, 3)
birds_sprite.add(new_target)
pygame.init()
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
picture = pygame.transform.scale(BACKGROUND_IMAGE, (1000, 540))
pygame.display.flip()
# gunhunter.shoot()
WIN.blit(picture, (0, 0))
birds_sprite.draw(WIN)
new_target.movement()
birds_sprite.update()
clock.tick(60)
pygame.quit()
You have to call the method movement
in a loop for each object in the group birds_sprite
:
while run:
# [...]
for target in birds_sprite:
target.movement()
# [...]
Alter natively you can implement an update
method. Just rename movement
to update
. See pygame.sprite.Group.update
:
Calls the
update()
method on all Sprites in the Group.
class Birds(pygame.sprite.Sprite):
# [...]
def update(self):
# [...]
while run:
# [...]
birds_sprite.update()
# [...]
``ยด
Thank you everyone, but i have managed to make it work.
enemyList = []
for target in range(5):
new_target = Birds("birds.png", 100, 100,
random.randrange(0, BACKGROUND_WIDTH), random.randrange(0, BACKGROUND_HEIGHT // 3), 2, 4)
enemyList.append(new_target)
birds_sprite.add(new_target)
then a function
def movement(self, thegroup):
for i in thegroup:
pos_x, pos_y = i.rect.center
pos_x += i.speed_x
pos_y += i.speed_y
i.rect.center = (pos_x, pos_y)
if(pos_x >= 900 or pos_x < 0):
i.speed_x = - i.speed_x
print(pos_x)
if(pos_y >= 300 or pos_y < 0):
i.speed_y = - i.speed_y
print(pos_y)
Then inside while loop
new_target.movement(enemyList)