charforge
A small, generic Python library for building 2D character-matrix games.
charforge provides reusable building blocks for games whose levels are
described by a matrix of single characters: a Matrix engine, a configurable
Tile system, a generic Entity class, collision and movement helpers, a tiny
event bus, an interaction layer, modular renderers and a plugin system.
Everything is tied together by the World class.
The library is pure Python, has no required third-party dependency, runs on Python 3.10+ and works on Linux, macOS and Windows.
It is intentionally genre-agnostic: it is not a RPG engine, nor a platformer engine, nor a roguelike engine. It is a toolbox from which you can build any of these.
Table of contents
- Goals
- Features
- Installation
- Quick start
- A complete example
- Main API
- Architecture
- Examples
- Tests
- License
Goals
- Simplicity first. A beginner should be able to build a playable mini-game in a few minutes by editing a matrix and a handful of parameters.
- Generic, not opinionated. The library does not assume a genre. Build a roguelike, a puzzle game, a turn-based tactical game, a Sokoban-like, etc.
- Modular. Every system (collisions, movement, rendering, events, interactions, plugins) can be swapped, extended or replaced.
- No required dependencies. Pure Python, standard library only.
- Type-safe and well-documented. Modern type hints, full docstrings, comprehensive test suite.
Features
- Matrix engine – build matrices from strings or lists, mutate them, search, replace, resize, copy, compare, serialise to JSON and load from text files.
- Tile system – describe the meaning of each character (solid, walkable, category, color, arbitrary metadata).
- Entity system – lightweight, subclassable entities with position,
symbol, properties, active state and an
update()hook. - Collision system – composable strategies (bounds, tiles, entities, callable, none); write your own by implementing a single method.
- Movement system – cardinal directions, multi-step moves with intermediate collision checks, teleport, absolute moves, event emission.
- World – the central hub that ties everything together with a clean API.
- Event system – pub/sub event bus with
move,collision,tile_enter,tile_leave,interact,entity_added,entity_removed,map_changedevents and a*wildcard. - Interaction helpers – entity-at, entities-in-zone, line-of-sight (Bresenham), adjacent entities, etc.
- Rendering – ASCII renderer, ANSI-color renderer, layered renderer with overlays; the architecture makes adding a graphical backend trivial.
- Plugin system – register, enable, disable and detach plugins that can install tiles, listeners, collision strategies or anything else.
- Optional game loop – a tiny blocking loop for quick prototyping.
Installation
From PyPI (once published):
pip install charforge
From source (development):
git clone https://github.com/charforge/charforge.git
cd charforge
pip install -e ".[dev]"
Quick start
from charforge import Matrix, World, Entity, Tile
matrix = Matrix([
"##########",
"#........#",
"#..P.....#",
"#........#",
"#.....E..#",
"##########",
])
world = World(matrix)
world.register_tile("#", Tile(solid=True))
world.register_tile(".", Tile(solid=False))
# spawn() creates entities from matrix markers and clears the markers.
players = world.spawn("P", name="player")
enemies = world.spawn("E", name="enemy", solid=True)
player = players[0]
print(world.render())
world.move(player, "right")
print(world.render())
Output:
##########
#........#
#..P.....#
#........#
#.....E..#
##########
##########
#........#
#...P....#
#........#
#.....E..#
##########
A complete example
A small "collect the coins" mini-game in 25 lines:
from charforge import Matrix, World, Entity, Tile
level = [
"###########",
"#..C......#",
"#.........#",
"#....P....#",
"#.........#",
"#......C..#",
"###########",
]
world = World(Matrix(level))
world.register_tile("#", Tile(name="wall", solid=True, color="blue"))
world.register_tile(".", Tile(name="floor"))
world.register_tile("C", Tile(name="coin")) # just metadata, not solid
player = world.spawn("P", name="player")[0]
coins = world.spawn("C", name="coin", background=".")
remaining = len(coins)
@world.on("tile_enter")
def on_enter(ev):
global remaining
if ev.tile.name == "coin":
# Pick up the coin: replace it with floor.
world.set(ev.x, ev.y, ".")
remaining -= 1
print(f"Coin collected! {remaining} remaining.")
if remaining == 0:
print("You win!")
# Move the player around to collect the coins.
for direction in ["up", "left", "left", "down", "down", "right", "right",
"right", "right", "right", "right", "up", "up", "left",
"left", "left", "left"]:
world.move(player, direction)
print(world.render())
Main API
| Class / function | Purpose |
|---|---|
Matrix |
2D grid of characters with rich operations. |
Tile, TileRegistry |
Configure the meaning of each character. |
Entity |
A movable thing with position, symbol, properties. |
World |
The central hub: matrix + tiles + entities + more. |
EventBus, Event |
Tiny pub/sub event system. |
*Collision classes |
Composable collision strategies. |
AsciiRenderer etc. |
Modular renderers. |
PluginManager |
Register / enable / disable plugins. |
run_game_loop |
Optional blocking game loop. |
See the docs for details.
Architecture
charforge
├── matrix.py # Matrix class (2D grid)
├── tiles.py # Tile + TileRegistry
├── entities.py # Entity base class
├── world.py # World (the central hub)
├── collisions.py # Collision strategies
├── movement.py # Direction constants + helpers
├── events.py # EventBus + Event
├── interactions.py # entity_at, line_of_sight, etc.
├── rendering.py # AsciiRenderer, AnsiRenderer, LayeredRenderer
├── plugins.py # PluginManager + FunctionPlugin
├── exceptions.py # Exception hierarchy
└── utils.py # Direction helpers, clamp, etc.
The World is the only object that knows about every subsystem; the other
modules can be used in isolation. There are no circular imports.
Examples
The examples/ directory contains small, runnable scripts:
| File | Demonstrates |
|---|---|
basic_map.py |
Creating a matrix and rendering it. |
movement.py |
Moving entities and observing events. |
collisions.py |
Tile and entity collisions. |
entities.py |
Subclassing Entity for custom behaviour. |
events.py |
Subscribing to world events. |
custom_tiles.py |
Defining custom tile types with metadata. |
Run any of them with python examples/<file>.py.
Tests
The library ships with 232 pytest tests covering every module:
python -m pytest
Lint with ruff:
python -m ruff check .
License
MIT — see LICENSE.
Metadata
Release files for charforge 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| charforge-0.1.0.tar.gz | 55.0 kB | Without provenance | Details for charforge-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| charforge-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for charforge-0.1.0-py3-none-any.whl |
Total release size: 89.2 kB
Release files / charforge-0.1.0.tar.gz
| Download URL | charforge-0.1.0.tar.gz |
|---|---|
| Size | 55.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
779cf57c2ad23f2cf64a49ce591077a73d3bfea6e7e1cc453601f5134e966098
|
|
BLAKE2b-256 checksum How to use checksums |
a6a245f68535a3f35601c07dca8cbcc6dade449d7d86561baa441203c9dabceb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / charforge-0.1.0-py3-none-any.whl
| Download URL | charforge-0.1.0-py3-none-any.whl |
|---|---|
| Size | 34.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f9b0082a7c01caf738964acb3294d291b8cbc185744102da5d552369c1a62156
|
|
BLAKE2b-256 checksum How to use checksums |
31d4eab272df17b82eaefcfb262686680c8022e2fb909548c74fcc05e61fed33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for charforge 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.