1
import pygame
from pygame.sprite import Sprite
from pygame.sprite import Group

class Raindrop(Sprite):

    def __init__(self, screen):
        super().__init__()
        ...
    ...
    def drop(self):
        ...
...
raindrops = Group()
...
raindrops.drop()
...

When I use this code, I end up with an error:

Traceback (most recent call last):
  File "raindrops.py", line 74, in <module>
    rain()
  File "raindrops.py", line 71, in rain
    update_raindrops.py(screen, raindrops)
  Dile "raindrops.py", line 57, in update_raindrops
   raindrops.drop()
AttributeError: 'Group' object has no attribute 'drop'

Why does this happen? When I was doing some very similar examples from a textbook, it would define a method for a subclass of Sprite, than make a Group list of sprites and then GroupList.ClassMethod() worked just fine. But not in this case, for some reason. I can provide the whole code, if you need.

  • that means you are *trying to access an attribute member of another class that doesn't exist at all*, look into the Group file for details on what you're trying to get out of there. – de_classified Mar 26 '20 at 15:20
  • Then why my textbook example was able to apply an arbitrary method in the very same way, but I can't? – Oleg Shevchenko Mar 26 '20 at 15:51
  • my hunch is that you have to inherit the `Group` class into the `class Raindrop(Sprite, Group):` but maybe wrong, what's the code like for `Group` – de_classified Mar 26 '20 at 16:10

1 Answers1

1

Your assertion is false - the PyGame Sprite Group does not apply arbitrary methods, just some well known pre-defined methods. Given your example code, I suspect you're thinking about the Sprite Group method update() (and perhaps draw()), which does call the update() method of every sprite in the group.

If your example was:

import pygame
from pygame.sprite import Sprite
from pygame.sprite import Group

class Raindrop(Sprite):

    def __init__(self, screen):
        super().__init__()
        ...
    ...
    def update(self):                        # <<-- HERE, not "drop()"
        # TODO: move/change sprite shomehow
        ...
...
raindrops = Group()
...
raindrops.update()
...

It would work fine.

Kingsley
  • 14,398
  • 5
  • 31
  • 53