pyxora.wrapper.shapes

  1from .functions import vector
  2from abc import ABC, abstractmethod
  3from math import ceil
  4from typing import Tuple
  5import pygame
  6
  7class Shape(ABC):
  8    """Abstract base class for all drawable shapes."""
  9
 10    def __init__(self, pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3, color: str | tuple):
 11        """
 12        Initializes the shape with a position and color.
 13
 14        Args:
 15            pos (pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the shape.
 16            color (str | tuple): The color of the shape, either as a string (e.g., "red") or a tuple (R, G, B).
 17        """
 18        self._pos = vector(*pos)
 19        self._color = color
 20
 21    @property
 22    def position(self) -> pygame.math.Vector2 | pygame.math.Vector3:
 23        """property to get a copy of the position of the shape."""
 24        return self._pos.copy()
 25
 26    @property
 27    def color(self) -> str | Tuple[int,int,int]:
 28        """property to get the color of the shape."""
 29        return self._color
 30
 31    @property
 32    @abstractmethod
 33    def rect(self) -> pygame.Rect | pygame.FRect:
 34        """
 35        Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.
 36
 37        Returns:
 38            pygame.Rect | pygame.FRect: The bounding rectangle of the shape.
 39        """
 40        pass
 41
 42    @abstractmethod
 43    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
 44        """
 45        Abstract method to draw the shape on a surface with a given fill and scale.
 46
 47        Args:
 48            surf (pygame.Surface): The surface to draw on.
 49            fill (int): The fill value for the shape (positive values for outline else is solid).
 50            scale (int | float): The scale factor for the shape size.
 51        """
 52        pass
 53
 54    def move(self,pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3) -> None:
 55        """
 56        Moves the shape by the given offset.
 57
 58        Args:
 59            pos (Tuple[int | float, int | float] | Vector2 | Vector3):
 60                The amount to move the shape by, relative to its current position.
 61        """
 62        self._pos.x += pos[0]
 63        self._pos.y += pos[1]
 64
 65    def move_at(self,pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3) -> None:
 66        """
 67        Moves the shape to a position.
 68
 69        Args:
 70            pos (Tuple[int | float, int | float] | Vector2 | Vector3):
 71                The new position for the shape.
 72        """
 73        self._pos.x = pos[0]
 74        self._pos.y = pos[1]
 75
 76
 77class Rect(Shape):
 78    """Represents a rectangle shape."""
 79
 80    def __init__(
 81            self,
 82            pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
 83            size: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
 84            color: str | tuple
 85    ):
 86        """
 87        Initializes the rectangle with position, size, and color.
 88
 89        Args:
 90            pos (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the rectangle.
 91            size (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The size of the rectangle (width, height).
 92            color (str | tuple): The color of the rectangle, either as a string (e.g., "red") or a tuple (R, G, B).
 93        """
 94        super().__init__(pos, color)
 95        self._size = tuple(size)
 96
 97    @property
 98    def size(self) -> Tuple[int | float, int | float]:
 99        """ The size of the Rect"""
100        return self._size
101
102    @property
103    def rect(self) -> pygame.Rect | pygame.FRect:
104        """
105        Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.
106
107        Returns:
108            pygame.Rect | pygame.FRect: The bounding rectangle of the shape.
109        """
110        if all(isinstance(i, int) for i in (self._pos[0], self._pos[1], self._size[0], self._size[1])):
111            return pygame.Rect(self._pos, self._size)  # Use pygame.Rect if all values are integers
112        return pygame.FRect(self._pos, self._size)  # Use pygame.FRect otherwise
113
114    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
115        """
116        Draws the rectangle on the surface with a given fill and scale.
117
118        Args:
119            surf (pygame.Surface): The surface to draw on.
120            fill (int): The fill value for the shape (positive values for outline else is solid).
121            scale (int | float): The scale factor for the rectangle size.
122        """
123        # Scale the rectangle and fill value
124        rect = self.rect
125        color = self.color
126        fill *= scale
127        rect.width *= scale
128        rect.height *= scale
129        fill = ceil(fill)  # Ensure fill is an integer
130
131        # Draw the rectangle
132        pygame.draw.rect(surf, color, rect, width=fill if fill > 0 else 0)
133
134
135class Circle(Shape):
136    """Represents a circle shape."""
137
138    def __init__(self, pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3, radius: int | float, color: str | tuple):
139        """
140        Initializes the circle with position, radius, and color.
141
142        Args:
143            pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
144            radius (int | float): The radius of the circle.
145            color (str | tuple): The color of the circle, either as a string (e.g., "red") or a tuple (R, G, B).
146        """
147        super().__init__(pos, color)
148        self._radius = radius
149
150    @property
151    def radius(self) -> int | float:
152        """ The radius of the Circle"""
153        return self._radius
154
155    @property
156    def rect(self) -> pygame.Rect:
157        """
158        Returns the bounding rectangle for the circle.
159
160        Returns:
161            pygame.Rect: The bounding rectangle that encloses the circle.
162        """
163        pos = (self._pos[0] - self.radius, self._pos[1] - self.radius)  # Top-left corner of the bounding rect
164        size = (self.radius * 2, self.radius * 2)  # Size of the bounding rectangle
165        return pygame.Rect(pos, size)
166
167    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
168        """
169        Draws the circle on the surface with a given fill and scale.
170
171        Args:
172            surf (pygame.Surface): The surface to draw on.
173            fill (int): The fill value for the circle outline (negative for outline, positive for solid).
174            scale (int | float): The scale factor for the circle size.
175        """
176        # Scale the circle and fill value
177        pos = self._pos
178        fill *= scale
179        radius = self.radius * scale  # Scale the radius
180        fill = ceil(fill)  # Ensure fill is an integer
181
182        # Draw the circle
183        pygame.draw.circle(surf, self.color, pos, radius, width=fill if fill > 0 else 0)
class Shape(abc.ABC):
 8class Shape(ABC):
 9    """Abstract base class for all drawable shapes."""
10
11    def __init__(self, pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3, color: str | tuple):
12        """
13        Initializes the shape with a position and color.
14
15        Args:
16            pos (pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the shape.
17            color (str | tuple): The color of the shape, either as a string (e.g., "red") or a tuple (R, G, B).
18        """
19        self._pos = vector(*pos)
20        self._color = color
21
22    @property
23    def position(self) -> pygame.math.Vector2 | pygame.math.Vector3:
24        """property to get a copy of the position of the shape."""
25        return self._pos.copy()
26
27    @property
28    def color(self) -> str | Tuple[int,int,int]:
29        """property to get the color of the shape."""
30        return self._color
31
32    @property
33    @abstractmethod
34    def rect(self) -> pygame.Rect | pygame.FRect:
35        """
36        Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.
37
38        Returns:
39            pygame.Rect | pygame.FRect: The bounding rectangle of the shape.
40        """
41        pass
42
43    @abstractmethod
44    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
45        """
46        Abstract method to draw the shape on a surface with a given fill and scale.
47
48        Args:
49            surf (pygame.Surface): The surface to draw on.
50            fill (int): The fill value for the shape (positive values for outline else is solid).
51            scale (int | float): The scale factor for the shape size.
52        """
53        pass
54
55    def move(self,pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3) -> None:
56        """
57        Moves the shape by the given offset.
58
59        Args:
60            pos (Tuple[int | float, int | float] | Vector2 | Vector3):
61                The amount to move the shape by, relative to its current position.
62        """
63        self._pos.x += pos[0]
64        self._pos.y += pos[1]
65
66    def move_at(self,pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3) -> None:
67        """
68        Moves the shape to a position.
69
70        Args:
71            pos (Tuple[int | float, int | float] | Vector2 | Vector3):
72                The new position for the shape.
73        """
74        self._pos.x = pos[0]
75        self._pos.y = pos[1]

Abstract base class for all drawable shapes.

Shape( pos: Union[Tuple[int | float, int | float], pygame.math.Vector2, pygame.math.Vector3], color: str | tuple)
11    def __init__(self, pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3, color: str | tuple):
12        """
13        Initializes the shape with a position and color.
14
15        Args:
16            pos (pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the shape.
17            color (str | tuple): The color of the shape, either as a string (e.g., "red") or a tuple (R, G, B).
18        """
19        self._pos = vector(*pos)
20        self._color = color

Initializes the shape with a position and color.

Arguments:
  • pos (pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the shape.
  • color (str | tuple): The color of the shape, either as a string (e.g., "red") or a tuple (R, G, B).
position: pygame.math.Vector2 | pygame.math.Vector3
22    @property
23    def position(self) -> pygame.math.Vector2 | pygame.math.Vector3:
24        """property to get a copy of the position of the shape."""
25        return self._pos.copy()

property to get a copy of the position of the shape.

color: Union[str, Tuple[int, int, int]]
27    @property
28    def color(self) -> str | Tuple[int,int,int]:
29        """property to get the color of the shape."""
30        return self._color

property to get the color of the shape.

rect: pygame.rect.Rect | pygame.rect.FRect
32    @property
33    @abstractmethod
34    def rect(self) -> pygame.Rect | pygame.FRect:
35        """
36        Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.
37
38        Returns:
39            pygame.Rect | pygame.FRect: The bounding rectangle of the shape.
40        """
41        pass

Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.

Returns:

pygame.Rect | pygame.FRect: The bounding rectangle of the shape.

@abstractmethod
def draw( self, surf: pygame.surface.Surface, fill: int, scale: int | float) -> None:
43    @abstractmethod
44    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
45        """
46        Abstract method to draw the shape on a surface with a given fill and scale.
47
48        Args:
49            surf (pygame.Surface): The surface to draw on.
50            fill (int): The fill value for the shape (positive values for outline else is solid).
51            scale (int | float): The scale factor for the shape size.
52        """
53        pass

Abstract method to draw the shape on a surface with a given fill and scale.

Arguments:
  • surf (pygame.Surface): The surface to draw on.
  • fill (int): The fill value for the shape (positive values for outline else is solid).
  • scale (int | float): The scale factor for the shape size.
def move( self, pos: Union[Tuple[int | float, int | float], pygame.math.Vector2, pygame.math.Vector3]) -> None:
55    def move(self,pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3) -> None:
56        """
57        Moves the shape by the given offset.
58
59        Args:
60            pos (Tuple[int | float, int | float] | Vector2 | Vector3):
61                The amount to move the shape by, relative to its current position.
62        """
63        self._pos.x += pos[0]
64        self._pos.y += pos[1]

Moves the shape by the given offset.

Arguments:
  • pos (Tuple[int | float, int | float] | Vector2 | Vector3): The amount to move the shape by, relative to its current position.
def move_at( self, pos: Union[Tuple[int | float, int | float], pygame.math.Vector2, pygame.math.Vector3]) -> None:
66    def move_at(self,pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3) -> None:
67        """
68        Moves the shape to a position.
69
70        Args:
71            pos (Tuple[int | float, int | float] | Vector2 | Vector3):
72                The new position for the shape.
73        """
74        self._pos.x = pos[0]
75        self._pos.y = pos[1]

Moves the shape to a position.

Arguments:
  • pos (Tuple[int | float, int | float] | Vector2 | Vector3): The new position for the shape.
class Rect(Shape):
 78class Rect(Shape):
 79    """Represents a rectangle shape."""
 80
 81    def __init__(
 82            self,
 83            pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
 84            size: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
 85            color: str | tuple
 86    ):
 87        """
 88        Initializes the rectangle with position, size, and color.
 89
 90        Args:
 91            pos (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the rectangle.
 92            size (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The size of the rectangle (width, height).
 93            color (str | tuple): The color of the rectangle, either as a string (e.g., "red") or a tuple (R, G, B).
 94        """
 95        super().__init__(pos, color)
 96        self._size = tuple(size)
 97
 98    @property
 99    def size(self) -> Tuple[int | float, int | float]:
100        """ The size of the Rect"""
101        return self._size
102
103    @property
104    def rect(self) -> pygame.Rect | pygame.FRect:
105        """
106        Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.
107
108        Returns:
109            pygame.Rect | pygame.FRect: The bounding rectangle of the shape.
110        """
111        if all(isinstance(i, int) for i in (self._pos[0], self._pos[1], self._size[0], self._size[1])):
112            return pygame.Rect(self._pos, self._size)  # Use pygame.Rect if all values are integers
113        return pygame.FRect(self._pos, self._size)  # Use pygame.FRect otherwise
114
115    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
116        """
117        Draws the rectangle on the surface with a given fill and scale.
118
119        Args:
120            surf (pygame.Surface): The surface to draw on.
121            fill (int): The fill value for the shape (positive values for outline else is solid).
122            scale (int | float): The scale factor for the rectangle size.
123        """
124        # Scale the rectangle and fill value
125        rect = self.rect
126        color = self.color
127        fill *= scale
128        rect.width *= scale
129        rect.height *= scale
130        fill = ceil(fill)  # Ensure fill is an integer
131
132        # Draw the rectangle
133        pygame.draw.rect(surf, color, rect, width=fill if fill > 0 else 0)

Represents a rectangle shape.

Rect( pos: Union[Tuple[int | float, int | float], pygame.math.Vector2, pygame.math.Vector3], size: Union[Tuple[int | float, int | float], pygame.math.Vector2, pygame.math.Vector3], color: str | tuple)
81    def __init__(
82            self,
83            pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
84            size: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
85            color: str | tuple
86    ):
87        """
88        Initializes the rectangle with position, size, and color.
89
90        Args:
91            pos (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the rectangle.
92            size (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The size of the rectangle (width, height).
93            color (str | tuple): The color of the rectangle, either as a string (e.g., "red") or a tuple (R, G, B).
94        """
95        super().__init__(pos, color)
96        self._size = tuple(size)

Initializes the rectangle with position, size, and color.

Arguments:
  • pos (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The position of the rectangle.
  • size (tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3): The size of the rectangle (width, height).
  • color (str | tuple): The color of the rectangle, either as a string (e.g., "red") or a tuple (R, G, B).
size: Tuple[int | float, int | float]
 98    @property
 99    def size(self) -> Tuple[int | float, int | float]:
100        """ The size of the Rect"""
101        return self._size

The size of the Rect

rect: pygame.rect.Rect | pygame.rect.FRect
103    @property
104    def rect(self) -> pygame.Rect | pygame.FRect:
105        """
106        Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.
107
108        Returns:
109            pygame.Rect | pygame.FRect: The bounding rectangle of the shape.
110        """
111        if all(isinstance(i, int) for i in (self._pos[0], self._pos[1], self._size[0], self._size[1])):
112            return pygame.Rect(self._pos, self._size)  # Use pygame.Rect if all values are integers
113        return pygame.FRect(self._pos, self._size)  # Use pygame.FRect otherwise

Returns the bounding rectangle (pygame.Rect or pygame.FRect) based on coordinate types.

Returns:

pygame.Rect | pygame.FRect: The bounding rectangle of the shape.

def draw( self, surf: pygame.surface.Surface, fill: int, scale: int | float) -> None:
115    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
116        """
117        Draws the rectangle on the surface with a given fill and scale.
118
119        Args:
120            surf (pygame.Surface): The surface to draw on.
121            fill (int): The fill value for the shape (positive values for outline else is solid).
122            scale (int | float): The scale factor for the rectangle size.
123        """
124        # Scale the rectangle and fill value
125        rect = self.rect
126        color = self.color
127        fill *= scale
128        rect.width *= scale
129        rect.height *= scale
130        fill = ceil(fill)  # Ensure fill is an integer
131
132        # Draw the rectangle
133        pygame.draw.rect(surf, color, rect, width=fill if fill > 0 else 0)

Draws the rectangle on the surface with a given fill and scale.

Arguments:
  • surf (pygame.Surface): The surface to draw on.
  • fill (int): The fill value for the shape (positive values for outline else is solid).
  • scale (int | float): The scale factor for the rectangle size.
Inherited Members
Shape
position
color
move
move_at
class Circle(Shape):
136class Circle(Shape):
137    """Represents a circle shape."""
138
139    def __init__(self, pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3, radius: int | float, color: str | tuple):
140        """
141        Initializes the circle with position, radius, and color.
142
143        Args:
144            pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
145            radius (int | float): The radius of the circle.
146            color (str | tuple): The color of the circle, either as a string (e.g., "red") or a tuple (R, G, B).
147        """
148        super().__init__(pos, color)
149        self._radius = radius
150
151    @property
152    def radius(self) -> int | float:
153        """ The radius of the Circle"""
154        return self._radius
155
156    @property
157    def rect(self) -> pygame.Rect:
158        """
159        Returns the bounding rectangle for the circle.
160
161        Returns:
162            pygame.Rect: The bounding rectangle that encloses the circle.
163        """
164        pos = (self._pos[0] - self.radius, self._pos[1] - self.radius)  # Top-left corner of the bounding rect
165        size = (self.radius * 2, self.radius * 2)  # Size of the bounding rectangle
166        return pygame.Rect(pos, size)
167
168    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
169        """
170        Draws the circle on the surface with a given fill and scale.
171
172        Args:
173            surf (pygame.Surface): The surface to draw on.
174            fill (int): The fill value for the circle outline (negative for outline, positive for solid).
175            scale (int | float): The scale factor for the circle size.
176        """
177        # Scale the circle and fill value
178        pos = self._pos
179        fill *= scale
180        radius = self.radius * scale  # Scale the radius
181        fill = ceil(fill)  # Ensure fill is an integer
182
183        # Draw the circle
184        pygame.draw.circle(surf, self.color, pos, radius, width=fill if fill > 0 else 0)

Represents a circle shape.

Circle( pos: Union[Tuple[int | float, int | float], pygame.math.Vector2, pygame.math.Vector3], radius: int | float, color: str | tuple)
139    def __init__(self, pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3, radius: int | float, color: str | tuple):
140        """
141        Initializes the circle with position, radius, and color.
142
143        Args:
144            pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
145            radius (int | float): The radius of the circle.
146            color (str | tuple): The color of the circle, either as a string (e.g., "red") or a tuple (R, G, B).
147        """
148        super().__init__(pos, color)
149        self._radius = radius

Initializes the circle with position, radius, and color.

Arguments:
  • pos: Tuple[int | float, int | float] | pygame.math.Vector2 | pygame.math.Vector3,
  • radius (int | float): The radius of the circle.
  • color (str | tuple): The color of the circle, either as a string (e.g., "red") or a tuple (R, G, B).
radius: int | float
151    @property
152    def radius(self) -> int | float:
153        """ The radius of the Circle"""
154        return self._radius

The radius of the Circle

rect: pygame.rect.Rect
156    @property
157    def rect(self) -> pygame.Rect:
158        """
159        Returns the bounding rectangle for the circle.
160
161        Returns:
162            pygame.Rect: The bounding rectangle that encloses the circle.
163        """
164        pos = (self._pos[0] - self.radius, self._pos[1] - self.radius)  # Top-left corner of the bounding rect
165        size = (self.radius * 2, self.radius * 2)  # Size of the bounding rectangle
166        return pygame.Rect(pos, size)

Returns the bounding rectangle for the circle.

Returns:

pygame.Rect: The bounding rectangle that encloses the circle.

def draw( self, surf: pygame.surface.Surface, fill: int, scale: int | float) -> None:
168    def draw(self, surf: pygame.Surface, fill: int, scale: int | float) -> None:
169        """
170        Draws the circle on the surface with a given fill and scale.
171
172        Args:
173            surf (pygame.Surface): The surface to draw on.
174            fill (int): The fill value for the circle outline (negative for outline, positive for solid).
175            scale (int | float): The scale factor for the circle size.
176        """
177        # Scale the circle and fill value
178        pos = self._pos
179        fill *= scale
180        radius = self.radius * scale  # Scale the radius
181        fill = ceil(fill)  # Ensure fill is an integer
182
183        # Draw the circle
184        pygame.draw.circle(surf, self.color, pos, radius, width=fill if fill > 0 else 0)

Draws the circle on the surface with a given fill and scale.

Arguments:
  • surf (pygame.Surface): The surface to draw on.
  • fill (int): The fill value for the circle outline (negative for outline, positive for solid).
  • scale (int | float): The scale factor for the circle size.
Inherited Members
Shape
position
color
move
move_at