Asteck
Asteck

Reputation: 60

What kind of attributes can be used for: 'self.image.get_rect()'

I am trying to draw my sprites to a location where I have their top left x and y coordinates.

I know that center is an attribute that can be used

class walls(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        super().__init__()
        self.width  = width
        self.height = height
        self.x      = x
        self.y      = y

        self.image = pygame.Surface([width,height])
        self.image.fill(black)
        self.rect = self.image.get_rect(center=(self.x, self.y))

I do not want the center coordinates rather I would like to draw this image by its top left x and y coordinates. How? What sort of other commands can I use within the self.image.get_rect() function?

Upvotes: 0

Views: 2645

Answers (1)

Rabbid76
Rabbid76

Reputation: 211135

The .image attribute of a pygame.sprite.Sprite holds a pygame.Surface object.

See the documentation of pygame.Surface:

You can pass keyword argument values to this function. [...] An example would be mysurf.get_rect(center=(100,100)) to create a rectangle for the Surface centered at a given position.

This means it can be passed any keyword attributes to this function which can be set to pygame.Rect

e.g.

self.rect = self.image.get_rect(topleft=(100, 100))

generates a rectangle whit the top left coordinate (100, 100) and the width and height of self.image.

Upvotes: 2

Related Questions