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.

Quick Reference can help with simple issues or give you a basic understanding of the methods available, but it can never replace full documentation which is available here.

Initialization

We can initialize the default 3 shapes by calling their respective constructors:

rectangle = Rectangle(screen, x, y, width, height)
oval = Oval(screen, x, y, width, height)
triangle = Triangle(screen, x, y, width, height)

The constructor has more arguments that are optional:

Renderable(screen, x, y, width, height, color, border, fill, rotation, visible)

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:

rectangle = Rectangle(screen, x, y, width, height)
oval = Oval(screen, x, y, width, height)
triangle = Triangle(screen, x, y, width, height)

# Note that for Polygon we specify num_sides before (x, y)
polygon = Polygon(screen, num_sides, x, y, width, height)

# We can create an irregular polygon by specifying vertices
irregular = CustomPolygon(screen, vertices, color, border, fill, rotation, visible)

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:

renderable.x(new_x)  # Get or set the x-coordinate
renderable.y(new_y)  # Get or set the y-coordinate

renderable.move(dx, dy)  # Move by (dx, dy)
renderable.move((dx, dy))  # Tuple representation of (dx, dy)
renderable.move(dx=100)  # Move the x-coordinate by +100

renderable.moveto(x, y)  # Move to (x, y)
renderable.moveto((x, y))  # Tuple representation of (x, y)
renderable.moveto(y=100)  # Move the y-coordinate to +100

We can also make a Renderable move forward at its current heading/angle via:

renderable.forward(distance)  # Move forward at current angle by distance
renderable.backward(distance)  # Move backward at current angle by distance

Note that these methods utilize the Renderables rotation.

Location

You can also retrieve the Location with:

renderable.location()

Center

You can get the Location of the center of any Renderable:

renderable.center()

Note: This calculates the center by default (except for CustomPolygons), but you can get the centroid of the shape by setting centroid=True in the method arguments.

You can also use this method to move the Renderable to place its center in a certain location:

renderable.center(x, y)
renderable.center(location)
renderable.center((x, y))

Rotation

You can get or modify the rotation of a Renderable like so:

renderable.rotation()  # Get the current angle
renderable.rotation(angle)  # Set a new angle of rotation

renderable.rotate(angle_change)  # Change the angle by a specified argument

You can also just make a Renderable look at a Location or Renderable:

renderable.lookat(other)  # Look at another Renderable
renderable.lookat(location)  # Look at a Location

You can also check the angle of the Renderable against any Object or Location:

renderable.angleto(obj)
renderable.angleto(location)
renderable.angleto((x, y))

Size

You can retrieve or modify the size of the Renderable like so:

renderable.width()  # Get the current width
renderable.width(width)  # Modify the width
renderable.width(width, ratio=True)  # Maintain the ratio of the Renderable

erable.height()  # Get the current height
renderable.height(height)  # Modify the height
renderable.height(height, ratio=True)  # Maintain the ratio of the Renderable

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:

renderable.color()  # Get the color
renderable.color(color)  # Set a new color

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:

renderable.border()  # Get the border's color. If none is set, returns Color.NONE
renderable.border(color)  # Set a new color for the border
renderable.border(color, width=5)  # Set a new color and a borderwidth
renderable.border(color, fill=False)  # Set a new color and disable the fill

You can get or set the border_width seperately with:

renderable.border_width()  # returns border width
renderable.border_width(5)  # sets to 5

Fill exists (as seen above) to create Framed Renderables with ease. Fill can be toggled without calling the border() method like so:

renderable.fill(False)  # Change the fill to False.

Visibility

You can make any Object in pyDraw invisible with:

renderable.visible(False)  # Make the Object invisible

Ordering

You can move objects to the front or back of layers with:

renderable.front()  # Move to the front
renderable.back()  # Move to the back

Distance

You can get the distance between a Renderable and another Renderable, or Location like so:

renderable.distance(other)  # Pass in another renderable
renderable.distance(location)  # Pass in a Location

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:

renderable.transform()  # Retrieve the transform
renderable.transform(transform)  # Set a new transform

You should only set the transform to other transforms retrieved from Renderables, however, it is possible to create one yourself:

transform = (width, height, angle);

You can also clone a Renderable by calling the aptly named:

renderable.clone()

Vertices

For those who want to perform more advanced mathematics with their shapes, you can retrieve a (copy) list of vertices:

renderable.vertices()

Vertices usually will begin at the top left and work clockwise.

Bounds

You can get the location and dimensions of a bounding box calculated by pyDraw around any Renderable:

renderable.bounds()  # returns (Location, width, height)

Contains and Overlaps

You can check if a point is contained in any Renderable like so:

renderable.contains(location)  # Pass in a normal location
renderable.contains(x, y)  # Or you can specify x and y
renderable.contains((x, y))  # Or you can pass in a tuple

Or you can check if two Renderables are overlapping:

renderable.overlaps(other)

Last updated