From b53ae9cce08f4219d207a3563efe54744f734505 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 22 Apr 2024 00:23:07 -0500 Subject: [PATCH] rects --- src/doodles/doodles.py | 48 +++++++++++++++++++++++++++++++++++ src/doodles/examples/rects.py | 5 ++++ 2 files changed, 53 insertions(+) create mode 100644 src/doodles/examples/rects.py diff --git a/src/doodles/doodles.py b/src/doodles/doodles.py index cd1c8d3..b1ea391 100644 --- a/src/doodles/doodles.py +++ b/src/doodles/doodles.py @@ -372,3 +372,51 @@ class Circle(Doodle): super().random() # constrain to 10-100 return self.radius(random.random()*90 + 10) + + +class Rectangle(Doodle): + def __init__(self, parent=None): + """ + For compatibility with circle, the rectangle is centered at pos + and expands out width/2, height/2 in each cardinal direction. + """ + super().__init__(parent) + self._width = 100 + self._height = 100 + + def __repr__(self): + return f"Rect(pos={self.pos_vec}, width={self._width}, height={self._height}, parent={self._parent})" + + def draw(self, screen): + rect = pygame.Rect( + self.x - self._width/2, + self.y - self._height/2, + self._width, + self._height, + ) + pygame.draw.rect(screen, self._color, rect) + + def width(self, w: float) -> "Doodle": + """ + A setter for the width + """ + self._width = w + return self + + def height(self, h: float) -> "Doodle": + """ + A setter for the height + """ + self._height = h + return self + + def grow(self, dw: float, dh: float): + """ + Modify radius by an amount. (Negative to shrink.) + """ + return self.width(self._w + dw).height(self._h + dh) + + def random(self, upper=100) -> "Doodle": + super().random() + # constrain to 10-100 + return self.width(random.random()*upper + 10).height(random.random()*upper + 10) diff --git a/src/doodles/examples/rects.py b/src/doodles/examples/rects.py new file mode 100644 index 0000000..992c8b4 --- /dev/null +++ b/src/doodles/examples/rects.py @@ -0,0 +1,5 @@ +from doodles.doodles import Group, Rectangle, Color + +for _ in range(100): + r = Rectangle().random(250) +