Skip to content

Character API

Character API Reference

The Character class renders and animates player characters using state-based animations. It works with the Animation class and is commonly created via CharacterClass.build().

Module Location

from skeletons.character import Character

Overview

Use Character to:

  • Manage animation state (idle, walk, jump, etc.)
  • Update animations each frame
  • Draw sprites with scaling and facing

Constructor

Character(
position: Tuple[float, float],
animations: Dict[str, Animation],
default_state: str = "idle",
scale: float = 1.0,
scale_x: Optional[float] = None,
scale_y: Optional[float] = None,
)

Parameters:

NameTypeDefaultDescription
positionTuple[float, float]-Initial position in pixels
animationsDict[str, Animation]-State → Animation mapping
default_statestr"idle"Initial animation state
scalefloat1.0Uniform scale if scale_x/scale_y not set
scale_xfloatNoneHorizontal scale override
scale_yfloatNoneVertical scale override

Attributes

AttributeTypeDescription
positionpygame.Vector2Current position (top-left)
animationsDict[str, Animation]Animation map
scale_xfloatHorizontal scale
scale_yfloatVertical scale
facingint1 for right, -1 for left
statestrCurrent animation state
animationAnimationActive animation

State Constants

Character.STATE_IDLE
Character.STATE_WALK
Character.STATE_JUMP
Character.STATE_FLY
Character.STATE_LAND

Methods

set_state(name, force=False)

Switch the current animation state.

character.set_state("walk")

Parameters:

NameTypeDefaultDescription
namestr-State name to switch to
forceboolFalseReset animation even if already in state

update_state(is_on_ground, is_moving, is_flying=False, is_landing=False)

Automatically select a state based on movement flags.

character.update_state(is_on_ground=True, is_moving=False)

Parameters:

NameTypeDefaultDescription
is_on_groundbool-True if standing
is_movingbool-True if moving horizontally
is_flyingboolFalseFlying state (optional)
is_landingboolFalseJust landed this frame

update(dt)

Advance the active animation.

character.update(delta_time)

Parameters:

NameTypeDescription
dtfloatDelta time in seconds

get_draw_size()

Get the scaled width/height of the current frame.

Returns: (width, height) in pixels.


set_center(center_pos)

Position the character so its center is at center_pos.

character.set_center(pygame.Vector2(400, 300))

draw(screen)

Draw the current frame with scaling and facing.

character.draw(screen)

load_frames(folder, names, scale=1.0)

Static helper to load frames from a folder.

frames = Character.load_frames(
"assets/sprites/characters/hero",
["walk1.png", "walk2.png"],
scale=0.5,
)

Parameters:

NameTypeDefaultDescription
folderstr-Folder path containing images
namesList[str]-Filenames to load
scalefloat1.0Optional scale factor

Returns: List[pygame.Surface]

Note: Results are cached by (folder, names, scale) to reduce repeated disk reads on network drives.

Usage Example

from skeletons.character import Character, Animation
idle_frames = Character.load_frames("assets/sprites/hero", ["idle.png"])
walk_frames = Character.load_frames("assets/sprites/hero", ["walk1.png", "walk2.png"])
animations = {
"idle": Animation(idle_frames, fps=1, loop=True),
"walk": Animation(walk_frames, fps=12, loop=True),
}
character = Character((100, 100), animations, default_state="idle", scale=0.5)
# In game loop
character.update_state(is_on_ground=True, is_moving=True)
character.update(dt)
character.draw(screen)