Sprites#

class arcade.PyMunk[source]#

Bases:

Object used to hold pymunk info for a sprite.

damping#
gravity#
max_horizontal_velocity#
max_velocity#
max_vertical_velocity#
class arcade.PymunkMixin[source]#

Bases:

pymunk_moved(physics_engine, dx, dy, d_angle)[source]#

Called by the pymunk physics engine if this sprite moves.

class arcade.SpriteCircle(radius: int, color: Tuple[int, int, int, int], soft: bool = False, **kwargs)[source]#

Bases: Sprite

A circle of the specified radius.

The texture is automatically generated instead of loaded from a file.

There may be a stutter the first time a combination of radius, color, and soft is used due to texture generation. All subsequent calls for the same combination will run faster because they will re-use the texture generated earlier.

For a gradient fill instead of a solid color, set soft to True. The circle will fade from an opaque center to transparent at the edges.

Parameters:
  • radius – Radius of the circle in pixels

  • color – The Color of the sprite as an RGB or RGBA tuple

  • soft – If True, the circle will fade from an opaque center to transparent edges.

boundary_bottom: float | None#
boundary_left: float | None#
boundary_right: float | None#
boundary_top: float | None#
change_angle: float#
cur_texture_index: int#
force#
guid: str | None#
physics_engines: List[Any]#
textures: List[Texture]#
class arcade.SpriteSolidColor(width: int, height: int, center_x: float = 0, center_y: float = 0, color: Tuple[int, int, int, int] = (255, 255, 255, 255), angle: float = 0, **kwargs)[source]#

Bases: Sprite

A rectangular sprite of the given width, height, and color.

The texture is automatically generated instead of loaded from a file. Internally only a single global texture is used for this sprite type, so concerns about memory usage non-existent regardless of size or number of sprite variations.

Parameters:
  • width – Width of the sprite in pixels

  • height – Height of the sprite in pixels

  • center_x – Initial x position of the sprite

  • center_y – Initial y position of the sprite

  • color – The color of the sprite as a Color, an RGBA tuple, or an RGB tuple.

  • angle – Initial angle of the sprite in degrees

class arcade.AnimatedWalkingSprite(scale: float = 1.0, center_x: float = 0.0, center_y: float = 0.0, **kwargs)[source]#

Bases: Sprite

Deprecated Sprite for platformer games that supports walking animations. Make sure to call update_animation after loading the animations so the initial texture can be set. Or manually set it.

It is highly recommended you create your own version of this class rather than try to use this pre-packaged one.

For an example, see this section of the platformer tutorial: Step 12 - Loading a Map From a Map Editor.

Parameters:
  • scale – Initial scale of the sprite.

  • center_x – Initial x position of the sprite.

  • center_y – Initial y position of the sprite.

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

Logic for texture animation.

Parameters:

delta_time – Time since last update.

boundary_bottom: float | None#
boundary_left: float | None#
boundary_right: float | None#
boundary_top: float | None#
change_angle: float#
cur_texture_index: int#
force#
guid: str | None#
physics_engines: List[Any]#
textures: List[Texture]#
class arcade.TextureAnimation(keyframes: List[TextureKeyframe] | None = None)[source]#

Bases:

Animation class that holds a list of keyframes. The animation should not store any state related to the current time so it can be shared between multiple sprites.

Parameters:
  • keyframes – List of keyframes for the animation.

  • loop – If the animation should loop.

append_keyframe(keyframe: TextureKeyframe) None[source]#

Add a keyframe to the animation.

Parameters:

keyframe – Keyframe to add.

get_keyframe(time: float, loop: bool = True) Tuple[int, TextureKeyframe][source]#

Get the frame at a given time.

Parameters:
  • time – Time in seconds.

  • loop – If the animation should loop.

Returns:

Tuple of frame index and keyframe.

remove_keyframe(index: int) None[source]#

Remove a keyframe from the animation.

Parameters:

index – Index of the keyframe to remove.

duration_ms#

Total duration of the animation in milliseconds.

duration_seconds#

Total duration of the animation in seconds.

keyframes#

A tuple of keyframes in the animation. Keyframes should not be modified directly.

num_frames#

Number of frames in the animation.

class arcade.TextureAnimationSprite(center_x: float = 0.0, center_y: float = 0.0, scale: float = 1.0, animation: TextureAnimation | None = None, **kwargs)[source]#

Bases: Sprite

Animated sprite based on keyframes. Primarily used internally by tilemaps.

Parameters:
  • path_or_texture – Path to the image file, or a Texture object.

  • center_x – Initial x position of the sprite.

  • center_y – Initial y position of the sprite.

  • scale – Initial scale of the sprite.

update_animation(delta_time: float = 0.016666666666666666, **kwargs) None[source]#

Logic for updating the animation.

Parameters:

delta_time – Time since last update.

animation#

Animation object for this sprite.

boundary_bottom: float | None#
boundary_left: float | None#
boundary_right: float | None#
boundary_top: float | None#
change_angle: float#
cur_texture_index: int#
force#
guid: str | None#
physics_engines: List[Any]#
textures: List[Texture]#
time#

Get or set the current time of the animation in seconds.

class arcade.TextureKeyframe(texture: Texture, duration: int = 100, tile_id: int | None = 0, **kwargs)[source]#

Bases:

Keyframe for texture animations.

Parameters:
  • texture – Texture to display for this keyframe.

  • duration – Duration in milliseconds to display this keyframe.

  • tile_id – Tile ID for this keyframe (only used for tiled maps)

duration#

Duration in milliseconds to display this keyframe.

texture#

The texture to display for this keyframe.

tile_id#

Tile ID for this keyframe (only used for tiled maps)

class arcade.Sprite(path_or_texture: str | Path | Texture | None = None, scale: float = 1.0, center_x: float = 0.0, center_y: float = 0.0, angle: float = 0.0, **kwargs: Any)[source]#

Bases: BasicSprite, PymunkMixin

Sprites are used to render image data to the screen & perform collisions.

Most games center around sprites. They are most frequently used as follows:

  1. Create Sprite instances from image data

  2. Add the sprites to a SpriteList instance

  3. Call SpriteList.draw() on the instance inside your on_draw method.

For runnable examples of how to do this, please see arcade’s built-in Sprite examples.

Tip

Advanced users should see BasicSprite

It uses fewer resources at the cost of having fewer features.

Parameters:
  • path_or_texture – Path to an image file, or a texture object.

  • center_x – Location of the sprite in pixels.

  • center_y – Location of the sprite in pixels.

  • scale – Show the image at this many times its original size.

  • angle – The initial rotation of the sprite in degrees

append_texture(texture: Texture)[source]#

Appends a new texture to the list of textures that can be applied to this sprite.

Parameters:

texture – Texture to add to the list of available textures

draw(*, filter: Tuple[int, int] | None = None, pixelated: bool | None = None, blend_function: Tuple[int, int] | Tuple[int, int, int, int] | None = None) None[source]#

A debug method which draws the sprite into the current OpenGL context.

Warning

You are probably looking for SpriteList.draw()!

Drawing individual sprites is slow compared to using SpriteList. See Why SpriteLists? for more information.

This method should not be relied on. It may be removed one day.

Parameters:
  • filter – Optional parameter to set OpenGL filter, such as gl.GL_NEAREST to avoid smoothing.

  • pixelatedTrue 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’

forward(speed: float = 1.0) None[source]#

Adjusts a Sprites forward.

Parameters:

speed – speed

register_physics_engine(physics_engine: Any) None[source]#

Register a physics engine on the sprite. This is only needed if you actually need a reference to your physics engine in the sprite itself. It has no other purposes.

The registered physics engines can be accessed through the physics_engines attribute.

It can for example be the pymunk physics engine or a custom one you made.

remove_from_sprite_lists() None[source]#

Remove this sprite from all sprite lists it is in including registered physics engines.

reverse(speed: float = 1.0) None[source]#

Adjusts a Sprite backwards.

Parameters:

speed – speed

set_texture(texture_no: int) None[source]#

Set the current texture by texture number. The number is the index into self.textures.

Parameters:

texture_no – Index into self.textures

stop() None[source]#

Stop the Sprite’s motion by setting the velocity and angle change to 0.

strafe(speed: float = 1.0) None[source]#

Adjusts a Sprite sideways.

Parameters:

speed – speed

sync_hit_box_to_texture()[source]#

Update the sprite’s hit box to match the current texture’s hit box.

turn_left(theta: float = 90.0) None[source]#

Rotate the sprite left by the passed number of degrees.

Parameters:

theta – change in angle, in degrees

turn_right(theta: float = 90.0) None[source]#

Rotate the sprite right by the passed number of degrees.

Parameters:

theta – change in angle, in degrees

update() None[source]#

The default update method for a Sprite. Can be overridden by a subclass.

This method moves the sprite based on its velocity and angle change.

update_spatial_hash() None[source]#

Update the sprites location in the spatial hash.

angle#

Get or set the rotation or the sprite.

The value is in degrees and is clockwise.

boundary_bottom: float | None#
boundary_left: float | None#
boundary_right: float | None#
boundary_top: float | None#
change_angle: float#
change_x#

Get or set the velocity in the x plane of the sprite.

change_y#

Get or set the velocity in the y plane of the sprite.

cur_texture_index: int#
force#
guid: str | None#
hit_box#

Get or set the hit box for this sprite.

physics_engines: List[Any]#
properties#

Get or set custom sprite properties.

radians#

Get or set the rotation of the sprite in radians.

The value is in radians and is clockwise.

texture#

Get or set the active texture for this sprite

textures: List[Texture]#
velocity#

Get or set the velocity of the sprite.

The x and y velocity can also be set separately using the sprite.change_x and sprite.change_y properties.

Example:

sprite.velocity = 1.0, 0.0
Returns:

Tuple[float, float]

class arcade.BasicSprite(texture: Texture, scale: float = 1.0, center_x: float = 0, center_y: float = 0, **kwargs: Any)[source]#

Bases:

The absolute minimum needed for a sprite.

It does not support features like rotation or changing the hitbox after creation. For more built-in features, please see Sprite.

Parameters:
  • texture – The texture data to use for this sprite.

  • scale – The scaling factor for drawing the texture.

  • center_x – Location of the sprite along the X axis in pixels.

  • center_y – Location of the sprite along the Y axis in pixels.

collides_with_list(sprite_list: SpriteList) List[SpriteType][source]#

Check if current sprite is overlapping with any other sprite in a list

Parameters:

sprite_list – SpriteList to check against

Returns:

List of all overlapping Sprites from the original SpriteList

collides_with_point(point: Tuple[float, float]) bool[source]#

Check if point is within the current sprite.

Parameters:

point – Point to check.

Returns:

True if the point is contained within the sprite’s boundary.

collides_with_sprite(other: SpriteType) bool[source]#

Will check if a sprite is overlapping (colliding) another Sprite.

Parameters:

other – the other sprite to check against.

Returns:

True or False, whether or not they are overlapping.

draw_hit_box(color: Tuple[int, int, int, int] = (0, 0, 0, 255), line_thickness: float = 2.0) None[source]#

Draw a sprite’s hit-box. This is useful for debugging.

Parameters:
  • color – Color of box

  • line_thickness – How thick the box should be

kill() None[source]#

Alias of remove_from_sprite_lists().

on_update(delta_time: float = 0.016666666666666666) None[source]#

Update the sprite. Similar to update, but also takes a delta-time. It can be called manually or by the SpriteList’s on_update method.

Parameters:

delta_time – Time since last update.

register_sprite_list(new_list: SpriteList) None[source]#

Register this sprite as belonging to a list. We will automatically remove ourselves from the list when kill() is called.

remove_from_sprite_lists() None[source]#

Remove the sprite from all sprite lists.

rescale_relative_to_point(point: Tuple[float, float], factor: float) None[source]#

Rescale the sprite and its distance from the passed point.

This function does two things:

  1. Multiply both values in the sprite’s scale_xy value by factor.

  2. Scale the distance between the sprite and point by factor.

If point equals the sprite’s position, the distance will be zero and the sprite will not move.

Parameters:
  • point – The reference point for rescaling.

  • factor – Multiplier for sprite scale & distance to point.

Returns:

rescale_xy_relative_to_point(point: Tuple[float, float], factors_xy: Iterable[float]) None[source]#

Rescale the sprite and its distance from the passed point.

This method can scale by different amounts on each axis. To scale along only one axis, set the other axis to 1.0 in factors_xy.

Internally, this function does the following:

  1. Multiply the x & y of the sprite’s scale_xy attribute by the corresponding part from factors_xy.

  2. Scale the x & y of the difference between the sprite’s position and point by the corresponding component from factors_xy.

If point equals the sprite’s position, the distance will be zero and the sprite will not move.

Parameters:
  • point – The reference point for rescaling.

  • factors_xy – A 2-length iterable containing x and y multipliers for scale & distance to point.

Returns:

update() None[source]#

Generic update method. It can be called manually or by the SpriteList’s update method.

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

Generic update animation method. Usually involves changing the active texture on the sprite.

This can be called manually or by the SpriteList’s update_animation method.

Parameters:

delta_time – Time since last update.

update_spatial_hash() None[source]#

Update the sprites location in the spatial hash if present.

alpha#

Get or set the alpha value of the sprite

bottom#

The lowest y coordinate in the hit box.

When setting this property the sprite is positioned relative to the lowest y coordinate in the hit box.

center_x#

Get or set the center x position of the sprite.

center_y#

Get or set the center y position of the sprite.

color#

Get or set the RGBA multiply color for the sprite.

When setting the color, it 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

Example usage:

>>> print(sprite.color)
Color(255, 255, 255, 255)

>>> sprite.color = arcade.color.RED

>>> sprite.color = 255, 0, 0

>>> sprite.color = 255, 0, 0, 128
depth#

Get or set the depth of the sprite.

This is really the z coordinate of the sprite and can be used with OpenGL depth testing with opaque sprites.

height#

Get or set the height of the sprite in pixels.

hit_box#
left#

The leftmost x coordinate in the hit box.

When setting this property the sprite is positioned relative to the leftmost x coordinate in the hit box.

position#

Get or set the center x and y position of the sprite.

Returns:

(center_x, center_y)

right#

The rightmost x coordinate in the hit box.

When setting this property the sprite is positioned relative to the rightmost x coordinate in the hit box.

scale#

Get or set the sprite’s x scale value or set both x & y scale to the same value.

Note

Negative values are supported. They will flip & mirror the sprite.

scale_xy#

Get or set the x & y scale of the sprite as a pair of values.

sprite_lists: List['SpriteList']#
texture#

Get or set the visible texture for this sprite This property can be changed over time to animate a sprite.

Note that this doesn’t change the hit box of the sprite.

top#

The highest y coordinate in the hit box.

When setting this property the sprite is positioned relative to the highest y coordinate in the hit box.

visible#

Get or set the visibility of this sprite. This is a shortcut for changing the alpha value of a sprite to 0 or 255:

# Make the sprite invisible
sprite.visible = False
# Change back to visible
sprite.visible = True
# Toggle visible
sprite.visible = not sprite.visible
width#

Get or set width or the sprite in pixels

arcade.load_animated_gif(resource_name) TextureAnimationSprite[source]#

Attempt to load an animated GIF as an TextureAnimationSprite.

Many older GIFs will load with incorrect transparency for every frame but the first. Until the Pillow library handles the quirks of the format better, loading animated GIFs will be pretty buggy. A good workaround is loading GIFs in another program and exporting them as PNGs, either as sprite sheets or a frame per file.