Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rect.size_rect (2429) #1242

Open
GalacticEmperor1 opened this issue Feb 12, 2023 · 0 comments · May be fixed by #3089
Open

Rect.size_rect (2429) #1242

GalacticEmperor1 opened this issue Feb 12, 2023 · 0 comments · May be fixed by #3089
Labels

Comments

@GalacticEmperor1
Copy link
Collaborator

Issue №2429 opened by penn5 at 2020-12-29 17:43:26

Description

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. I think it would save quite a bit of time if I could simply do rect.size_rect.center and it's certainly more intuitive. I don't like the name size_rect but it's pretty descriptive of what the method does, and I couldn't come up with anything better.

Thanks for all your effort!


Comments

# # penn5 commented at 2020-12-29 18:38:22

If people think this is a good idea, I made a fork where I implemented it (it's pretty simple)


# # MyreMylar commented at 2020-12-30 08:31:05

Maybe just a rel_center attribute should be added to Rect. Should be fairly simple as it is just:
self.rel_center = (self.centerx - self.x, self.centery - self.y)


# # penn5 commented at 2020-12-30 09:34:18

I thought about that too, but I thought that having a full-blown rect available increases flexibility (as the client isn't limited to just the center, but anything attributes of rect, like for example midright


# # Mega-JC commented at 2020-12-30 15:00:20

What about doing Rect((0,0), my_rect.size).center?

That would already give you a full-blown rect without too much extra code.
I agree with adding a rel_center attribute to pygame.Rect though.


# # penn5 commented at 2020-12-30 15:30:32

What about doing Rect((0,0), my_rect.size).center?

That would already give you a full-blown rect without too much extra code.
I agree with adding a rel_center attribute to pygame.Rect though.

my_rect.base is much cleaner IMO, and it gives you autocomplete in any IDE with type annotations support. Also, typing too many brackets hurts my fingers :P


# # husano896 commented at 2021-01-05 07:52:17

pygame.Rect itself does have a center attribute already.

https://www.pygame.org/docs/ref/rect.html

rect = pygame.Rect((0,0), (640,480))
print(rect.center) #  (320, 240)

#  rel_center as MyreMylar said
rel_center = (rect.centerx - rect.x, rect.centery- rect.y)
print(rel_center) #  also (320, 240)

# # penn5 commented at 2021-01-05 09:22:59

pygame.Rect itself does have a center attribute already.

https://www.pygame.org/docs/ref/rect.html

rect = pygame.Rect((0,0), (640,480))
print(rect.center) #  (320, 240)

#  rel_center as MyreMylar said
rel_center = (rect.centerx - rect.x, rect.centery- rect.y)
print(rel_center) #  also (320, 240)

Yes, I know how to do this manually, the idea is that it would be more idiomatic and easier to type

@MyreMylar MyreMylar added the rect pygame.rect label Oct 7, 2023
@bilhox bilhox linked a pull request Aug 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants