I recommend to 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.