Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: pygame.sprite.Sprite subclassing code example could be improved #3269

Open
elliot-100 opened this issue Dec 16, 2024 · 2 comments
Open
Labels

Comments

@elliot-100
Copy link

elliot-100 commented Dec 16, 2024

On reading https://pyga.me/docs/ref/sprite.html#pygame.sprite.Sprite:

When subclassing the Sprite, be sure to call the base initializer before adding the Sprite to Groups. For example:

I expected the immediately following example to include adding a Sprite to a Group, but it doesn't:

class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
       self.image = pygame.Surface([width, height])
       self.image.fill(color)

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       self.rect = self.image.get_rect()

I think it should add e.g.

  blocks = pygame.sprite.Group
  blocks.add(Block(pygame.Color('red'), 100, 50))
@damusss
Copy link
Member

damusss commented Dec 16, 2024

I don't think the documentation is wrong here. If you read it carefully it never says that code snippet is going to add the sprite to groups. It only says that it is a required step for the sprite to be allowed to be added to sprite groups.

@elliot-100
Copy link
Author

I'm not saying it's wrong, and I did read it carefully, several times.
I'm saying I found it unexpected that when reading the construction:

`When x, be sure to do y before z. For example...'
The immediately following example shows a case of x, does y, but not z.

And that I think it could be improved by showing z in the example.

I don't agree that 'be sure to' reads as it's a required step - it sounds more like a recommendation. Perhaps 'be sure to' could be strengthened to 'you must'?

Others may agree or disagree with my suggestion of course. But perhaps it would help discussion to give a reason not to add to the code example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants