W
W
Waffle472022-02-22 15:14:40
Python
Waffle47, 2022-02-22 15:14:40

Groups do not work correctly in pygame. How to fix the code so that the action happens with a certain item in the group?

I threw off all the working files in this link:
https://drive.google.com/drive/folders/10l6UgTeIUm...
In general. A bullet is created after pressing space (available in controls.py), then if it goes beyond the screen, it is deleted, and if it hits, an explosion animation is created and after the animation it disappears. The problem is that at first my first bullets were made by an explosion, now the last ones. You can try to play it yourself. It's just that the wrong bullet explodes. Added a lot of comments and should be more or less clear. The most important issues in controls.py a lot of things are processed there, and the bullets themselves, which change self.live to False and change their picture.
Please help!!! Help with a bug!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YariKartoshe4ka, 2022-02-22
@Waffle47

Look for the collision
in controls.py incorrectly:

for bullet in bullets.copy(): 
    if bullet.rect.left >= 1920: #удаление при выходе за экран
        bullets.remove(bullet)
    if pygame.sprite.groupcollide(bullets, enemys, False, False) and bullet.live == True: #создание взрыва после попадания
        pygame.sprite.groupcollide(bullets, enemys, False, True)
        bullet.animCount = 0
        bullet.live = False

Since you yourself are iterating over the bullets, you should use not groupcollide, but spritecollideany (where the sprite is the bullet to be iterated, and the group is the enemies)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question