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). 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 Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=False, color=(255, 255, 255, 255), batch=None, group=None)
group_class

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property angle

The angle of the arc.

Type

float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property rotation

Clockwise rotation of the arc, in degrees.

The arc will be rotated about its (anchor_x, anchor_y) position.

Type

float

property start_angle

The start angle of the arc.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property points

Control points of the curve.

Type

List[[int, int]]

property 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.

property t

Draw 100*t percent of the curve.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property color

The rectangle’s fill color.

This property sets the color of the inside 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)

property group

User assigned Group object.

property height

The height of the rectangle.

Type

float

property 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

property 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.

property rotation

Clockwise rotation of the rectangle, in degrees.

The Rectangle will be rotated about its (anchor_x, anchor_y) position.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property width

The width of the rectangle.

Type

float

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property radius

The radius of the circle.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property a

The semi-major axes of the ellipse.

Type

float

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property b

The semi-minor axes of the ellipse.

Type

float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property rotation

Clockwise rotation of the arc, in degrees.

The arc will be rotated about its (anchor_x, anchor_y) position.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property x2

Second X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

property y2

Second Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property rotation

Clockwise rotation of the polygon, in degrees.

The Polygon will be rotated about its (anchor_x, anchor_y) position.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property height

The height of the rectangle.

Type

float

property 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

property 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.

property rotation

Clockwise rotation of the rectangle, in degrees.

The Rectangle will be rotated about its (anchor_x, anchor_y) position.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property width

The width of the rectangle.

Type

float

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property angle

The angle of the sector.

Type

float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property radius

The radius of the sector.

Type

float

property rotation

Clockwise rotation of the sector, in degrees.

The sector will be rotated about its (anchor_x, anchor_y) position.

Type

float

property start_angle

The start angle of the sector.

Type

float

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

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)
group_class

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property inner_radius

The inner radius of the star.

property num_spikes

Number of spikes of the star.

property 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

property outer_radius

The outer radius of the star.

property 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.

property rotation

Rotation of the star, in degrees.

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

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

alias of _ShapeGroup

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.

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.

property 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.

property anchor_x

The X coordinate of the anchor point

Type

int or float

property anchor_y

The Y coordinate of the anchor point

Type

int or float

property batch

User assigned Batch object.

property 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)

property group

User assigned Group object.

property 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

property 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.

property visible

True if the shape will be drawn.

Type

bool

property x

X coordinate of the shape.

Type

int or float

property x2

Second X coordinate of the shape.

Type

int or float

property x3

Third X coordinate of the shape.

Type

int or float

property y

Y coordinate of the shape.

Type

int or float

property y2

Second Y coordinate of the shape.

Type

int or float

property y3

Third Y coordinate of the shape.

Type

int or float