Search code examples
imagepygamecollisionrect

Can Pygame images go beyond their Rects?


I'm making a game in Pygame, and I have it so that if the character collides with a rect that it will push the character back a layer of pixels, creating a very small impassable boundary between it and the object. I was wondering that when I started to add art to it if I could have it covered up so it looked like a solid collision, or is there any way how a solid collision could function without their being that layer of pixels? Thanks in advance!


Solution

  • Sure, just create a Rect that is a little bit bigger than the Rect of the Surface using inflate:

    Returns a new rectangle with the size changed by the given offset. The rectangle remains centered around its current center. Negative values will shrink the rectangle.

    s = your_surface # the Surface containing your image .
    r = s.get_rect() # the rect of the Surface.
    c_r = r.inflate(2, 2) # the rect used for collision detection.