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
- 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_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).
- 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:
- 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
- class BezierCurve(*points, t=1.0, segments=100, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class Circle(x, y, radius, segments=None, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class Ellipse(x, y, a, b, segments=None, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class Sector(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class Line(x, y, x2, y2, width=1, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class Rectangle(x, y, width, height, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class Box(x, y, width, height, thickness=1, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase
- class BorderedRectangle(x, y, width, height, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=None, group=None)
Bases:
ShapeBase
- 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).
- class Triangle(x, y, x2, y2, x3, y3, color=(255, 255, 255, 255), batch=None, group=None)
Bases:
ShapeBase