Class pyglet.sprite.Sprite

event.EventDispatcher --+
                        |
                       Sprite

Instance of an on-screen image.

See the module documentation for usage.

Events

  on_animation_end(self)
The sprite animation reached the final frame.

Methods

  __init__(self, img, x=0, y=0, blend_src=770, blend_dest=771, batch=None, group=None, usage='dynamic')
Create a sprite.
  __del__(self)
  delete(self)
Force immediate removal of the sprite from video memory.
  set_position(self, x, y)
Set the X and Y coordinates of the sprite simultaneously.
  draw(self)
Draw the sprite at its current position.
  dispatch_event(self, event_type, *args)
Dispatch a single event to the attached handlers.
(Inherited from pyglet.event.EventDispatcher)
  event(self, *args)
Function decorator for an event handler.
(Inherited from pyglet.event.EventDispatcher)
  pop_handlers(self)
Pop the top level of event handlers off the stack.
(Inherited from pyglet.event.EventDispatcher)
  push_handlers(self, *args, **kwargs)
Push a level onto the top of the handler stack, then attach zero or more event handlers.
(Inherited from pyglet.event.EventDispatcher)
  register_event_type(cls, name)
Register an event type with the dispatcher.
(Inherited from pyglet.event.EventDispatcher)
  remove_handler(self, name, handler)
Remove a single event handler.
(Inherited from pyglet.event.EventDispatcher)
  remove_handlers(self, *args, **kwargs)
Remove event handlers from the event stack.
(Inherited from pyglet.event.EventDispatcher)
  set_handler(self, name, handler)
Attach a single event handler.
(Inherited from pyglet.event.EventDispatcher)
  set_handlers(self, *args, **kwargs)
Attach one or more event handlers to the top level of the handler stack.
(Inherited from pyglet.event.EventDispatcher)

Properties

Batch batch
Graphics batch.
Group group
Parent graphics group.
AbstractImage or Animation image
Image or animation to display.
(int, int) position
The (x, y) coordinates of the sprite.
int x
X coordinate of the sprite.
int y
Y coordinate of the sprite.
float rotation
Clockwise rotation of the sprite, in degrees.
float scale
Scaling factor.
int width
Scaled width of the sprite.
int height
Scaled height of the sprite.
int opacity
Blend opacity.
(int, int, int) color
Blend color.
  visible

Class Variables

  event_types = ['on_animation_end']

Event Details

on_animation_end

on_animation_end(self)

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.


Method Details

__init__

(Constructor) __init__(self, img, x=0, y=0, blend_src=770, blend_dest=771, batch=None, group=None, usage='dynamic')
Create a sprite.
Parameters:
img : AbstractImage or Animation
Image or animation to display.
x : int
X coordinate of the sprite.
y : int
Y coordinate of the sprite.
blend_src : int
OpenGL blend source mode. The default is suitable for compositing sprites drawn from back-to-front.
blend_dest : int
OpenGL blend destination mode. The default is suitable for compositing sprites drawn from back-to-front.
batch : Batch
Optional batch to add the sprite to.
group : Group
Optional parent group of the sprite.
usage : str
Vertex buffer object usage hint, one of "none" (default), "stream", "dynamic" or "static". Applies only to vertex data.

delete

delete(self)

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.

set_position

set_position(self, x, y)
Set the X and Y coordinates of the sprite simultaneously.
Parameters:
x : int
X coordinate of the sprite.
y : int
Y coordinate of the sprite.

draw

draw(self)

Draw the sprite at its current position.

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


Property Details

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:
Batch

group

Parent graphics group.

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

Type:
Group

image

Image or animation to display.
Type:
AbstractImage or Animation

position

The (x, y) coordinates of the sprite.
Type:
(int, int)

x

X coordinate of the sprite.
Type:
int

y

Y coordinate of the sprite.
Type:
int

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

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

width

Scaled width of the sprite.

Read-only. Invariant under rotation.

Type:
int

height

Scaled height of the sprite.

Read-only. Invariant under rotation.

Type:
int

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

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)

visible