Recently I started to learn Python, I learned the syntax, now I understand Pygame. I didn’t really understand about the Group class from the pygame.sprite module.

Suppose there is a class Bullet , in which there is a method update() , and I created a group bullets based on the class Group ( bullets = Group() ). I read that the Group class has an update() method that calls the update() method from the Bullet class (if we create a group to work with the Bullet class) for each sprite in the bullets group.

So, I wanted to ask if we have several imported modules, each of which has a class in which there is an update() method, then how to understand that the bullets.update() command will call the update() method from the class we need ?

I hope that you will understand what I wanted to say. Thank you in advance.

    1 answer 1

    The group you have created is empty, and even more so it knows nothing about what objects of type Bullet are. And do not confuse, the Group.update() method calls Sprite.update() on all objects in the group , it does not know about any classes.

     # создали группу bullets = Group() # создали экземпляры спрайтов bullet1 = Bullet(*параметры*) bullet2 = Bullet(*параметры*) bullet3 = Bullet(*параметры*) # поместили нужные спрайты 1 и 2 в группу bullets.add(bullet1, bullet2) # в игровом цикле вызываем update у нашей группы # update() будет вызван только у bullet1 и bullet2 # которые мы вручную и добавили в группу bullets.update() 

    The bullet3 object bullet3 not added to the group, and therefore the update group would not affect it.

    • understood, thank you, but, nevertheless, I did not understand, and where is the Sprite.update () method to ask?) - Ilya Romanov
    • I create the Bullet class based on the Sprite class, in the Bullet class I write the update () method, but this method is not counted as Sprite.update (), right? - Ilya Romanov
    • @ IlyaRomanov, why he does not count? You overload Sprite.update() during inheritance, so Bullet.update() will be called in the group. - RiotBr3aker 2:46 pm
    • Thank you for answering me)) Spite.update () means that we call the update () method from a class that inherits from the Sprite class? Sorry, honestly, I don’t really understand it) I also wrote that if there are several classes that are based on the Sprite class and there is an update () method in each of these classes, then how to understand that the Group.update method ( ) will cause what we want? - Ilya Romanov
    • @ IlyaRomanov, when you inherit from the Sprite class and overload the Sprite.update() method, the method overloaded by you will always be called. It is rather difficult to explain this concept in the comments (it’s better to refer to Google on the topic of inheritance and calling overloaded methods), in short, this is done so that you can store not only Bullet objects in the group, but also any other objects whose types are inherited from Sprite class. And at the same time, all of them, regardless of their type, will be called update() . - RiotBr3aker pm