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.

New in version 1.1.

class Sprite(img, x=0, y=0, blend_src=770, blend_dest=771, batch=None, group=None, usage='dynamic', subpixel=False)

Instance of an on-screen image.

See the module documentation for usage.

Methods

delete()

Force immediate removal of the sprite from video memory.

This is often necessary when using batches, as the Python garbage collector will not necessarily call the finalizer as soon as the sprite is garbage.

draw()

Draw the sprite at its current position.

See the module documentation for hints on drawing multiple sprites efficiently.

update(x=None, y=None, rotation=None, scale=None, scale_x=None, scale_y=None)

Simultaneously change the position, rotation or scale.

This method is provided for performance. In cases where multiple Sprite attributes need to be updated at the same time, it is more efficent to update them together using the update method, rather than modifying them one by one.

Parameters:
  • x (int) – X coordinate of the sprite.
  • y (int) – Y coordinate of the sprite.
  • rotation (float) – Clockwise rotation of the sprite, in degrees.
  • scale (float) – Scaling factor.
  • scale_x (float) – Horizontal scaling factor.
  • scale_y (float) – Vertical scaling factor.

Events

on_animation_end()

The sprite animation reached the final frame.

The event is triggered only if the sprite has an animation, not an image. For looping animations, the event is triggered each time the animation loops.

Attributes

batch

Graphics batch.

The sprite can be migrated from one batch to another, or removed from its batch (for individual drawing). Note that this can be an expensive operation.

Type:pyglet.graphics.Batch
color

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

Parent graphics group.

The sprite can change its rendering group, however this can be an expensive operation.

Type:pyglet.graphics.Group
height

Scaled height of the sprite.

Read-only. Invariant under rotation.

Type:int
image

Image or animation to display.

Type:AbstractImage or Animation
opacity

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.

Type:int
position

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

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

Clockwise rotation of the sprite, in degrees.

The sprite image will be rotated about its image’s (anchor_x, anchor_y) position.

Type:float
scale

Base Scaling factor.

A scaling factor of 1 (the default) has no effect. A scale of 2 will draw the sprite at twice the native size of its image.

Type:float
scale_x

Horizontal scaling factor.

A scaling factor of 1 (the default) has no effect. A scale of 2 will draw the sprite at twice the native width of its image.
Type:float
scale_y

Vertical scaling factor.

A scaling factor of 1 (the default) has no effect. A scale of 2 will draw the sprite at twice the native height of its image.
Type:float
visible

True if the sprite will be drawn.

Type:bool
width

Scaled width of the sprite.

Read-only. Invariant under rotation.

Type:int
x

X coordinate of the sprite.

Type:int
y

Y coordinate of the sprite.

Type:int
class SpriteGroup(texture, blend_src, blend_dest, parent=None)

Shared sprite rendering group.

The group is automatically coalesced with other sprite groups sharing the same parent group, texture and blend parameters.

set_state()

Apply the OpenGL state change.

The default implementation does nothing.

unset_state()

Repeal the OpenGL state change.

The default implementation does nothing.