Sprite - Rabbyt Reference

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.

(See the docs for `BaseSprite.attrgetter()` for more information.)

Sprite adds support for `u` and `v`, in addition to the attributes supported by `BaseSprite`.

### convert_offset

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

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

### render

`render()`

Renders the sprite onto the screen.

This method depends on the global OpenGL state. Keep that in mind if things seem to go crazy.

### 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.

### 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

red, green, and blue color components

### rgba

red, green, blue, and alpha color components

### right

x coordinate of the right side of the sprite

### rot

rotation

### 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.

### 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.)

### texture_id

`texture_id` is a number as returned by `rabbyt.load_texture()`. (But you can use any loaded OpenGL texture.)

If `texture_id` is `0`, texturing will be disabled for this sprite.

### top

y coordinate of the top of the sprite

### u

u texture offset

### uv

The values of `u` and `v` are added to `tex_shape`. This could be useful if you have multiple frames of an animation on a single texture. Set `tex_shape` to the shape of a single frame and set `uv`, to select each frame.

### v

v texture offset

### x

x coordinate

### xy

A tuple of the x and y coordinates

### y

y coordinate