DEV Community

Sonam
Sonam

Posted on

Build an AI Adventure Game in Python

Most LLM demos are one prompt and one answer.

This one is a little more fun: a text-based choose-your-own-adventure game.

The player picks a genre, names a character, reads a generated scene, and chooses option 1, 2, or 3. The app keeps track of game state while Telnyx AI Inference generates the next scene.

Code: https://github.com/team-telnyx/telnyx-code-examples/tree/main/adventure-game-python

What the app does

The Flask API exposes:

POST /game/start
POST /game/<id>/choose
GET /game/<id>
GET /games
GET /health
Enter fullscreen mode Exit fullscreen mode

Each game tracks:

  • genre
  • player name
  • current scene
  • available choices
  • location
  • health
  • inventory
  • turn count
  • status

Statuses can be:

ongoing
won
lost
escaped
Enter fullscreen mode Exit fullscreen mode

How it uses Telnyx AI Inference

The app calls:

POST /v2/ai/chat/completions
Enter fullscreen mode Exit fullscreen mode

Default model:

AI_MODEL=moonshotai/Kimi-K2.6
Enter fullscreen mode Exit fullscreen mode

The system prompt asks the model to act as a game master and return JSON only:

{
  "scene": "the scene description",
  "choices": ["choice 1", "choice 2", "choice 3"],
  "state": {
    "location": "...",
    "health": 100,
    "inventory": [],
    "turn": 1
  },
  "status": "ongoing"
}
Enter fullscreen mode Exit fullscreen mode

That structure is what makes the example useful. The model writes the scene, but the app can still parse the result, store state, validate choices, and decide whether the game continues.

Run it

Clone the repo:

git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/adventure-game-python
Enter fullscreen mode Exit fullscreen mode

Create your .env:

cp .env.example .env
Enter fullscreen mode Exit fullscreen mode

Add your Telnyx API key:

TELNYX_API_KEY=your_telnyx_api_key
AI_MODEL=moonshotai/Kimi-K2.6
HOST=127.0.0.1
PORT=5000
Enter fullscreen mode Exit fullscreen mode

Install and start:

pip install -r requirements.txt
python app.py
Enter fullscreen mode Exit fullscreen mode

Check health:

curl http://localhost:5000/health
Enter fullscreen mode Exit fullscreen mode

Start a fantasy adventure:

curl -X POST http://localhost:5000/game/start \
  -H "Content-Type: application/json" \
  -d '{
    "genre": "fantasy",
    "player_name": "Aria"
  }' | python3 -m json.tool
Enter fullscreen mode Exit fullscreen mode

Choose your next move:

curl -X POST http://localhost:5000/game/game-<id>/choose \
  -H "Content-Type: application/json" \
  -d '{"choice":1}' | python3 -m json.tool
Enter fullscreen mode Exit fullscreen mode

Try other genres:

curl -X POST http://localhost:5000/game/start \
  -H "Content-Type: application/json" \
  -d '{"genre":"cyberpunk","player_name":"Void"}'
Enter fullscreen mode Exit fullscreen mode

Valid genres:

  • fantasy
  • sci-fi
  • mystery
  • horror
  • cyberpunk
  • post-apocalyptic

Why this is a useful pattern

The game is playful, but the app pattern is practical.

It shows how to:

  • keep state in your backend
  • pass recent history back to the model
  • ask for structured JSON
  • validate user choices
  • continue or end a session based on model output

That same pattern can power training simulations, support flows, onboarding tutorials, guided troubleshooting, or interactive learning tools.

The model handles the creative generation. Your app owns the rules.

Resources

Code: https://github.com/team-telnyx/telnyx-code-examples/tree/main/adventure-game-python

Telnyx AI skills and toolkits: https://github.com/team-telnyx/ai

Telnyx AI Inference docs: https://developers.telnyx.com/docs/inference

Chat Completions API: https://developers.telnyx.com/api/inference/chat-completions

Telnyx Portal: https://portal.telnyx.com/

Top comments (0)