Search code examples
pythonspritepygamecollisionrect

Implementation of collide method by creating a rectangle that is smaller than the object


How would I create a rectangle that is smaller than the image? As far as I know, the only way to create a rect around the image is by using:

self.rect = self.image.get_rect()

But that creates a rect that is larger than the image. Thanks.


Solution

  • A "Rectangle" in a Pygame is a simple object to pass coordinates on surfaces between functions and methods. There is the pygame.rect.Rect class, to which you pass (x, y, width, height) to instantiate. Aside from that specific class, almost all functions in pygame that require a rectangle, will also accept an ordinary tuple of (x,y, width, height) - including collision verifications.