0

I'm learning from a book by Eric Mattis. I noticed that at one point, when I change the name of the method, I get an error.

Here is an example (I removed everything unnecessary so that it does not distract):

# Importing the Group class
from pygame.sprite import Group

# create an instance of the Group class
something = group()

# main loop
while True:

    something.update()


# Another class description file and its method

class Some_Class(Sprite):

    def __init__(self):
        super().__init__()

    def update(self):

        something is happening

Not a word was said about this in the book, and at first I thought that the name update was chosen for ease of writing, because it is clear that something is being updated here and there is no need to write another name. I used to change the names of the methods at my own discretion in order to better understand what is happening here, for example, not just def update() , but def screen_update() or def position_update()

But this time I ran into an error and tried to figure out what was wrong for a long time, until I finally realized that it was impossible to change the name update to something else.

Do I understand correctly that in this case this is not just a random name for the method, but some reserved name in pygame for Group() objects?

And what you need to remember is as a rule: if I import from pygame.sprite import Group and I create an instance of the Group() class and then access methods based on that class, then I have to use only the method in the main loop update() ?

I just want to understand why this is so and not otherwise...

If this is a built-in method, then why do I create a def update() method in the class file, because here I choose the name myself? What's the point? Or do I just refer to this method and say what it will do?

And if I create a simple instance of a class without inheritance, then I can choose arbitrary names for methods, for example:

# Import class some_class
from some_class import Some_class

# create an instance of the class Some_class
some_class = Some_class()

# main loop
while True:

    some_class.some_method()

    # OR THIS:

    some_class.updating_something_because_it_needs_to_be_updated()


# Another class description file and its method

class Some_class():

    def __init__(self):

    def updating_something_because_it_needs_to_be_updated(self):

        something is happening
        ...
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • What is the full traceback of the error because `something = group()` should be `something = Group()` – It_is_Chris Feb 18 '22 at 17:39
  • You very correctly and well noticed my mistake with **`something = group()`**, but I wrote the text for the question without copying from the code file, so this is a simple typo when creating the question. But if anything, then the exact error when changing the name of the **`update`** method: **`'Group' object has no attribute 'any other method name'`** – Александэр Спанчев Feb 18 '22 at 19:41

2 Answers2

1

The method name must be update since this is a pygame feature. pygame.sprite.Group.update() is a methods which is provided by pygame.sprite.Group, it delegates to the update methods of the contained pygame.sprite.Sprites.

See pygame.sprite.Group.update():

Calls the update() method on all Sprites in the Group. [...]

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

Short Answer

Yes, the method always has to be called update() in the sprite class you create (Some_Class in your case). Also, you did it right to call the update() method of the Group in your main loop, but you have to ensure that your sprite is added to the group!

Longer Answer

What you can see in this picture is the Sprite class that's defined in the pygame library. When you write Some_Class(Sprite), then you use it as a base class and add additional features in the class you define on your own. You can also see in the picture that the Sprite class has an update function, which is empty by default. When writing your own update() function in Some_Class, you overwrite that method that it does what you want it to do.

When you ask "why is it important that this method has the name 'update'?", then take a look at the Group class from the pygame library (don't get bothered by the name 'AbstractGroup'). The Group.update() method calls the update() function from every sprite that is within the group. So if you don't name the method update() then it won't get called in the group.

In the end, you either follow the naming conventions to use the Group.update() in your main loop, or you create your own update functions and call them on your own. Below you can see a code example that contains both options.

import pygame


class Some_Class(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

    def update(self):
        # pygames update method, called by the Group.update()
        pass

    def my_own_update(self):
        # your own update method, that you have to call yourself
        pass


pygame.init()
window = pygame.display.set_mode((800, 800))

# Create sprite and group
sprite = Some_Class()
group = pygame.sprite.Group()

# Make the sprite a part of the group
group.add(sprite)

while True:

    # Either update the sprite through the group or use your own update method
    group.update()
    sprite.my_own_update()

    pygame.display.update()
Sven
  • 41
  • 3