Skip to main content

make Dungeons & Dragons characters as serializable objects

Project description

dnd-character

A Python library to make 5e Dungeons & Dragons characters for use in another app. Characters are serializable into Python dicts so they can be saved and loaded however you wish.

SRD rules are fetched from the 5e SRD API the first time they're requested, then the JSON is cached locally for faster retrieval in the future. I've included the json_cache containing the SRD inside the repo in case this API changes, but when the API does change I will update this library. So please pin your version if you want to avoid any breaking changes.

Installation and Use

  1. Install from PyPI using pip install dnd-character
  2. See example.py for example code on how to use the library.

Licenses

The software is EPL-2.0 and the text for this license is in LICENSE as is standard for software. Originally forked from PyDnD. The contents of dnd_character/json_cache are retrieved from 5e-srd-api, and are covered by the Open Game License. See dnd_character/json_cache/OGLv1.0a.txt for details.

Example Code

Creating Characters and Monsters

The classes module has functions for creating all 12 classes from the System Reference Document. The monsters module has a dictionary of monsters, which are dictionaries themselves.

from dnd_character.classes import Bard
from dnd_character.monsters import SRD_monsters
from random import randint

brianna = Bard(
    name="Brianna",
    level=10,
    )
zombie = SRD_monsters["zombie"]
attack_bonus = zombie["actions"][0]["attack_bonus"]
# Zombie rolls a d20 to attack a Bard
if randint(1, 20) + attack_bonus => brianna.armour_class:
    print(f"{brianna.name} was hit by {zombie['name']}!")
else:
    print(f"{brianna.name} bravely dodged the attack")

Leveling and Experience

The library should help leveling up characters automatically if you manage the Character's experience attribute. It's simpler to avoid modifying the level directly.

import dnd_character
thor = Character(name="Thor")
assert thor.level == 1
thor.experience += 1000
assert thor.level == 3
assert thor.experience.to_next_level == 1700
thor.experience += thor.experience.to_next_level
assert thor.level == 4

Starting Equipment

Characters initialized with a class will have the starting equipment of that class, and an attribute called player_options which lists the optional starting equipment.

from dnd_character.classes import Paladin
from pprint import pprint
sturm = Paladin(dexterity=10)
pprint(sturm.inventory)
print(sturm.armour_class)
# Remove Chain Mail
sturm.removeItem(sturm.inventory[0])
print(sturm.armour_class)
# New Item
from dnd_character.equipment import SRD_equipment
dragonlance = SRD_equipment['lance']
dragonlance["name"] = "Dragonlance®"
sturm.giveItem(dragonlance)
# View optional starting equipment
pprint(sturm.player_options)

Using Spells

Support for spells is not super great at the moment. Characters have dictionaries like spells_known and cantrips_known in which you're expected to store dictionaries from SRD_spells... but there's no useful help from the library here. Yet!

from dnd_character.spellcasting import SRD_spells, spells_for_class_level
from pprint import pprint
cantrips = spells_for_class_level('wizard', 0)
print(f"Cantrips available to a Wizard: {', '.join(cantrips)}")
for spell in cantrips:
    print(f"{SRD_spells[spell]['name']}:")
    pprint(SRD_spells[spell])
    break

Character Object

Normal initialization arguments for a Character object:

name         (str)
age          (str)
gender       (str)
alignment    (str): character's two letter alignment
description  (str): physical description of player character
biography    (str): backstory of player character	
level        (int): starting level
wealth       (int): starting wealth	
strength     (int)
dexterity    (int)
constitution (int)
wisdom       (int)
intelligence (int)
charisma     (int)
hp           (int):
classs      (dict): JSON returned from the 5e API -- dnd_character.SRD.SRD_classes["bard"]

In addition, the Character object can receive attributes that are normally set automatically, such as the UUID. This is for re-loading the objects from serialized data (via Character(**characterData)) and probably aren't arguments you would write manually into your code.

Contributing

Please feel free to open a Pull Request on GitHub! I would be happy to help merge any contributions no matter your skill level.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dnd_character-20.11.20.tar.gz (458.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dnd_character-20.11.20-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file dnd_character-20.11.20.tar.gz.

File metadata

  • Download URL: dnd_character-20.11.20.tar.gz
  • Upload date:
  • Size: 458.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

File hashes

Hashes for dnd_character-20.11.20.tar.gz
Algorithm Hash digest
SHA256 6d85c8f145037fca7ab687c701501fc863b0204eb43d47b039df027df5294db6
MD5 e43a2bd566759a4f7109793e4b16d134
BLAKE2b-256 89c8d7ed4fe1c3f3375dac93d97a9f5218b34460b967e6651dfde21273275b9d

See more details on using hashes here.

File details

Details for the file dnd_character-20.11.20-py3-none-any.whl.

File metadata

  • Download URL: dnd_character-20.11.20-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

File hashes

Hashes for dnd_character-20.11.20-py3-none-any.whl
Algorithm Hash digest
SHA256 db182ea10089115a9fdb3a81c6c2ee8339d50f9a91a1b4e8900f594f3ce629f2
MD5 fb4f51561834a0c20d82e51c8f960df2
BLAKE2b-256 70da8b42e8e79fc81927adc1db23ab03c63e094768fdf51a8ded6ee842d41df9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page