Sprites
Contents
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.
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.
arcade.AnimationKeyframe
- class arcade.AnimationKeyframe(tile_id: int, duration: int, texture: arcade.texture.Texture)[source]
Used in animated sprites.
arcade.PyMunk
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 thePymunkPhysicsEngine
.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.
- 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.
- 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
- 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
- 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
- collides_with_sprite(other: arcade.sprite.Sprite) bool [source]
Will check if a sprite is overlapping (colliding) another Sprite.
- 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.
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_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 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
.
- 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.
- 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.
- 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_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.
- strafe(speed: float = 1.0)[source]
Set a sprites position perpendicular to its angle by speed :param speed: speed factor
- 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_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
arcade.SpriteCircle
arcade.SpriteSolidColor
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
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.