Skip to content

player_character

player_character

Module containing the PlayerCharacter class. Use PlayerCharacter to represent a PC the player adds to their party.

Alignment

Bases: Enum

Represents the alignment of a player character (PC) or monster.

PlayerCharacter

PlayerCharacter(name: str, character_class_type: CharacterClassType, level: int = 1, xp_adjustment_percentage: int = 0)

Represents a player character (PC) in the game.

Attributes:

  • name (str) –

    The name of the character.

  • abilities (dict) –

    A dictionary of the character's abilities.

  • character_class (CharacterClass) –

    The character's class.

  • inventory (Inventory) –

    The character's inventory.

  • xp_adjustment_percentage (int) –

    The character's XP adjustment based on the scores of ability their prime requisite(s). This value is set when the character's class is set, or when restoring a saved character.

armor_class property

armor_class

Get the armor class of the character.

is_alive property

is_alive: bool

Returns True if the character is alive.

The character is considered alive if their hit points are greater than 0.

Returns:

  • bool ( bool ) –

    True if the character is alive (hit points > 0), False otherwise.

max_hit_points property

max_hit_points: int

Get the maximum hit points of the character.

xp property

xp: int

Get the character's current XP total.

xp_needed_for_next_level property

xp_needed_for_next_level: int

Get the amount of XP needed for the character to reach the next level.

apply_damage

apply_damage(hit_points_damage: int)

Apply damage to the player character by reducing their hit points by the given amount, down to a minimum of 0.

This method has no affect if the character is already dead.

Parameters:

  • hit_points_damage (int) –

    The amount of damage done to the PC.

get_ability_roll

get_ability_roll()

Rolls a 4d6 and returns the sum of the three highest rolls.

get_attack_roll

get_attack_roll() -> DiceRoll

Roll a 1d20 to hit, add all applicable modifiers, and return the roll.

Returns:

  • DiceRoll ( DiceRoll ) –

    The result of the to hit roll.

get_damage_roll

get_damage_roll() -> DiceRoll

Roll appropriate damage die, add all applicable modifiers, and return the roll.

Returns:

  • DiceRoll ( DiceRoll ) –

    The result of the damage roll.

get_initiative_roll

get_initiative_roll() -> int

Rolls a 1d6, adds the character's Dexterity modifier, and returns the total.

grant_xp

grant_xp(xp: int) -> bool

Grants XP to the character, taking into account their Constitution ability modifier, if any.

If the the character's new XP total is enough to level up, the character's level and associated statistics (including a new "XP neeeded for next level" value) are increased appropriately.

heal

heal(hit_points_healed: int)

Heal the player character by increasing their hit points by the given amount, up to their maximum hit points.

Parameters:

  • hit_points_healed (int) –

    The amount of hit points to heal the PC.

roll_abilities

roll_abilities()

Rolls the ability scores for the character.

roll_hp

roll_hp() -> DiceRoll

Rolls the character's hit dice and applies their Constitution modifier, if any.

The total value of the roll with modifier can be negative after if the roll was low and the character has a negative Constitution modifier. You should clamp the value to 1 before applying it to the character's HP.

Returns:

  • DiceRoll ( DiceRoll ) –

    The result of the HP roll. Value with modifiers can be negative.

save_character

save_character(file_path: str = None) -> str

Saves the character to a JSON file.

Parameters:

  • file_path (str, default: None ) –

    The path where the file will be saved. If None, saves in the default data directory.

Returns:

  • str ( str ) –

    The path where the file was saved.

Raises:

  • OSError

    If the file cannot be written.

set_character_class

set_character_class(character_class_type: CharacterClassType, level: int = 1)

Sets the character class of the character.