Description
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 arel_center
attribute topygame.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 acenter
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