“Of course bad code can be cleaned up. But it’s very expensive.”
Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
Related Stack Overflow questions:
Related Stack Overflow questions:
The method pygame.Rect.move_ip
doesn’t return any value. It modifies the pygame.Rect
object itself.
So after
textrect = textrect.move_ip(-50, -50)
the value of textrect
is None
.
Related Stack Overflow questions:
Related Stack Overflow questions:
Pygame doesn’t let me use float for rect.move, but I need it
How to draw a moving circle in Pygame with a small angle at a low speed and blinking?
Ship moves up and left faster than down and right when rotating in pygame
Since pygame.Rect
is supposed to represent an area on the screen, a pygame.Rect
object can only store integral data.
The coordinates for Rect objects are all integers. […]
The fraction part of the coordinates gets lost when the new position of the object is assigned to the Rect object. If this is done every frame, the position error will accumulate over time.
If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect
object. round
the coordinates and assign it to the location (e.g. .topleft
) of the rectangle:
x, y = # floating point coordinates
rect.topleft = round(x), round(y)
Related Stack Overflow questions:
How do I get the size (width x height) of my pygame window How do I get the size (width x height) of my pygame window
If you only want to limit the player to a bounding rectangle, I recommend using pygame.Rect.clamp_ip
moves the rectangle inside another, in place
Define the bounding respectively border rectangle. If the player is limited to the screen, you can get a rectangle that defines the border from the display Surface with pygame.display.get_surface()
and pygame.Surface.get_rect
:
border_rect = pygame.display.get_surface().get_rect()
Clamp the player pygame.Rect
by border_rect
:
self.rect.clamp_ip(border_rect)
You can do this with 1 line of code:
self.rect.clamp_ip(pygame.display.get_surface().get_rect())
Use a pygame.Rect
object to limit the a circle to the bounds of the window. In the following window
is the display Surface :
radius = 16
clampRect = window.get_rect().inflate(-radius*2, -radius*2)
circleX = max(clampRect.left, min(clampRect.right, circleX))
circleY = max(clampRect.top, min(clampRect.bottom, circleY))
Explanation:
get_rect()
generates a pygame.Rect
with the size oft the pygame.Surface
which is associated to the display. inflate()
generates a new rectangle with the size changed by the diameter oft the circle, where the rectangle remains centered around its current center.
In the following, min
and max
are used to clamp the center of the circle in the area defined by the rectangle.
📁 minimal example - Move object with keys limit it to the window borders
Related Stack Overflow questions:
How do I center objects so the rectangle is around my character?
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions: