VOOZH about

URL: https://www.geeksforgeeks.org/nlp/building-a-text-based-adventure-game-with-spacy-a-step-by-step-tutorial/

⇱ Building a Text-Based Adventure Game with SpaCy - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Building a Text-Based Adventure Game with SpaCy

Last Updated : 1 Apr, 2026

Text based adventure games allow players to explore stories by typing commands instead of clicking buttons. Here, we build a simple game where a player navigates a fictional forest and makes choices that affect the outcome. To understand this, we combine basic game design concepts with Natural Language Processing (NLP) using the SpaCy library.

👁 spacy
Text based game using Spacy

To build this game, we focus on the following main elements:

  • Natural Language Processing (NLP): Helps the system understand player commands by detecting actions and important words.
  • SpaCy Library: Analyzes the typed text by breaking it into words and identifying verbs and nouns to understand the command.
  • Game Design Logic: Creates different paths (north, south, east, west) where player choices decide what happens next.
  • Class Based Structure: Uses a Python class to manage the game state, process input and control the flow of the story.

Implementing Text Based Adventure Game with SpaCy

Step 1: Install Required Libraries

Run the following commands in your command prompt

pip install spacy

python -m spacy download en_core_web_sm

Step 2: Setting Up SpaCy

We begin by loading the SpaCy model, which enables natural language understanding. This allows us to:

  • Tokenize user input
  • Identify verbs and nouns
  • Perform lemmatization (convert verbs to base form)
  • Use dependency parsing for better intent detection

Step 3: Designing the Game World

We create a class Game that will contain all the game logic. The constructor initializes the starting state and loads the SpaCy model.

Step 4: Parsing User Input

We define a method to process player input. This method checks if important keywords are present and returns the base form of the word.

Step 5: Starting the Game

The start method introduces the story and asks the player to choose a direction.

Step 6: Handling Each Path

Each direction has its own method. These methods present a scenario and determine the outcome.

North path

East path

West path

South Path

Step 7: Ending the Game

After the outcome, the game displays a result and asks if the player wants to play again.

Step 8: Running the Game

Create an object of the Gameclass and start it.

Output:

👁 Output
Output

Download the full code from here

Comment

Explore