PyRect
PyRect is a simple module that provides a `Rect` class, mimicking the functionality of Pygame's `Rect` objects for handling rectangular areas. It is designed to be a standalone, lightweight solution for geometric operations. The current version is 0.2.0, released in March 2022, indicating an infrequent release cadence.
Warnings
- gotcha By default, Rect objects are locked to integer coordinates. Any float values assigned to position or size attributes will be truncated.
- gotcha Methods that modify the rectangle's position or size, like `move()` or `inflate()`, return a *new* Rect object with the changes, leaving the original object unchanged. This is consistent with Pygame's `Rect` behavior.
- gotcha When modifying a Rect's `width` or `height` (or `size`), the top-left corner (`topleft`) of the rectangle remains anchored. This means the rectangle expands or shrinks from its top-left point.
Install
-
pip install pyrect
Imports
- Rect
from pyrect import Rect
Quickstart
from pyrect import Rect
# Create a Rect object: x, y, width, height
r = Rect(10, 20, 100, 50)
print(f"Initial Rect: {r}")
print(f"Top-left: {r.topleft}, Width: {r.width}, Height: {r.height}")
print(f"Center: {r.center}")
# Move the Rect (returns a new Rect)
new_r = r.move(5, 10)
print(f"Moved Rect (new object): {new_r}")
print(f"Original Rect (unchanged): {r}")
# Modify in-place
r.x = 0
r.y = 0
print(f"Modified in-place: {r}")
# Enable float coordinates
r_float = Rect(0, 0, 10, 20, enableFloat=True)
r_float.width = 10.5
print(f"Rect with float enabled: {r_float.size}")