sprite – Tile Grids in motion

class sprite.Sprite(tiles, x=0, y=0, transparent_color=(247, 251, 247), initial_index=0)[source]

A sprite object with multiple pictures for animation.

A Sprite IS a TileGrid with added motion/collision features.

Sprites typically move, and these objects have a deltaX and deltaY. Use the “move_by_deltas” function to change the X,Y position of the Sprite, or you can set the position directly.

You might want to know if Sprites have collided. For this check, each Sprite is treated as a rectangle. The Sprite checks if its rectangle overlaps another Sprite’s rectangle. Sometimes a Sprite image doesn’t fill the entire Sprite area. And some images of the same Sprite may have different collision boxes than the others. A kneeling man, for instance, has a smaller collision area than a standing one.

Use the “set_bounding_box” to define a single box for all images or a list of boxes – a different one for each image. The default bounding box is the entire Sprite.

Use the “check_collision” method to use the bounding boxes and check if one Sprite has collided with another.

For example:

from codex import *
from sprite import Sprite
import ascii_art
import time

ims = ascii_art.get_images(‘’’
#..# ####
.##. #..#
.##. #..#
#..# ####

sp1 = Sprite(ims,x=10,y=10)

sp2 = Sprite(ims,x=12,y=12)

if sp1.check_collision(sp2):
print(“They overlap”)

while True:

Create a new Sprite.

  • tiles (list[Bitmap]) – The images available in the sprite

  • x (int, optional) – The x coordinate relative to the parent object. Defaults to 0.

  • y (int, optional) – The y coordinate relative to the parent object. Defaults to 0.

  • transparent_color (tuple, optional) – The RGB clear-color value. Defaults to None.

  • initial_index (int, optional) – The initial image index. Default is 0 (the first in the list).


Check if this sprite has collided with another.


others (list): a single Sprite or a list of sprites


all the input sprites that overlap this one

Return type



Get the collision detection box definition.


Either a single box or a list of boxes

Return type



Get the current motion deltas.


the dx and dy values

Return type



Move the sprite by its current delta values.


Define the collision detection box for this sprite.

A box is defined by 4 integers: the upper left corner X,Y and the lower right corner X,Y.

You may pass in either form:

  • [1,2,3,4] a list of lists of 4 things as (X1,Y1,X2,Y2)

  • [ [1,2,3,4], [1,2,3,4] ] a list of boxes (one box per image)

For multi-cell sprites, you must use a single bounding box.


box (list): Ether one box or a list of boxes (one per image)

set_deltas(dx, dy)[source]

Set the motion offsets used by move_by_deltas.


dx (int): offset in the X direction dy (int): offset in the Y direction

Download Source