Sprites

arcade.AnimatedTimeBasedSprite

class arcade.AnimatedTimeBasedSprite(filename: Optional[str] = None, scale: float = 1, image_x: float = 0, image_y: float = 0, image_width: float = 0, image_height: float = 0, center_x: float = 0, center_y: float = 0, _repeat_count_x=1, _repeat_count_y=1)[source]

Sprite for platformer games that supports animations. These can be automatically created by the Tiled Map Editor.

update_animation(delta_time: float = 0.016666666666666666)[source]

Logic for selecting the proper texture to use.

arcade.AnimatedWalkingSprite

class arcade.AnimatedWalkingSprite(scale: float = 1, image_x: float = 0, image_y: float = 0, center_x: float = 0, center_y: float = 0)[source]

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 - Add Character Animations, and Better Keyboard Control.

update_animation(delta_time: float = 0.016666666666666666)[source]

Logic for selecting the proper texture to use.

arcade.AnimationKeyframe

class arcade.AnimationKeyframe(tile_id: int, duration: int, texture: arcade.texture.Texture)[source]

Used in animated sprites.

arcade.PyMunk

class arcade.PyMunk[source]

Object used to hold pymunk info for a sprite.

arcade.Sprite

class arcade.Sprite(filename: Optional[str] = None, scale: float = 1, image_x: float = 0, image_y: float = 0, image_width: float = 0, image_height: float = 0, center_x: float = 0, center_y: float = 0, repeat_count_x: int = 1, repeat_count_y: int = 1, flipped_horizontally: bool = False, flipped_vertically: bool = False, flipped_diagonally: bool = False, hit_box_algorithm: str = 'Simple', hit_box_detail: float = 4.5, texture: Optional[arcade.texture.Texture] = None, angle: float = 0)[source]

Class that represents a ‘sprite’ on-screen. Most games center around sprites. For examples on how to use this class, see: https://api.arcade.academy/en/latest/examples/index.html#sprites

Parameters
  • filename (str) – Filename of an image that represents the sprite.

  • scale (float) – Scale the image up or down. Scale of 1.0 is none.

  • image_x (float) – X offset to sprite within sprite sheet.

  • image_y (float) – Y offset to sprite within sprite sheet.

  • image_width (float) – Width of the sprite

  • image_height (float) – Height of the sprite

  • center_x (float) – Location of the sprite

  • center_y (float) – Location of the sprite

  • flipped_horizontally (bool) – Mirror the sprite image. Flip left/right across vertical axis.

  • flipped_vertically (bool) – Flip the image up/down across the horizontal axis.

  • flipped_diagonally (bool) – Transpose the image, flip it across the diagonal.

  • hit_box_algorithm (str) – One of ‘None’, ‘Simple’ or ‘Detailed’. Defaults to ‘Simple’. Use ‘Simple’ for the PhysicsEngineSimple, PhysicsEnginePlatformer and ‘Detailed’ for the PymunkPhysicsEngine.

  • texture (Texture) – Specify the texture directly.

  • angle (float) – The initial rotation of the sprite in degrees

This will ignore all hit box and image size arguments.

../_images/hit_box_algorithm_none.png

hit_box_algorithm = “None”

../_images/hit_box_algorithm_simple.png

hit_box_algorithm = “Simple”

../_images/hit_box_algorithm_detailed.png

hit_box_algorithm = “Detailed”

Parameters

hit_box_detail (float) – Float, defaults to 4.5. Used with ‘Detailed’ to hit box

Attributes:
alpha

Transparency of sprite. 0 is invisible, 255 is opaque.

angle

Rotation angle in degrees.

radians

Rotation angle in radians.

bottom

Set/query the sprite location by using the bottom coordinate. This will be the ‘y’ of the bottom of the sprite.

boundary_left

Used in movement. Left boundary of moving sprite.

boundary_right

Used in movement. Right boundary of moving sprite.

boundary_top

Used in movement. Top boundary of moving sprite.

boundary_bottom

Used in movement. Bottom boundary of moving sprite.

center_x

X location of the center of the sprite

center_y

Y location of the center of the sprite

change_x

Movement vector, in the x direction.

change_y

Movement vector, in the y direction.

change_angle

Change in rotation.

color

Color tint the sprite

collision_radius

Used as a fast-check to see if this item is close enough to another item. If this check works, we do a slower more accurate check. You probably don’t want to use this field. Instead, set points in the hit box.

cur_texture_index

Index of current texture being used.

guid

Unique identifier for the sprite. Useful when debugging.

height

Height of the sprite.

force

Force being applied to the sprite. Useful when used with Pymunk for physics.

left

Set/query the sprite location by using the left coordinate. This will be the ‘x’ of the left of the sprite.

points

Points, in relation to the center of the sprite, that are used for collision detection. Arcade defaults to creating points for a rectangle that encompass the image. If you are creating a ramp or making better hit-boxes, you can custom-set these.

position

A list with the (x, y) of where the sprite is.

right

Set/query the sprite location by using the right coordinate. This will be the ‘y=x’ of the right of the sprite.

sprite_lists

List of all the sprite lists this sprite is part of.

texture

Texture class with the current texture.

textures

List of textures associated with this sprite.

top

Set/query the sprite location by using the top coordinate. This will be the ‘y’ of the top of the sprite.

scale

Scale the image up or down. Scale of 1.0 is original size, 0.5 is 1/2 height and width.

velocity

Change in x, y expressed as a list. (0, 0) would be not moving.

width

Width of the sprite

It is common to over-ride the update method and provide mechanics on movement or other sprite updates.

add_spatial_hashes()[source]

Add spatial hashes for this sprite in all the sprite lists it is part of.

property alpha: int

Return the alpha associated with the sprite.

property angle: float

Get the angle of the sprite’s rotation.

append_texture(texture: arcade.texture.Texture)[source]

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

Parameters

texture (arcade.Texture) – Texture to add ot the list of available textures

property bottom: float

Return the y coordinate of the bottom of the sprite.

property center_x: float

Get the center x coordinate of the sprite.

property center_y: float

Get the center y coordinate of the sprite.

property change_x: float

Get the velocity in the x plane of the sprite.

property change_y: float

Get the velocity in the y plane of the sprite.

clear_spatial_hashes()[source]

Search the sprite lists this sprite is a part of, and remove it from any spatial hashes it is a part of.

collides_with_list(sprite_list: SpriteList) list[source]

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

Parameters

sprite_list (SpriteList) – SpriteList to check against

Returns

SpriteList of all overlapping Sprites from the original SpriteList

Return type

SpriteList

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

Check if point is within the current sprite.

Parameters

point (Point) – Point to check.

Returns

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

Return type

bool

collides_with_sprite(other: arcade.sprite.Sprite) bool[source]

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

Parameters

other (Sprite) – the other sprite to check against.

Returns

True or False, whether or not they are overlapping.

Return type

bool

property collision_radius

Get the collision radius.

Note

Final collision checking is done via geometry that was set in get_points/set_points. These points are used in the check_for_collision function. This collision_radius variable is used as a “pre-check.” We do a super-fast check with collision_radius and see if the sprites are close. If they are, then we look at the geometry and figure if they really are colliding.

property color: Union[Tuple[int, int, int], List[int]]

Return the RGB color associated with the sprite.

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

Draw the sprite.

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’

draw_hit_box(color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]] = (0, 0, 0), line_thickness: float = 1)[source]

Draw a sprite’s hit-box.

The ‘hit box’ drawing is cached, so if you change the color/line thickness later, it won’t take.

Parameters
  • color – Color of box

  • line_thickness – How thick the box should be

forward(speed: float = 1.0)[source]

Set a Sprite’s position to speed by its angle :param speed: speed factor

get_adjusted_hit_box() Sequence[Union[Tuple[float, float], List[float]]][source]

Get the points that make up the hit box for the rect that makes up the sprite, including rotation and scaling.

get_hit_box() Sequence[Union[Tuple[float, float], List[float]]][source]

Use the hit_box property to get or set a sprite’s hit box. Hit boxes are specified assuming the sprite’s center is at (0, 0). Specify hit boxes like:

mySprite.hit_box = [[-10, -10], [10, -10], [10, 10]]

Specify a hit box unadjusted for translation, rotation, or scale. You can get an adjusted hit box with arcade.Sprite.get_adjusted_hit_box.

property height: float

Get the height in pixels of the sprite.

property hit_box: Sequence[Union[Tuple[float, float], List[float]]]

Use the hit_box property to get or set a sprite’s hit box. Hit boxes are specified assuming the sprite’s center is at (0, 0). Specify hit boxes like:

mySprite.hit_box = [[-10, -10], [10, -10], [10, 10]]

Specify a hit box unadjusted for translation, rotation, or scale. You can get an adjusted hit box with arcade.Sprite.get_adjusted_hit_box.

kill()[source]

Alias of remove_from_sprite_lists

property left: float

Return the x coordinate of the left-side of the sprite’s hit box.

on_update(delta_time: float = 0.016666666666666666)[source]

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

property position: Union[Tuple[float, float], List[float]]

Get the center x and y coordinates of the sprite.

Returns:

(center_x, center_y)

property properties: Dict[str, Any]

Get or set custom sprite properties.

Return type

Dict[str, Any]

property pymunk: arcade.sprite.PyMunk

Get or set the Pymunk property objects. This is used by the pymunk physics engine.

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

Called by the pymunk physics engine if this sprite moves.

property radians: float

Converts the degrees representation of self.angle into radians. :return: float

register_physics_engine(physics_engine)[source]

Called by the Pymunk physics engine when this sprite is added to that physics engine. Lets the sprite know about the engine and remove itself if it gets deleted.

register_sprite_list(new_list: SpriteList)[source]

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

remove_from_sprite_lists()[source]

Remove the sprite from all sprite lists.

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

Rescale the sprite relative to a different point than its center.

reverse(speed: float = 1.0)[source]

Set a new speed, but in reverse. :param speed: speed factor

property right: float

Return the x coordinate of the right-side of the sprite’s hit box.

property scale: float

Get the scale of the sprite.

set_hit_box(points: Sequence[Union[Tuple[float, float], List[float]]])[source]

Set a sprite’s hit box. Hit box should be relative to a sprite’s center, and with a scale of 1.0. Points will be scaled with get_adjusted_hit_box.

set_position(center_x: float, center_y: float)[source]

Set a sprite’s position

Parameters
  • center_x (float) – New x position of sprite

  • center_y (float) – New y position of sprite

set_texture(texture_no: int)[source]

Sets texture by texture id. Should be renamed because it takes a number rather than a texture, but keeping this for backwards compatibility.

stop()[source]

Stop the Sprite’s motion

strafe(speed: float = 1.0)[source]

Set a sprites position perpendicular to its angle by speed :param speed: speed factor

property top: float

Return the y coordinate of the top of the sprite.

turn_left(theta: float = 90)[source]

Rotate the sprite left a certain number of degrees. :param theta: change in angle

turn_right(theta: float = 90)[source]

Rotate the sprite right a certain number of degrees. :param theta: change in angle

update()[source]

Update the sprite.

update_animation(delta_time: float = 0.016666666666666666)[source]

Override this to add code that will change what image is shown, so the sprite can be animated.

Parameters

delta_time (float) – Time since last update.

property visible: bool

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
Return type

bool

property width: float

Get the width of the sprite.

arcade.SpriteCircle

class arcade.SpriteCircle(radius: int, color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]], soft: bool = False)[source]

This sprite is just an elliptical sprite of one solid color. No need to use an image file.

Parameters
  • radius (float) – Radius of the circle

  • color (Color) – Color of the circle

  • soft (bool) – If True, will add a alpha gradient

arcade.SpriteSolidColor

class arcade.SpriteSolidColor(width: int, height: int, color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]])[source]

This sprite is just a rectangular sprite of one solid color. No need to use an image file.

Parameters
  • width (int) – Width of the sprite

  • height (int) – Height of the sprite

  • color (Color) – Color of the sprite

arcade.get_distance_between_sprites

arcade.get_distance_between_sprites(sprite1: arcade.sprite.Sprite, sprite2: arcade.sprite.Sprite) float[source]

Returns the distance between the center of two given sprites

Parameters
  • sprite1 (Sprite) – Sprite one

  • sprite2 (Sprite) – Sprite two

Returns

Distance

Return type

float

arcade.load_animated_gif

arcade.load_animated_gif(resource_name)[source]

Given an animated gif, return a AnimatedTimeBasedSprite.

Support for transparency in animated gifs in Python is lacking. There are a lot of older animated gifs that are saved weird. The end result is that the often the first frame of an animated gif is the only frame that we correctly get the transparency on. Until the Pillow library better handles this, loading animated gifs will be pretty buggy.