0
\$\begingroup\$

I got used to get_rect() when detecting collisions in Pygame but I've just discovered that another method called get_bounding_rect() exists. So what are its uses ?

What is the difference between get_rect() and get_bounding_rect() methods in Pygame? Which one is better ?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

As the documentation for get_bounding_rect says, get_bounding_rect will check the pixels in the surface and discard pixels that have an alpha value equal or less to the passed in value.

Returns the smallest rectangular region that contains all the pixels in the surface that have an alpha value greater than or equal to the minimum alpha value.

So for an example, if your surface has 10 pixels of transparent padding around in all sides, the bounding rect will not contain that padding, but instead be a sub-rectangle starting from (10, 10) and being of size (width - 20, height - 20).

The documentation also mentions that the surface pixels will be locked and unlocked during a get_bounding_rect call, which most likely means that it will be quite a lot slower than calling get_rect. There's really no direct answer as to which one is better, use the one that suits your needs better. The bounding rectangle will perhaps be more accurate for collisions, but if your surfaces don't contain a lot of empty transparent space, it won't be a big difference.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you ! That's what I was looking for ! \$\endgroup\$
    – Salem
    Jun 20, 2021 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .