Python arcade sprite sheet

Sprite Lists#

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.

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.

append ( sprite : SpriteType ) [source] #

Add a new sprite to the list.

sprite (Sprite) – Sprite to add to the list.

Get the texture atlas for this sprite list

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 name in_angle .

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 name in_color .

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.

Get the internal OpenGL position buffer for this spritelist.

The buffer contains 32 bit float values with x, y and z positions. These are the center positions for each sprite.

This buffer is attached to the geometry instance with name in_pos .

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 name in_size .

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 name in_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.

Get the mean center coordinates of all sprites in the list.

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 an O(1)` clear using deep=False . Make sure you know exactly what you are doing before using this option. Any lingering sprite reference will cause a massive memory leak. The deep option will iterate all the sprites and remove their references to this spritelist. Sprite and SpriteList have a circular reference for performance reasons.

Читайте также:  Java http server thread

Get or set the multiply color for all sprites in the list RGBA integers

This will affect all sprites in the list, and each value must be between 0 and 255.

The color may be specified as any of the following:

  • an RGBA tuple with each channel value between 0 and 255
  • an instance of Color
  • an RGB tuple , in which case the color will be treated as opaque

Each individual sprite can also be assigned a color via its color property.

When SpriteList.draw() is called, each pixel will default to a value equivalent to the following:

  1. Convert the sampled texture, sprite, and list colors into normalized floats (0.0 to 1.0)
  2. Multiply the color channels together: texture_color * sprite_color * spritelist_color
  3. Multiply the floating point values by 255 and round the result

Get or set the spritelist color in normalized form (0.0 -> 1.0 floats). This property works the same as color .

Deletes the internal spatial hash object

draw ( * , filter = None , pixelated = None , blend_function = None ) [source] #

Draw this list of sprites.

  • filter – Optional parameter to set OpenGL filter, such as gl.GL_NEAREST to avoid smoothing.
  • pixelated – True for pixelated and False 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 all the hit boxes in this list

enable_spatial_hashing ( spatial_hash_cell_size : int = 128 ) [source] #

Extends the current list with the given iterable

sprites (list) – Iterable of Sprites to add to the list

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; 

Return the index of a sprite in the spritelist

sprite (Sprite) – Sprite to find and return the index of

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 : SpriteType ) [source] #

Inserts a sprite at a given index.

  • index (int) – The index at which to insert
  • sprite (Sprite) – The sprite to insert

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.

  • change_x (float) – Amount to change all x values by
  • change_y (float) – Amount to change all y values by
Читайте также:  Css button как ссылка

Update the sprite. Similar to update, but also takes a delta-time.

pop ( index : int = -1 ) → SpriteType [source] #

Pop off the last sprite, or the given index, from the list

index (int) – Index of sprite to remove, defaults to -1 for the last item.

Preload a set of textures that will be used for sprites in this sprite list.

texture_list (array) – List of textures.

remove ( sprite : SpriteType ) [source] #

Remove a specific sprite from the list. :param Sprite sprite: Item to remove from the list

Rescale all sprites in the list relative to the spritelists center.

Reverses the current list in-place

Shuffles the current list in-place

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) 
  • 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 two sprites by index :param int index_1: Item index to swap :param int index_2: Item index to swap

Call the update() method on each sprite in the list.

update_animation ( delta_time : float = 0.016666666666666666 ) [source] #

Call the update_animation in every sprite in the sprite list.

Get or set the visible flag for this spritelist. If visible is False the draw() has no effect.

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 called SpriteList.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.SpatialHash#

Structure for fast collision checking with sprites.

cell_size (int) – Size (width and height) of the cells in the spatial hash

Add a sprite to the spatial hash.

sprite (Sprite) – The sprite to add

Return the number of sprites in the spatial hash

get_sprites_near_point ( point : Tuple [ float , float ] ) → Set [ SpriteType ] [source] #

Return sprites in the same bucket as the given point.

point (Point) – The point to check

get_sprites_near_rect ( rect : Tuple [ int , int , int , int ] | List [ int ] ) → Set [ SpriteType ] [source] #

Return sprites in the same buckets as the given rectangle.

rect (Rect) – The rectangle to check (left, right, bottom, top)

A set of sprites in the rectangle

get_sprites_near_sprite ( sprite : SpriteType ) → Set [ SpriteType ] [source] #

Get all the sprites that are in the same buckets as the given sprite.

Читайте также:  Kotlin dagger 2 github

sprite (Sprite) – The sprite to check

Convert world coordinates to cell coordinates

Shortcut to remove and re-add a sprite.

sprite (Sprite) – The sprite to move

sprite (Sprite) – The sprite to remove

Clear all the sprites from the spatial hash.

arcade.check_for_collision#

Check for a collision between two sprites.

True or False depending if the sprites intersect.

arcade.check_for_collision_with_list#

arcade. check_for_collision_with_list ( sprite : SpriteType , sprite_list : SpriteList , method : int = 0 ) → List [ SpriteType ] [source] #

Check for a collision between a sprite, and a list of sprites.

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

List of sprites colliding, or an empty list.

arcade.check_for_collision_with_lists#

arcade. check_for_collision_with_lists ( sprite : SpriteType , sprite_lists : Iterable [ SpriteList ] , method = 1 ) → List [ SpriteType ] [source] #

Check for a collision between a Sprite, and a list of SpriteLists.

  • sprite (Sprite) – Sprite to check
  • sprite_lists (Iterable[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.

List of sprites colliding, or an empty list.

arcade.get_closest_sprite#

arcade. get_closest_sprite ( sprite : SpriteType , sprite_list : SpriteList ) → Tuple [ SpriteType , float ] | None [source] #

Given a Sprite and SpriteList, returns the closest sprite, and its distance.

  • sprite (Sprite) – Target sprite
  • sprite_list (SpriteList) – List to search for closest sprite.

A tuple containing the closest sprite and the minimum distance. If the spritelist is empty we return None .

arcade.get_distance_between_sprites#

arcade. get_distance_between_sprites ( sprite1 : SpriteType , sprite2 : SpriteType ) → float [source] #

Returns the distance between the center of two given sprites

arcade.get_sprites_at_exact_point#

arcade. get_sprites_at_exact_point ( point : Tuple [ float , float ] , sprite_list : SpriteList ) → List [ SpriteType ] [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.

  • point (Point) – Point to check
  • sprite_list (SpriteList) – SpriteList to check against

List of sprites colliding, or an empty list.

arcade.get_sprites_at_point#

arcade. get_sprites_at_point ( point : Tuple [ float , float ] , sprite_list : SpriteList [ SpriteType ] ) → List [ SpriteType ] [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.

  • point (Point) – Point to check
  • sprite_list (SpriteList) – SpriteList to check against

List of sprites colliding, or an empty list.

arcade.get_sprites_in_rect#

arcade. get_sprites_in_rect ( rect : Tuple [ int , int , int , int ] | List [ int ] , sprite_list : SpriteList ) → List [ SpriteType ] [source] #

Get a list of sprites in a particular rectangle. This function sees if any sprite overlaps the specified rectangle. If a sprite has a different center_x/center_y but touches the rectangle, this will return that sprite.

The rectangle is specified as a tuple of (left, right, bottom, top).

  • rect (Rect) – Rectangle to check
  • sprite_list (SpriteList) – SpriteList to check against

List of sprites colliding, or an empty list.

Источник

Оцените статью