PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it

“Of course bad code can be cleaned up. But it’s very expensive.”
Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship


Rectangle

Rectangle attributes

Related Stack Overflow questions:

Move rectangle

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.

Change size of rectangle

Related Stack Overflow questions:

Floating point coordinates

Related Stack Overflow questions:

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)

Use a rectangle as a frame and limit the object to a rectangular area

Related Stack Overflow questions:

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

Center of a rectangular area

Related Stack Overflow questions:

Nested rectangles

Related Stack Overflow questions:

List of rectangles

Related Stack Overflow questions:

Touching rectangles

Related Stack Overflow questions:

Rotated rectangle

Related Stack Overflow questions: