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 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)
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()
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), 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.
-
rotation
¶ Clockwise rotation of the arc, in degrees.
The arc will be rotated about its (anchor_x, anchor_y) position.
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
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
Ellipse
(x, y, a, b, 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.
-
a
¶ The semi-major axes of the ellipse.
Type: float
-
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
-
b
¶ The semi-minor axes of the ellipse.
Type: 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.
-
rotation
¶ Clockwise rotation of the arc, in degrees.
The arc will be rotated about its (anchor_x, anchor_y) position.
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 line.
- y (int or float) – Y coordinate of the line.
- x2 (int or float) – X2 coordinate of the line.
- y2 (int or float) – Y2 coordinate of the line.
-
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
-
-
class
BorderedRectangle
(x, y, width, height, border=1, color=(255, 255, 255), border_color=(100, 100, 100), 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
-
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)’. Each color component must be in the range 0 (dark) to 255 (saturated).
Type: (int, int, int)
-
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
-
-
class
Triangle
(x, y, x2, y2, x3, y3, 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, x3, y3) coordinates of the triangle, as a tuple.
Parameters: - x (int or float) – X coordinate of the triangle.
- y (int or float) – Y coordinate of the triangle.
- x2 (int or float) – X2 coordinate of the triangle.
- y2 (int or float) – Y2 coordinate of the triangle.
- x3 (int or float) – X3 coordinate of the triangle.
- y3 (int or float) – Y3 coordinate of the triangle.
-
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
-
x3
¶ Third 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
-
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), 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)
-
inner_radius
¶ The inner radius of the star.
-
num_spikes
¶ Number of spikes of the star.
-
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
-
outer_radius
¶ The outer radius of the star.
-
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
¶ Rotation of the star, in degrees.
-
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
Polygon
(*coordinates, 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 shape.
- y (int or float) – Y coordinate of the shape.
-
rotation
¶ Clockwise rotation of the polygon, in degrees.
The Polygon will be rotated about its (anchor_x, anchor_y) position.
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
Sector
(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, 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
-
rotation
¶ Clockwise rotation of the sector, in degrees.
The sector will be rotated about its (anchor_x, anchor_y) position.
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
-