Skip to content

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

Open
@elliot-100

Description

@elliot-100

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))

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions