sprite
– Tile Grids in motion¶
-
class
codex.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 Spriteimport ascii_artimport timeims = ascii_art.get_images(‘’’/#=(255,255,255)#..# ####.##. #..#.##. #..##..# ####‘’’)sp1 = Sprite(ims,x=10,y=10)display.add_child(sp1)sp2 = Sprite(ims,x=12,y=12)display.add_child(sp2)if sp1.check_collision(sp2):print(“They overlap”)sp1.set_deltas(2,5)while True:sp1.move_by_deltas()time.sleep(0.25)Create a new Sprite.
- Parameters
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_collision
(others)[source]¶ Check if this sprite has collided with another.
- Params:
others (list): a single Sprite or a list of sprites
- Returns
all the input sprites that overlap this one
- Return type
-
get_bounding_box
()[source]¶ Get the collision detection box definition.
- Returns
Either a single box or a list of boxes
- Return type
-
set_bounding_box
(box)[source]¶ 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.
- Params:
box (list): Ether one box or a list of boxes (one per image)