-
pygame.geometry
Warning
Experimental Module
This module is a work in progress. Refrain from relying on any features provided by this module, as they are subject to change or removal without prior notice.
— pygame object for representing a circle pygame module for the Circle, Line, and Polygon objects- pygame.Circle¶
- pygame object for representing a circleCircle((x, y), radius) -> CircleCircle(x, y, radius) -> Circle
— center x coordinate of the circle — center y coordinate of the circle — radius of the circle — radius of the circle squared — x and y coordinates of the center of the circle — diameter of the circle — area of the circle — circumference of the circle — test if a point is inside the circle — test if two circles collide — checks if a rectangle intersects the circle — updates the circle position and radius — returns a copy of the circle The Circle class provides many useful methods for collision / transform and intersection. A Circle can be created from a combination of a pair of coordinates that represent the center of the circle and a radius. Circles can also be created from python objects that are already a Circle or have an attribute named "circle".
Specifically, to construct a circle you can pass the x, y, and radius values as separate arguments or inside a sequence(list or tuple).
Functions that require a Circle argument may also accept these values as Circles:
((x, y), radius) (x, y, radius)
It is important to note that you cannot create degenerate circles, which are circles with a radius of 0 or less. If you try to create such a circle, the Circle object will not be created and an error will be raised. This is because a circle with a radius of 0 or less is not a valid geometric object.
The Circle class has both virtual and non-virtual attributes. Non-virtual attributes are attributes that are stored in the Circle object itself. Virtual attributes are the result of calculations that utilize the Circle's non-virtual attributes.
Here is the list of all the attributes and methods of the Circle class:
Circle Attributes
- x¶
- center x coordinate of the circlex -> float
The x coordinate of the center of the circle. It can be reassigned to move the circle. Reassigning the x attribute will move the circle to the new x coordinate. The y and r attributes will not be affected.
- y¶
- center y coordinate of the circley -> float
The y coordinate of the center of the circle. It can be reassigned to move the circle. Reassigning the y attribute will move the circle to the new y coordinate. The x and r attributes will not be affected.
- r¶
- radius of the circler -> float
It is not possible to set the radius to a negative value. It can be reassigned. If reassigned it will only change the radius of the circle. The circle will not be moved from its original position.
- r_sqr¶
- radius of the circle squaredr_sqr -> float
It's equivalent to r*r. It can be reassigned. If reassigned, the radius of the circle will be changed to the square root of the new value. The circle will not be moved from its original position.
- center¶
- x and y coordinates of the center of the circlecenter -> (float, float)
It's a tuple containing the x and y coordinates that represent the center of the circle. It can be reassigned. If reassigned, the circle will be moved to the new position. The radius will not be affected.
- diameter¶
- diameter of the circlediameter -> float
It's calculated using the d=2*r formula. It can be reassigned. If reassigned the radius will be changed to half the diameter. The circle will not be moved from its original position.
- area¶
- area of the circlearea -> float
It's calculated using the area=pi*r*r formula. It can be reassigned. If reassigned the circle radius will be changed to produce a circle with matching area. The circle will not be moved from its original position.
- circumference¶
- circumference of the circlecircumference -> float
It's calculated using the circumference=2*pi*r formula. It can be reassigned. If reassigned the circle radius will be changed to produce a circle with matching circumference. The circle will not be moved from its original position.
Circle Methods
- collidepoint()¶
- test if a point is inside the circlecollidepoint((x, y)) -> boolcollidepoint(x, y) -> boolcollidepoint(Vector2) -> bool
The collidepoint method tests whether a given point is inside the Circle (including the edge of the Circle). It takes a tuple of (x, y) coordinates, two separate x and y coordinates, or a Vector2 object as its argument, and returns True if the point is inside the Circle, False otherwise.
- collidecircle()¶
- test if two circles collidecollidecircle(Circle) -> boolcollidecircle(x, y, radius) -> boolcollidecircle((x, y), radius) -> bool
The collidecircle method tests whether two Circle objects overlap. It takes either a Circle object, a tuple of (x, y) coordinates and a radius, or separate x and y coordinates and a radius as its argument, and returns True if any portion of the two Circle objects overlap, False otherwise.
Note
If this method is called with a Circle object that is the same as the Circle it is called on, it will always return True.
- colliderect()¶
- checks if a rectangle intersects the circlecolliderect(Rect) -> boolcolliderect((x, y, width, height)) -> boolcolliderect(x, y, width, height) -> boolcolliderect((x, y), (width, height)) -> bool
The colliderect method tests whether a given rectangle intersects the Circle. It takes either a Rect object, a tuple of (x, y, width, height) coordinates, or separate x, y coordinates and width, height as its argument. Returns True if any portion of the rectangle overlaps with the Circle, False otherwise.
- update()¶
- updates the circle position and radiusupdate((x, y), radius) -> Noneupdate(x, y, radius) -> None
The update method allows you to set the position and radius of a Circle object in place. This method takes either a tuple of (x, y) coordinates, two separate x and y coordinates, and a radius as its arguments, and it always returns None.
Note
This method is equivalent(behaviour wise) to the following code:
circle.x = x circle.y = y circle.r = radius
- copy()¶
- returns a copy of the circlecopy() -> Circle
The copy method returns a new Circle object having the same position and radius as the original Circle object. The function takes no arguments and returns the new Circle object.
Edit on GitHub