pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
939 stars 155 forks source link

Implementation of ``pygame.(F)Rect.rel_center`` #3089

Open bilhox opened 2 months ago

bilhox commented 2 months ago

Implementation of #1242 according to MyreMylar name proposal.

While I'm bringing this feature to a potential release, what's your opinion on rel_centerx and rel_centery ?

Closes #1242 .

Tests + documentation added.

damusss commented 2 months ago

I'm not sure about the x and y variants, as (r.w/2+N, r.h/2+M) is shorter than (r.rel_centerx+N, r.rel_centery+M) (where N and M are arbitrary numbers not to make the expression useless), but I like rel_center, it makes sense.

aatle commented 2 months ago

rel_centerx and rel_centery are probably less readable than width/2, and less useful compared to centerx.

It seems to me like most uses would be related to Surfaces. I think more concrete use cases for this feature need to be listed.

bilhox commented 2 months ago

rel_centerx and rel_centery are probably less readable than width/2, and less useful compared to centerx.

It seems to me like most uses would be related to Surfaces. I think more concrete use cases for this feature need to be listed.

With the argument you gave with damus, indeed rel_centerx and rel_centery is not needed. Except that, I think the person who opened the issue gave a fairly good usecase, and I personally support the feature for the same reason.

aatle commented 2 months ago

I agree that the feature is logical and should probably be implemented, but I wonder if there are more use cases than the one given:

I often find myself evaluating (rect.w / 2, rect.h / 2) to find the middle of a rect, relative to itself. This is useful when drawing a circle onto a surface for a sprite, for example.

If I understand this case correctly, one could use surface.get_rect().center instead, right? For rects from surfaces, the position is (0,0) since only size is relevant, so rel_center is not needed. I personally don't see any use cases in my own project codebase, but that's probably because I use Vector2 for this stuff.