packages/docs/examples-gallery/games.mdx
Build games where AI agents make strategic decisions.
A text-based dungeon crawler where an AI agent explores, fights, and collects treasure.
| Language | Location |
|---|---|
| TypeScript | examples/typescript/adventure-game.ts |
| Python | examples/game/python/game.py |
| Rust | examples/game/rust/game/src/main.rs |
export OPENAI_API_KEY="your-key"
LOG_LEVEL=fatal bun run examples/typescript/adventure-game.ts
╔══════════════════════════════════════════════════════════════════╗
║ 🏰 DUNGEON ADVENTURE 🏰 ║
╠══════════════════════════════════════════════════════════════════╣
║ Location: Great Hall ║
║ Description: A vast hall with crumbling pillars. ║
║ Exits: north, south, east, west ║
║ Enemies: goblin (30 HP) ║
║ Items: none ║
╠══════════════════════════════════════════════════════════════════╣
║ Inventory: torch ║
║ Health: 100/100 ║
╚══════════════════════════════════════════════════════════════════╝
AI Decision: attack
Result: You strike the goblin for 10 damage! It retaliates for 10 damage.
Your health: 90/100 | Goblin health: 20/30
// The AI analyzes the game state and chooses an action
const decision = await runtime.useModel(ModelType.TEXT_SMALL, {
prompt: `
Current Location: ${room.name}
Enemies present: ${room.enemies?.length ? "YES" : "NO"}
Health: ${state.health}%
Has sword: ${state.inventory.includes("sword")}
Choose ONE action: move north, attack, take item, use potion
Your choice:`,
maxTokens: 20,
temperature: 0.3,
});
const rooms = {
secret_chamber: {
name: "Secret Chamber",
description: "A hidden room filled with ancient artifacts.",
items: ["golden_idol"],
exits: { south: "treasure_room" },
},
};
const items = {
golden_idol: {
name: "Golden Idol",
description: "An ancient artifact of immense value.",
use: (state) => {
state.score += 1000;
return "The idol glows with ancient power!";
},
},
};
const enemies = {
lich: {
name: "Lich",
health: 80,
damage: 30,
weakness: "holy_water",
special: "Can summon skeletons",
},
};