pyglet.shapes

2D shapes.

This module provides classes for a variety of simplistic 2D shapes, such as Rectangles, Circles, and Lines. These shapes are made internally from OpenGL primitives, and provide excellent performance when drawn as part of a Batch. Convenience methods are provided for positioning, changing color and opacity, and rotation (where applicable). The Python in operator to check whether a point is inside a shape.

To create more complex shapes than what is provided here, the lower level graphics API is more appropriate. See the Shaders and Rendering for more details.

A simple example of drawing shapes:

import pyglet
from pyglet import shapes

window = pyglet.window.Window(960, 540)
batch = pyglet.graphics.Batch()

circle = shapes.Circle(700, 150, 100, color=(50, 225, 30), batch=batch)
square = shapes.Rectangle(200, 200, 200, 200, color=(55, 55, 255), batch=batch)
rectangle = shapes.Rectangle(250, 300, 400, 200, color=(255, 22, 20), batch=batch)
rectangle.opacity = 128
rectangle.rotation = 33
line = shapes.Line(100, 100, 100, 200, width=19, batch=batch)
line2 = shapes.Line(150, 150, 444, 111, width=4, color=(200, 20, 20), batch=batch)
star = shapes.Star(800, 400, 60, 40, num_spikes=20, color=(255, 255, 0), batch=batch)

@window.event
def on_draw():
    window.clear()
    batch.draw()

pyglet.app.run()

Note

Some Shapes, such as Lines and Triangles, have multiple coordinates. If you update the x, y coordinate, this will also affect the secondary coordinates. This allows you to move the shape without affecting it’s overall dimensions.

New in version 1.5.4.

class ShapeBase

Base class for all shape objects.

A number of default shapes are provided in this module. Curves are approximated using multiple vertices.

If you need shapes or functionality not provided in this module, you can write your own custom subclass of ShapeBase by using the provided shapes as reference.

Methods

draw()

Draw the shape at its current position.

Using this method is not recommended. Instead, add the shape to a pyglet.graphics.Batch for efficient rendering.

delete()

Force immediate removal of the shape from video memory.

It is recommended to call this whenever you delete a shape, as the Python garbage collector will not necessarily call the finalizer as soon as the sprite falls out of scope.

Attributes

x

X coordinate of the shape.

Type:

int or float

y

Y coordinate of the shape.

Type:

int or float

position

The (x, y) coordinates of the shape, as a tuple.

Parameters:
xint or float

X coordinate of the sprite.

yint or float

Y coordinate of the sprite.

rotation

Clockwise rotation of the shape in degrees.

It will be rotated about its (anchor_x, anchor_y) position, which defaults to the first vertex point of the shape.

For most shapes, this is the lower left corner. The shapes below default to the points their radius values are measured from:

anchor_x

The X coordinate of the anchor point

Type:

int or float

anchor_y

The Y coordinate of the anchor point

Type:

int or float

anchor_position

The (x, y) coordinates of the anchor point, as a tuple.

Parameters:
xint or float

X coordinate of the anchor point.

yint or float

Y coordinate of the anchor point.

color

The shape color.

This property sets the color of the shape.

The color is specified as an RGB tuple of integers ‘(red, green, blue)’. Each color component must be in the range 0 (dark) to 255 (saturated).

Type:

(int, int, int)

opacity

Blend opacity.

This property sets the alpha component of the color of the shape. With the default blend mode (see the constructor), this allows the shape to be drawn with fractional opacity, blending with the background.

An opacity of 255 (the default) has no effect. An opacity of 128 will make the shape appear translucent.

Type:

int

visible

True if the shape will be drawn.

Type:

bool

group

User assigned Group object.

batch

User assigned Batch object.

class Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=False, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

angle

The angle of the arc.

Type:

float

start_angle

The start angle of the arc.

Type:

float

class BezierCurve(*points, t=1.0, segments=100, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

points

Control points of the curve.

Type:

List[[int, int]]

t

Draw 100*t percent of the curve.

Type:

float

class Circle(x, y, radius, segments=None, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

radius

The radius of the circle.

Type:

float

class Ellipse(x, y, a, b, segments=None, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

a

The semi-major axes of the ellipse.

Type:

float

b

The semi-minor axes of the ellipse.

Type:

float

class Sector(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

angle

The angle of the sector.

Type:

float

start_angle

The start angle of the sector.

Type:

float

radius

The radius of the sector.

Type:

float

class Line(x, y, x2, y2, width=1, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

x2

Second X coordinate of the shape.

Type:

int or float

y2

Second Y coordinate of the shape.

Type:

int or float

class Rectangle(x, y, width, height, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

width

The width of the rectangle.

Type:

float

height

The height of the rectangle.

Type:

float

class Box(x, y, width, height, thickness=1, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

width

The width of the Box.

Type:

float

height

The height of the Box.

Type:

float

class BorderedRectangle(x, y, width, height, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=None, group=None)

Bases: ShapeBase

width

The width of the rectangle.

Type:

float

height

The height of the rectangle.

Type:

float

border_color

The rectangle’s border color.

This property sets the color of the border of a bordered rectangle.

The color is specified as an RGB tuple of integers ‘(red, green, blue)’ or an RGBA tuple of integers ‘(red, green, blue, alpha)`. Setting the alpha on this property will change the alpha of the entire shape, including both the fill and the border.

Each color component must be in the range 0 (dark) to 255 (saturated).

Type:

(int, int, int, int)

class Triangle(x, y, x2, y2, x3, y3, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

x2

Second X coordinate of the shape.

Type:

int or float

y2

Second Y coordinate of the shape.

Type:

int or float

x3

Third X coordinate of the shape.

Type:

int or float

y3

Third Y coordinate of the shape.

Type:

int or float

class Star(x, y, outer_radius, inner_radius, num_spikes, rotation=0, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase

outer_radius

The outer radius of the star.

inner_radius

The inner radius of the star.

num_spikes

Number of spikes of the star.

class Polygon(*coordinates, color=(255, 255, 255, 255), batch=None, group=None)

Bases: ShapeBase