pyglet.sprite
Display positioned, scaled and rotated images.
A sprite is an instance of an image displayed on-screen. Multiple sprites can display the same image at different positions on the screen. Sprites can also be scaled larger or smaller, rotated at any angle and drawn at a fractional opacity.
The following complete example loads a "ball.png" image and creates a
sprite for that image. The sprite is then drawn in the window’s
draw event handler:
import pyglet
ball_image = pyglet.image.load('ball.png')
ball = pyglet.sprite.Sprite(ball_image, x=50, y=50)
window = pyglet.window.Window()
@window.event
def on_draw():
ball.draw()
pyglet.app.run()
The sprite can be moved by modifying the x and
y properties. Other
properties determine the sprite’s rotation,
scale and
opacity.
By default, sprite coordinates are restricted to integer values to avoid
sub-pixel artifacts. If you require to use floats, for example for smoother
animations, you can set the subpixel parameter to True when creating
the sprite (:since: pyglet 1.2).
The sprite’s positioning, rotation and scaling all honor the original
image’s anchor (anchor_x,
anchor_y).
Drawing multiple sprites
Sprites can be “batched” together and drawn at once more quickly than if each
of their draw methods were called individually. The following example
creates one hundred ball sprites and adds each of them to a Batch. The
entire batch of sprites is then drawn in one call:
batch = pyglet.graphics.Batch()
ball_sprites = []
for i in range(100):
x, y = i * 10, 50
ball_sprites.append(pyglet.sprite.Sprite(ball_image, x, y, batch=batch))
@window.event
def on_draw():
batch.draw()
Sprites can be freely modified in any way even after being added to a batch,
however a sprite can belong to at most one batch. See the documentation for
pyglet.graphics for more details on batched rendering, and grouping of
sprites within batches.
Added in version 1.1.
- class Sprite
Manipulate an on-screen image.
See the module documentation for usage.
- group_class
Default class used to create the rendering group.
alias of
SpriteGroup
- property batch: Batch
Graphics batch.
The sprite can be migrated from one batch to another, or removed from its batch (for individual drawing).
Note
Changing this can be an expensive operation as it involves deleting the vertex list and recreating it.
- property blend_mode: tuple[int, int]
The current blend mode applied to this sprite.
Note
Changing this can be an expensive operation as it involves a group creation and transfer.
- property color: tuple[int, int, int, int]
Blend color.
This property sets the color of the sprite’s vertices. This allows the sprite to be drawn with a color tint.
The color is specified as either an RGBA tuple of integers ‘(red, green, blue, opacity)’ or an RGB tuple of integers (red, blue, green).
If there are fewer than three components, a :py:func`ValueError` will be raised. Each color component must be an int in the range 0 (dark) to 255 (saturated). If any component is not an int, a
TypeErrorwill be raised.
-
event_types:
list= ['on_animation_end']
- property frame_index: int
The current Animation frame.
If the
Sprite.imageis anAnimation, you can query or set the current frame. If not an Animation, this will always be 0.
- property group: Group
Parent graphics group specified by the user.
This group will always be the parent of the internal sprite group.
Note
Changing this can be an expensive operation as it involves a group creation and transfer.
- property height: float
Scaled height of the sprite.
Invariant under rotation.
- property image: AbstractImage | Animation
The Sprite’s Image or Animation to display.
Note
Changing this can be an expensive operation if the texture is not part of the same texture or atlas.
- property opacity: int
Blend opacity.
This property sets the alpha component of the colour of the sprite’s vertices. With the default blend mode (see the constructor), this allows the sprite 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 sprite appear translucent.
- property paused: bool
Pause/resume the Sprite’s Animation.
If
Sprite.imageis an Animation, you can pause or resume the animation by setting this property to True or False. If not an Animation, this has no effect.
- property position: tuple[float, float, float]
The (x, y, z) coordinates of the sprite, as a tuple.
- property program: ShaderProgram
The current shader program.
Note
Changing this can be an expensive operation as it involves a group creation and transfer.
- property rotation: float
Clockwise rotation of the sprite, in degrees.
The sprite image will be rotated about its image’s (anchor_x, anchor_y) position.
- property scale: float
Base Scaling factor.
A scaling factor of 1.0 (the default) has no effect. A scale of 2.0 will draw the sprite at twice the native size of its image.
- property scale_x: float
Horizontal scaling factor.
A scaling factor of 1.0 (the default) has no effect. A scale of 2.0 will draw the sprite at twice the native width of its image.
- property scale_y: float
Vertical scaling factor.
A scaling factor of 1.0 (the default) has no effect. A scale of 2.0 will draw the sprite at twice the native height of its image.
- property visible: bool
True if the sprite will be drawn.
- property width: float
Scaled width of the sprite.
Invariant under rotation.
- property x: float
X coordinate of the sprite.
- property y: float
Y coordinate of the sprite.
- property z: float
Z coordinate of the sprite.
- class SpriteGroup
Shared Sprite rendering Group.
The Group defines custom
__eq__and__hash__methods, and so will be automatically coalesced with other Sprite Groups sharing the same parent Group, Texture and blend parameters.