Documentation Index | From module rabbyt.sprites.

Sprite

Inherits from: BaseSprite

Sprite(texture=None, shape=None, tex_shape=(0,1,1,0), ...)

This class provides a basic, four point, textured sprite.

All arguments are optional.

texture should be an image filename, a pyglet texture object, or an OpenGL texture id. (See Sprite.texture for more information.)

If shape is not given it will default to the dimensions of the texture if they are available. For more information on shape and tex_shape read the docstrings for Sprite.shape and Sprite.tex_shape

Additionally, you can pass values for most of the properties as keyword arguments. (x, y, xy, u, v, uv, etc...)

Methods

attrgetter

attrgetter(name)

Returns an anim that returns the value of the given attribute name.

Perhaps this is easiest to see with an example. The following two lines will both do the same thing, only the second is much, much faster:

sprite.x = lambda: other_sprite.x

sprite.x = other_sprite.attrgetter("x")

The anim returned by attrgetter is implemented in C and will retrieve the value without doing a python attribute lookup.

This works for any attribute that you can assign an anim to.

swizzle descriptors are properly handled.

convert_offset

convert_offset((x, y)) -> (x, y)

Converts coordinates relative to this sprite to global coordinates, including rotation and scaling.

render

render_after_transform

render_after_transform()

This method is called by BaseSprite.render() after transformations have been applied.

If you don't want to mess with doing transformations yourself, you can override this method instead of render().

Properties

alpha

alpha color component

blue

blue color component

bottom

y coordinate of the bottom of the sprite

bounding_radius

bounding_radius

This should be the distance of the farthest point from the center. It can be used for collision detection.

By default this is calculated from the shape property, and is automatically updated whenever the shape is updated. However, you can set it explicitly yourself.

After the value is explicitly set it will no longer be updated by changes to the shape. To revert back to the default behavior, delete the property using the del statement:

del sprite.bounding_radius

bounding_radius_squared

bounding_radius_squared

This is just like bounding_radius, only it's squared. (duh)

bounding_radius and bounding_radius_squared are automatically kept in sync with each other.

green

green color component

left

x coordinate of the left side of the sprite

red

red color component

rgb

swizzle for red, green, blue

rgba

swizzle for red, green, blue, alpha

right

x coordinate of the right side of the sprite

rot

rotation angle in degrees.

scale

scale

1.0 is normal size; 0.5 is half size, 2.0 is double size... you get the point.

You can scale the x and y axes independently by assigning a tuple with a length of two.

scale_x

x component of scale

scale_y

y component of scale

shape

The shape of the sprite.

This must either be of the form [left, top, right, bottom], or a list of four coordinates, eg. [(0,0), (20,0), (20,20), (0,20)]

[-10, -10, 10, 10] is the default.

When you assign to shape, bounding_radius is automatically set to the distance of the farthest coordinate.

tex_shape

This defines how a texture is mapped onto the sprite.

Like Sprite.shape, you can give either [left, top, right, bottom] or a list of coordinates.

The default is [0, 1, 1, 0], which uses the entire texture.

For easy integration with pyglet, a tuple with four items will be interpreted as the format used by the tex_coords attribute of pyglet textures.

texture

Sprite.texture

The texture used for this sprite.

The value can be in a variety of formats:

If it's a string, it will be used as a filename to load the texture.

If it's an integer, it will be used as an OpenGL texture id.

If it's an object with an id attribute, it will be treated as a pyglet texture object. (The width, height, and tex_coords attributes will set the sprite's shape and tex_shape properties.)

top

y coordinate of the top of the sprite

u

texture offset

uv

swizzle for u, v

v

texture offset

x

x coordinate of the sprite

xy

swizzle for x, y

y

y coordinate of the sprite