Renderable
Renderable serves as the base class for any object with a width and height (most notably excluding lines and dots). The Renderable class does extend the Object class which tracks location and updates.

Initialization

We can initialize the default 3 shapes by calling their respective constructors:
1
rectangle = Rectangle(screen, x, y, width, height)
2
oval = Oval(screen, x, y, width, height)
3
triangle = Triangle(screen, x, y, width, height)
Copied!
The constructor has more arguments that are optional:
1
Renderable(screen, x, y, width, height, color, border, fill, rotation, visible)
Copied!
It's crucial to note that all Renderables are rendered from the top-left. So the passed (x, y) for a Rectangle would be its top left corner.

Types

There are a few different Renderables that can be created:
1
rectangle = Rectangle(screen, x, y, width, height)
2
oval = Oval(screen, x, y, width, height)
3
triangle = Triangle(screen, x, y, width, height)
4
5
# Note that for Polygon we specify num_sides before (x, y)
6
polygon = Polygon(screen, num_sides, x, y, width, height)
7
8
# We can create an irregular polygon by specifying vertices
9
irregular = CustomPolygon(screen, vertices, color, border, fill, rotation, visible)
Copied!
Although Text is classified as a Renderable, it is not directly resizable and has specific methods unique to Text: Reference.

Movement

Moving any Renderable is alike to moving a Location:
1
renderable.x(new_x) # Get or set the x-coordinate
2
renderable.y(new_y) # Get or set the y-coordinate
3
4
renderable.move(dx, dy) # Move by (dx, dy)
5
renderable.move((dx, dy)) # Tuple representation of (dx, dy)
6
renderable.move(dx=100) # Move the x-coordinate by +100
7
8
renderable.moveto(x, y) # Move to (x, y)
9
renderable.moveto((x, y)) # Tuple representation of (x, y)
10
renderable.moveto(y=100) # Move the y-coordinate to +100
Copied!
We can also make a Renderable move forward at its current heading/angle via:
1
renderable.forward(distance) # Move forward at current angle by distance
2
renderable.backward(distance) # Move backward at current angle by distance
Copied!
Note that these methods utilize the Renderables rotation.

Location

You can also retrieve the Location with:
1
renderable.location()
Copied!

Center

You can get the Location of the center of any Renderable:
1
renderable.center()
Copied!
Note: This calculates the centroid of the shape.

Rotation

You can get or modify the rotation of a Renderable like so:
1
renderable.rotation() # Get the current angle
2
renderable.rotation(angle) # Set a new angle of rotation
3
4
renderable.rotate(angle_change) # Change the angle by a specified argument
Copied!
You can also just make a Renderable look at a Location or Renderable:
1
renderable.lookat(other) # Look at another Renderable
2
renderable.lookat(location) # Look at a Location
Copied!

Size

You can retrieve or modify the size of the Renderable like so:
1
renderable.width() # Get the current width
2
renderable.width(width) # Modify the width
3
renderable.width(width, ratio=True) # Maintain the ratio of the Renderable
4
5
erable.height() # Get the current height
6
renderable.height(height) # Modify the height
7
renderable.height(height, ratio=True) # Maintain the ratio of the Renderable
Copied!
Note: Width and Height refer to the width and height of the original shape, regardless of rotation.

Color

All Renderables have a default Color of black; the color can be retrieved or set via:
1
renderable.color() # Get the color
2
renderable.color(color) # Set a new color
Copied!

Border and Fill

Renderables also have an optional border that is set to Color.NONE by default. You can set or retrieve the border like so:
1
renderable.border() # Get the border's color. If none is set, returns Color.NONE
2
renderable.border(color) # Set a new color for the border
3
renderable.border(color, width=5) # Set a new color and a borderwidth
4
renderable.border(color, fill=False) # Set a new color and disable the fill
Copied!
Fill exists (as seen above) to create Framed Renderables with ease. Fill can be toggled without calling the border() method like so:
1
renderable.fill(False) # Change the fill to False.
Copied!

Visibility

You can make any Object in pyDraw invisible with:
1
renderable.visible(False) # Make the Object invisible
Copied!

Ordering

You can move objects to the front or back of layers with:
1
renderable.front() # Move to the front
2
renderable.back() # Move to the back
Copied!

Distance

You can get the distance between a Renderable and another Renderable, or Location like so:
1
renderable.distance(other) # Pass in another renderable
2
renderable.distance(location) # Pass in a Location
Copied!

Transform and Cloning

A transform is a data structure that represents the width, height, and rotation. You can copy the transform of a Renderable and set it to another transform:
1
renderable.transform() # Retrieve the transform
2
renderable.transform(transform) # Set a new transform
Copied!
You should only set the transform to other transforms retrieved from Renderables, however, it is possible to create one yourself:
1
transform = (width, height, angle);
Copied!
You can also clone a Renderable by calling the aptly named:
1
renderable.clone()
Copied!

Vertices

For those who want to perform more advanced mathematics with their shapes, you can retrieve a (copy) list of vertices:
1
renderable.vertices()
Copied!
Vertices usually will begin at the top left and work clockwise.

Contains and Overlaps

You can check if a point is contained in any Renderable like so:
1
renderable.contains(location) # Pass in a normal location
2
renderable.contains(x, y) # Or you can specify x and y
3
renderable.contains((x, y)) # Or you can pass in a tuple
Copied!
Or you can check if two Renderables are overlapping:
1
renderable.overlaps(other)
Copied!
Last modified 7mo ago