pyglet.shapes

2D shapes.

This module provides classes for a variety of simplistic 2D shapes, such as Rectangles, Circles, and Lines. These shapes are 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 applicible). To create more complex shapes than what is provided here, the lower evel graphics API is more appropriate. See the Graphics 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)

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

pyglet.app.run()

New in version 1.5.4.

class Arc(x, y, radius, segments=25, angle=6.283185307179586, color=(255, 255, 255), batch=None, group=None)
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.

anchor_position

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

Parameters:
  • x (int or float) – X coordinate of the anchor point.
  • y (int or float) – Y coordinate of the anchor point.
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
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
position

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

Parameters:
  • x (int or float) – X coordinate of the sprite.
  • y (int or float) – Y coordinate of the sprite.
visible

True if the shape will be drawn.

Type:bool
x

X coordinate of the shape.

Type:int or float
y

Y coordinate of the shape.

Type:int or float
class Circle(x, y, radius, segments=None, color=(255, 255, 255), batch=None, group=None)
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.

anchor_position

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

Parameters:
  • x (int or float) – X coordinate of the anchor point.
  • y (int or float) – Y coordinate of the anchor point.
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
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
position

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

Parameters:
  • x (int or float) – X coordinate of the sprite.
  • y (int or float) – Y coordinate of the sprite.
radius

The radius of the circle.

Type:float
visible

True if the shape will be drawn.

Type:bool
x

X coordinate of the shape.

Type:int or float
y

Y coordinate of the shape.

Type:int or float
class Line(x, y, x2, y2, width=1, color=(255, 255, 255), batch=None, group=None)
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.

anchor_position

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

Parameters:
  • x (int or float) – X coordinate of the anchor point.
  • y (int or float) – Y coordinate of the anchor point.
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
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
position

The (x, y, x2, y2) coordinates of the line, as a tuple.

Parameters:
  • x (int or float) – X coordinate of the sprite.
  • y (int or float) – Y coordinate of the sprite.
  • x2 (int or float) – X2 coordinate of the sprite.
  • y2 (int or float) – Y2 coordinate of the sprite.
visible

True if the shape will be drawn.

Type:bool
x

X coordinate of the shape.

Type:int or float
x2

Second X coordinate of the shape.

Type:int or float
y

Y 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), batch=None, group=None)
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.

anchor_position

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

Parameters:
  • x (int or float) – X coordinate of the anchor point.
  • y (int or float) – Y coordinate of the anchor point.
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
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)
height

The height of the rectangle.

Type:float
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
position

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

Parameters:
  • x (int or float) – X coordinate of the sprite.
  • y (int or float) – Y coordinate of the sprite.
rotation

Clockwise rotation of the rectangle, in degrees.

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

Type:float
visible

True if the shape will be drawn.

Type:bool
width

The width of the rectangle.

Type:float
x

X coordinate of the shape.

Type:int or float
y

Y coordinate of the shape.

Type:int or float