rects
This commit is contained in:
parent
aa82295182
commit
b53ae9cce0
@ -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)
|
||||
|
5
src/doodles/examples/rects.py
Normal file
5
src/doodles/examples/rects.py
Normal file
@ -0,0 +1,5 @@
|
||||
from doodles.doodles import Group, Rectangle, Color
|
||||
|
||||
for _ in range(100):
|
||||
r = Rectangle().random(250)
|
||||
|
Loading…
Reference in New Issue
Block a user