AI Game Framework (aigf)
An elegant, high-performance, asynchronous Python library designed to streamline the development, testing, and optimization of AI agents in both real-time and discrete-time games. Detailed documentation is hosted on GitHub Pages.
Inspired by modern web micro-frameworks like FastAPI and Flask, aigf allows developers to build games using clean functional decorators or object-oriented classes. It bundles a fully integrated lightweight HTTP/WebSocket server that automatically serves custom visualizers along with packaged styling (nord.css) and client-side logic (framework.js).
Features
- Lightweight & High Performance: Utilizes
websocketsand async/await event loops to easily handle large volumes of concurrent agents (perfect for neuroevolution or RL optimization). - Flexible API Models:
- Decorated Hooks (
AIGameServer): FastAPI/Flask style callbacks for connection, action, tick, and state-retrieval events. - Class Inheritance (
GameInterface): Rigid Object-Oriented structure for strict architectural isolation.
- Decorated Hooks (
- Zero-Dependency Web Server: Houses an integrated static asset server, letting developers serve a rich HTML/JS/CSS frontend directly from Python.
- Pre-packaged UI Ecosystem: Standardized layout sheets (
nord.css) and event handlers (framework.js) are packaged inside the wheel distribution and served natively. - No Docker Required: Run natively on host machines inside virtual environments.
Installation
Add it directly to your game project's requirements.txt:
ai-game-framework >= 1.3.0
Or install it directly from source/PyPI:
pip install .
Hidden Information
get_state() is viewer-only. Each agent receives GameInterface.get_agent_state(player_id) instead. By default it is the viewer state filtered by agent_mask(player_id),
an allow-list of top-level keys: a field that is not listed is never sent, so a forgotten field cannot leak (None sends everything). Games on grids can hide cells outside an observer's radius with aigf.fog_grid.
class MyGame(GameInterface):
def agent_mask(self, player_id):
return {"my_ships", "scores", "valid_actions"}
Viewer Connection
A viewer served by the framework must call new GameClient() with no port: it then connects to the host and port of the page (wss on HTTPS, no port on 80/443), so it works behind a proxy too. Pass a port, e.g. new GameClient(8765), only when the page is served from elsewhere.
Static Files
By default no files are served (pygame or any non-web frontend needs nothing). To serve a web viewer, list its folders:
GameInterface(static_dirs=["viewer", "assets"]) # relative to the working directory, or absolute
With a list, the package assets (/aigf/framework.js, /aigf/nord.css) are served too.
How it Works: Webpage Integration
The framework handles HTTP requests through a specialized static file server routing mechanism.
When a web browser connects to the server (e.g. http://localhost:8765/):
- HTML Serving: The server looks for an
index.htmlin the folders listed instatic_dirsand serves it as the root webpage. With the defaultstatic_dirs=Noneno files are served. - Asset Bundling: The custom HTML imports
/aigf/framework.jsor/aigf/nord.css. - Automatic Routing: The framework's internal path resolver interceptor (
find_static_file) catches requests starting with/aigf/or/framework/and automatically serves (whenstatic_dirsis set) the embedded assets bundled inside the pip package wheel. No Nginx, complex volume binds, or system-wide assets are required!
// Inside your viewer/index.html
import { GameClient } from "/aigf/framework.js";
// Connects to the backend that served the page (same host and port; wss on HTTPS).
// Pass a port only when the page is served from elsewhere: new GameClient(8765).
const client = new GameClient();
client.onUpdate((state) => {
// Renders the real-time game state coordinates on canvas
console.log("Visualizer Update:", state);
});
Quick Start: Decorated Game Server
Creating a game server is exceptionally quick. Register game loops and player interactions with function annotations:
from aigf.interface import AIGameServer, GameState
# 1. Initialize a real-time server operating at 30 FPS
app = AIGameServer(is_real_time=True, fps=30)
players = {}
# 2. Player handshake connector
@app.on_connect
async def connect(player_id: int):
players[player_id] = {"x": 400.0, "y": 300.0, "score": 0}
if len(players) >= 2:
app.state = GameState.RUNNING
# 3. Real-time game tick
@app.on_tick
async def tick(dt: float):
for p in players.values():
p["x"] += 10.0 * dt # Move players slightly right
# 4. Action receiver hook
@app.on_action
async def action(player_id: int, act: dict):
if act.get("cmd") == "jump":
players[player_id]["y"] -= 50.0
# 5. Visualizer state builder
@app.on_get_state
def get_state() -> dict:
return {"players": list(players.values())}
# 6. Launch the server
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8765)
Interactive Examples
The package includes two fully functioning, high-quality sample games demonstrating the API:
1. Dino Jump (Real-Time Game)
Located in examples/dino_jump/. Mimics Chrome's offline dinosaur game.
- Start Server:
python3 examples/dino_jump/main.py - Play in Browser: Open
http://localhost:8765/(press Space to jump). - Play in Terminal: Run
python3 examples/dino_jump/manual_agent.py.
2. Hangman (Discrete/Turn-Based Game)
Located in examples/hangman/. A turn-based word-discovery game with zero ticks.
- Start Server:
python3 examples/hangman/main.py - Play in Browser: Open
http://localhost:8765/(click or type letters). - Play in Terminal: Run
python3 examples/hangman/manual_agent.py.
Running Unit Tests
Validate code changes using unittest:
python3 -m unittest discover -s tests
The GameClient.wsUrl test needs Node.js only (no browser); make test runs both suites:
node --test tests/test_ws_url.mjs
Generating Documentation
The codebase utilizes Google-style docstrings. Build documentation into standard HTML using pdoc:
pip install pdoc
PYTHONPATH=src pdoc --math -d google -o docs_out aigf
Authors
- Mário Antunes - mariolpantunes
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for ai-game-framework 1.3.1
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 |
|---|---|---|---|---|
| ai_game_framework-1.3.1.tar.gz | 24.3 kB | Without provenance | Details for ai_game_framework-1.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_game_framework-1.3.1-py3-none-any.whl | Python 3 | none | any | Details for ai_game_framework-1.3.1-py3-none-any.whl |
Total release size: 43.5 kB
Release files / ai_game_framework-1.3.1.tar.gz
| Download URL | ai_game_framework-1.3.1.tar.gz |
|---|---|
| Size | 24.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bf0c9fba9446abffc425d552a09bb2f847e0df1340378d8d257e29d92e19a286
|
|
BLAKE2b-256 checksum How to use checksums |
c797342e634389a5506741ff5d9b80fd1fe0e3f54b27f998f4105f027c879faf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ai_game_framework-1.3.1-py3-none-any.whl
| Download URL | ai_game_framework-1.3.1-py3-none-any.whl |
|---|---|
| Size | 19.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3731467ab2a0d22c0a4267d34c2adf31dc592d2afba717029577a2c2a2be166a
|
|
BLAKE2b-256 checksum How to use checksums |
0c65113e01102f10e0862e8e93d44ca0495b56341304b6193899a47a63756680
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for ai-game-framework 1.3.1
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.