Sprite Lists#
arcade.SpriteList#
- class arcade.SpriteList(use_spatial_hash=None, spatial_hash_cell_size=128, is_static=False, atlas: TextureAtlas = None, capacity: int = 100, lazy: bool = False, visible: bool = True)[source]#
The purpose of the spriteList is to batch draw a list of sprites. Drawing single sprites will not get you anywhere performance wise as the number of sprites in your project increases. The spritelist contains many low level optimizations taking advantage of your graphics processor. To put things into perspective, a spritelist can contain tens of thousands of sprites without any issues. Sprites outside the viewport/window will not be rendered.
If the spriteslist are going to be used for collision it’s a good idea to enable spatial hashing. Especially if no sprites are moving. This will make collision checking a lot faster. In technical terms collision checking is
O(1)
with spatial hashing enabled andO(N)
without. However, if you have a list of moving sprites the cost of updating the spatial hash when they are moved can be greater than what you save with spatial collision checks. This needs to be profiled on a case by case basis.For the advanced options check the advanced section in the arcade documentation.
- Parameters
use_spatial_hash (bool) – If set to True, this will make creating a sprite, and moving a sprite in the SpriteList slower, but it will speed up collision detection with items in the SpriteList. Great for doing collision detection with static walls/platforms in large maps.
spatial_hash_cell_size (int) – The cell size of the spatial hash (default: 128)
is_static (bool) – DEPRECATED. This parameter has no effect.
atlas (TextureAtlas) – (Advanced) The texture atlas for this sprite list. If no atlas is supplied the global/default one will be used.
capacity (int) – (Advanced) The initial capacity of the internal buffer. It’s a suggestion for the maximum amount of sprites this list can hold. Can normally be left with default value.
lazy (bool) – (Advanced) Enabling lazy spritelists ensures no internal OpenGL resources are created until the first draw call or
initialize()
is called. This can be useful when making spritelists in threads because only the main thread is allowed to interact with OpenGL.visible (bool) – Setting this to False will cause the SpriteList to not be drawn. When draw is called, the method will just return without drawing.
- property alpha: int#
Get or set the alpha/transparency of the entire spritelist. This is a byte value from 0 to 255 were 0 is completely transparent/invisible and 255 is opaque.
- property alpha_normalized: float#
Get or set the alpha/transparency of all the sprites in the list. This is a floating point number from 0.0 to 1.0 were 0.0 is completely transparent/invisible and 1.0 is opaque.
This is a shortcut for setting the alpha value in the spritelist color.
- Return type
- append(sprite: arcade.sprite_list.sprite_list._SpriteType)[source]#
Add a new sprite to the list.
- Parameters
sprite (Sprite) – Sprite to add to the list.
- property atlas: TextureAtlas#
Get the texture atlas for this sprite list
- property buffer_angles: arcade.gl.buffer.Buffer#
Get the internal OpenGL angle buffer for the spritelist.
This buffer contains a series of 32 bit floats representing the rotation angle for each sprite in degrees.
This buffer is attached to the
geometry
instance with namein_angle
.
- property buffer_colors: arcade.gl.buffer.Buffer#
Get the internal OpenGL color buffer for this spritelist.
This buffer contains a series of 32 bit floats representing the RGBA color for each sprite. 4 x floats = RGBA.
This buffer is attached to the
geometry
instance with namein_color
.
- property buffer_indices: arcade.gl.buffer.Buffer#
Get the internal index buffer for this spritelist.
The data in the other buffers are not in the correct order matching
spritelist[i]
. The index buffer has to be used used to resolve the right order. It simply contains a series of integers referencing locations in the other buffers.Also note that the length of this buffer might be bigger than the number of sprites. Rely on
len(spritelist)
for the correct length.This index buffer is attached to the
geometry
instance and will be automatically be applied the the input buffers when rendering or transforming.
- property buffer_positions: arcade.gl.buffer.Buffer#
Get the internal OpenGL position buffer for this spritelist.
The buffer contains 32 bit float values with x and y positions. These are the center postions for each sprite.
This buffer is attached to the
geometry
instance with namein_pos
.
- property buffer_sizes: arcade.gl.buffer.Buffer#
Get the internal OpenGL size buffer for this spritelist.
The buffer contains 32 bit float width and height values.
This buffer is attached to the
geometry
instance with namein_size
.
- property buffer_textures: arcade.gl.buffer.Buffer#
Get the internal openGL texture id buffer for the spritelist.
This buffer contains a series of single 32 bit floats referencing a texture ID. This ID references a texture in the texture atlas assigned to this spritelist. The ID is used to look up texture coordinates in a 32bit floating point texture the texter atlas provides. This system makes sure we can resize and rebuild a texture atlas without having to rebuild every single spritelist.
This buffer is attached to the
geometry
instance with namein_texture
.Note that it should ideally an unsigned integer, but due to compatibility we store them as 32 bit floats. We cast them to integers in the shader.
- clear(deep: bool = True)[source]#
Remove all the sprites resetting the spritelist to it’s initial state.
The complexity of this method is
O(N)
with a deep clear (default). If ALL the sprites in the list gets garbage collected with the list itself you can do anO(1)`
clear usingdeep=False
. Make sure you know exactly what you are doing before using this option. Any lingering sprite reference will cause a massive memory leak. Thedeep
option will iterate all the sprites and remove their references to this spritelist. Sprite and SpriteList have a circular reference for performance reasons.
- property color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]]#
Get or set the spritelist color. This will affect all sprites in the list. Individual sprites can also be assigned a color. These colors are converted into floating point colors (0.0 -> 1.0) and multiplied together.
The final color of the sprite is:
texture_color * sprite_color * spritelist_color
- Return type
Color
- property color_normalized: Tuple[float, float, float, float]#
Get or set the spritelist color in normalized form (0.0 -> 1.0 floats). This property works the same as
color
.
- draw(*, filter=None, pixelated=None, blend_function=None)[source]#
Draw this list of sprites.
- Parameters
filter – Optional parameter to set OpenGL filter, such as gl.GL_NEAREST to avoid smoothing.
pixelated –
True
for pixelated andFalse
for smooth interpolation. Shortcut for setting filter=GL_NEAREST.blend_function – Optional parameter to set the OpenGL blend function used for drawing the sprite list, such as ‘arcade.Window.ctx.BLEND_ADDITIVE’ or ‘arcade.Window.ctx.BLEND_DEFAULT’
- draw_hit_boxes(color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]] = (0, 0, 0, 255), line_thickness: float = 1)[source]#
Draw all the hit boxes in this list
- extend(sprites: Union[list, arcade.sprite_list.sprite_list.SpriteList])[source]#
Extends the current list with the given list
- Parameters
sprites (list) – list of Sprites to add to the list
- property geometry: arcade.gl.vertex_array.Geometry#
Returns the internal OpenGL geometry for this spritelist. This can be used to execute custom shaders with the spritelist data.
One or multiple of the following inputs must be defined in your vertex shader:
in vec2 in_pos; in float in_angle; in vec2 in_size; in float in_texture; in vec4 in_color;
- index(sprite: arcade.sprite.Sprite) int [source]#
Return the index of a sprite in the spritelist
- initialize()[source]#
Create the internal OpenGL resources. This can be done if the sprite list is lazy or was created before the window / context. The initialization will happen on the first draw if this method is not called. This is acceptable for most people, but this method gives you the ability to pre-initialize to potentially void initial stalls during rendering.
Calling this otherwise will have no effect. Calling this method in another thread will result in an OpenGL error.
- insert(index: int, sprite: arcade.sprite_list.sprite_list._SpriteType)[source]#
Inserts a sprite at a given index.
- move(change_x: float, change_y: float) None [source]#
Moves all Sprites in the list by the same amount. This can be a very expensive operation depending on the size of the sprite list.
- on_update(delta_time: float = 0.016666666666666666)[source]#
Update the sprite. Similar to update, but also takes a delta-time.
- pop(index: int = - 1) arcade.sprite.Sprite [source]#
Pop off the last sprite, or the given index, from the list
- Parameters
index (int) – Index of sprite to remove, defaults to -1 for the last item.
- preload_textures(texture_list: List[Texture]) None [source]#
Preload a set of textures that will be used for sprites in this sprite list.
- Parameters
texture_list (array) – List of textures.
- remove(sprite: arcade.sprite_list.sprite_list._SpriteType)[source]#
Remove a specific sprite from the list. :param Sprite sprite: Item to remove from the list
- rescale(factor: float) None [source]#
Rescale all sprites in the list relative to the spritelists center.
- sort(*, key=None, reverse: bool = False)[source]#
Sort the spritelist in place using
<
comparison between sprites. This function is similar to python’slist.sort
.Example sorting sprites based on y axis position using a lambda:
# Normal order spritelist.sort(key=lambda x: x.position[1]) # Reversed order spritelist.sort(key=lambda x: x.position[1], reverse=True)
Example sorting sprites using a function:
# More complex sorting logic can be applied, but let's just stick to y position def create_y_pos_comparison(sprite): return sprite.position[1] spritelist.sort(key=create_y_pos_comparison)
- Parameters
key – A function taking a sprite as an argument returning a comparison key
reverse (bool) – If set to
True
the sprites will be sorted in reverse
- swap(index_1: int, index_2: int)[source]#
Swap two sprites by index :param int index_1: Item index to swap :param int index_2: Item index to swap
- update_angle(sprite: arcade.sprite.Sprite)[source]#
Called by the Sprite class to update the angle in this sprite. Necessary for batch drawing of items.
- Parameters
sprite (Sprite) – Sprite to update.
- update_animation(delta_time: float = 0.016666666666666666)[source]#
Call the update_animation in every sprite in the sprite list.
- update_color(sprite: arcade.sprite.Sprite) None [source]#
Called by the Sprite class to update position, angle, size and color of the specified sprite. Necessary for batch drawing of items.
- Parameters
sprite (Sprite) – Sprite to update.
- update_height(sprite: arcade.sprite.Sprite)[source]#
Called by the Sprite class to update the size/scale in this sprite. Necessary for batch drawing of items.
- Parameters
sprite (Sprite) – Sprite to update.
- update_location(sprite: arcade.sprite.Sprite)[source]#
Called by the Sprite class to update the location in this sprite. Necessary for batch drawing of items.
- Parameters
sprite (Sprite) – Sprite to update.
- update_position(sprite: arcade.sprite.Sprite) None [source]#
Called when setting initial position of a sprite when added or inserted into the SpriteList.
update_location
should be called to move them once the sprites are in the list.- Parameters
sprite (Sprite) – Sprite to update.
- update_size(sprite: arcade.sprite.Sprite) None [source]#
Called by the Sprite class to update the size/scale in this sprite. Necessary for batch drawing of items.
- Parameters
sprite (Sprite) – Sprite to update.
- update_texture(sprite) None [source]#
Make sure we update the texture for this sprite for the next batch drawing
- update_width(sprite: arcade.sprite.Sprite)[source]#
Called by the Sprite class to update the size/scale in this sprite. Necessary for batch drawing of items.
- Parameters
sprite (Sprite) – Sprite to update.
- property use_spatial_hash: bool#
Boolean variable that controls if this sprite list is using a spatial hash. If spatial hashing is turned on, it takes longer to move a sprite, and less time to see if that sprite is colliding with another sprite.
- property visible: bool#
Get or set the visible flag for this spritelist. If visible is
False
thedraw()
has no effect.- Return type
- write_sprite_buffers_to_gpu() None [source]#
Ensure buffers are resized and fresh sprite data is written into the internal sprite buffers.
This is automatically called in
SpriteList.draw()
, but there are instances when using custom shaders we need to force this to happen since we might have not calledSpriteList.draw()
since the spritelist was modified.If you have added, removed, moved or changed ANY sprite property this method will synchronize the data on the gpu side (buffer resizing and writing in new data).
arcade.check_for_collision#
- arcade.check_for_collision(sprite1: arcade.sprite.Sprite, sprite2: arcade.sprite.Sprite) bool [source]#
Check for a collision between two sprites.
- Parameters
sprite1 – First sprite
sprite2 – Second sprite
- Returns
True or False depending if the sprites intersect.
- Return type
arcade.check_for_collision_with_list#
- arcade.check_for_collision_with_list(sprite: arcade.sprite.Sprite, sprite_list: arcade.sprite_list.sprite_list.SpriteList, method=0) List[arcade.sprite.Sprite] [source]#
Check for a collision between a sprite, and a list of sprites.
- Parameters
sprite (Sprite) – Sprite to check
sprite_list (SpriteList) – SpriteList to check against
method (int) – Collision check method. 0 is auto-select. (spatial if available, GPU if 1500+ sprites, else simple) 1 is Spatial Hashing if available, 2 is GPU based, 3 is simple check-everything. Defaults to 0.
- Returns
List of sprites colliding, or an empty list.
- Return type
arcade.check_for_collision_with_lists#
- arcade.check_for_collision_with_lists(sprite: arcade.sprite.Sprite, sprite_lists: Iterable[arcade.sprite_list.sprite_list.SpriteList], method=1) List[arcade.sprite.Sprite] [source]#
Check for a collision between a Sprite, and a list of SpriteLists.
- Parameters
sprite (Sprite) – Sprite to check
sprite_lists (List[SpriteList]) – SpriteLists to check against
method (int) – Collision check method. 1 is Spatial Hashing if available, 2 is GPU based, 3 is slow CPU-bound check-everything. Defaults to 1.
- Returns
List of sprites colliding, or an empty list.
- Return type
arcade.get_closest_sprite#
- arcade.get_closest_sprite(sprite: arcade.sprite.Sprite, sprite_list: arcade.sprite_list.sprite_list.SpriteList) Optional[Tuple[arcade.sprite.Sprite, float]] [source]#
Given a Sprite and SpriteList, returns the closest sprite, and its distance.
- Parameters
sprite (Sprite) – Target sprite
sprite_list (SpriteList) – List to search for closest sprite.
- Returns
A tuple containing the closest sprite and the minimum distance. If the spritelist is empty we return
None
.- Return type
arcade.get_sprites_at_exact_point#
- arcade.get_sprites_at_exact_point(point: Union[Tuple[float, float], List[float]], sprite_list: arcade.sprite_list.sprite_list.SpriteList) List[arcade.sprite.Sprite] [source]#
Get a list of sprites whose center_x, center_y match the given point. This does NOT return sprites that overlap the point, the center has to be an exact match.
- Parameters
point (Point) – Point to check
sprite_list (SpriteList) – SpriteList to check against
- Returns
List of sprites colliding, or an empty list.
- Return type
arcade.get_sprites_at_point#
- arcade.get_sprites_at_point(point: Union[Tuple[float, float], List[float]], sprite_list: arcade.sprite_list.sprite_list.SpriteList) List[arcade.sprite.Sprite] [source]#
Get a list of sprites at a particular point. This function sees if any sprite overlaps the specified point. If a sprite has a different center_x/center_y but touches the point, this will return that sprite.
- Parameters
point (Point) – Point to check
sprite_list (SpriteList) – SpriteList to check against
- Returns
List of sprites colliding, or an empty list.
- Return type