How to Create a Simple Text Adventure Game in Batch Script
A Text Adventure (or Interactive Fiction) is the pure essence of programmatic branching logic. Before 3D graphics, players navigated dungeons and solved puzzles by reading text descriptions and typing commands like "Go North" or "Take Sword". Building one in a Windows Batch script is remarkably straightforward because the entire game relies entirely on set /p user inputs and goto statement jumps between different "Rooms".
In this guide, we will demonstrate how to construct a functioning Text Adventure with inventory tracking and conditional logic using native Batch.
The Strategy: Rooms and Flags
- Rooms: Each physical location in the game is represented by a
:Label(e.g.,:CaveEntrance,:DarkHallway). - Narrative: The
echocommand prints the room's description. - Input: The
set /pcommand captures the player's choice (e.g.,Nfor North,Takefor taking item). - Flags (Inventory): We use Variables (e.g.,
set "hasKey=1") to track if the player has solved a puzzle or acquired an item. - Conditional Jumps: If the player tries to open a door (
:LockedDoor), the script evaluates the flag and branches to the appropriate outcome.
Implementation Script
@echo off
setlocal EnableDelayedExpansion
:: Setup the Terminal
title Batch Quest: The Lost Artifact
color 0F
:: 1. Initialize Global Inventory "Flags"
set "hasTorch=0"
set "hasKey=0"
:: 2. The Main Menu / Start Room
:Start
cls
echo ==========================================
echo BATCH QUEST: THE LOST ARTIFACT
echo ==========================================
echo You stand outside the mysterious Cave of
echo Echoes. The wind howls ominously.
echo.
echo [1] Enter the cave.
echo [2] Walk away (Quit).
echo.
set "choice="
set /p "choice=What do you do? (1-2): "
if "!choice!"=="1" goto CaveEntrance
if "!choice!"=="2" (
echo Farewell, adventurer^^!
pause
exit /b
)
echo [^^!] Invalid choice.
pause
goto Start
:: 3. Room 1: Cave Entrance
:CaveEntrance
cls
echo ==========================================
echo CAVE ENTRANCE
echo ==========================================
echo You are inside the cave. It is dark and damp.
echo To the North is a long hallway.
echo To the East is a small alcove.
echo.
set "choice="
set /p "choice=Go (N)orth or (E)ast? "
if /i "!choice!"=="N" goto DarkHallway
if /i "!choice!"=="E" goto Alcove
echo [^^!] You stumble in the dark. Choose N or E.
pause
goto CaveEntrance
:: 4. Room 2: The Alcove (Item Discovery)
:Alcove
cls
echo ==========================================
echo THE ALCOVE
echo ==========================================
echo You enter a small, dead-end alcove.
:: Check the inventory flag for the Torch
if "!hasTorch!"=="0" (
echo You see a flickering Torch on the ground^^!
echo.
echo [1] Take the Torch
echo [2] Go back West
echo.
set "choice="
set /p "choice=What do you do? (1-2): "
if "!choice!"=="1" (
set "hasTorch=1"
echo.
echo You picked up the Torch^^! It illuminates the room.
pause
goto CaveEntrance
)
if "!choice!"=="2" goto CaveEntrance
echo [^^!] Invalid choice.
pause
goto Alcove
) else (
echo The alcove is empty now.
echo.
echo Press any key to go back West...
pause >nul
goto CaveEntrance
)
:: 5. Room 3: The Hallway (Conditional Obstacle)
:DarkHallway
cls
echo ==========================================
echo DARK HALLWAY
echo ==========================================
echo You arrive at a long, pitch-black hallway.
if "!hasTorch!"=="1" (
echo Your Torch casts a warm glow, revealing a
echo Golden Key sitting on a pedestal^^!
echo.
if "!hasKey!"=="0" (
echo [1] Take Key and move North
echo [2] Go back South
echo.
set "choice="
set /p "choice=What do you do? (1-2): "
if "!choice!"=="1" (
set "hasKey=1"
echo.
echo You take the Golden Key^^!
pause
goto TreasureRoom
)
if "!choice!"=="2" goto CaveEntrance
echo [^^!] Invalid choice.
pause
goto DarkHallway
) else (
echo The pedestal is empty. You already have the Key.
echo.
echo [1] Continue North to the vault
echo [2] Go back South
echo.
set "choice="
set /p "choice=What do you do? (1-2): "
if "!choice!"=="1" goto TreasureRoom
if "!choice!"=="2" goto CaveEntrance
echo [^^!] Invalid choice.
pause
goto DarkHallway
)
) else (
echo It is too dark to see anything^^! You hear
echo scurrying noises... it is unsafe to proceed.
echo.
echo [1] Retreat South
echo.
set "choice="
set /p "choice=What do you do? (1): "
if "!choice!"=="1" goto CaveEntrance
echo You linger too long, and bat swarms force you out^^!
pause
goto CaveEntrance
)
:: 6. Victory Room
:TreasureRoom
cls
echo ==========================================
echo THE TREASURE ROOM
echo ==========================================
echo The Golden Key unlocks the massive vault door^^!
echo Inside, you find the Legendary Batch File of Power^^!
echo.
echo ==========================================
echo YOU WIN^^!
echo ==========================================
echo.
pause
exit /b
How It Works
- State Machines (
goto): The script never "ends" until the player hitsexit /b. It merely jumps between the labelled environments. If a player types invalid input, the script prints an error, hitspause, andgotos the top of the current room label to reload it smoothly. - Inventory Bools (
hasTorch): Native Batch variables only manage numbers and strings, not booleanTrue/Falsevalues. However,0and1simulate flags perfectly. The:Alcoveentirely alters its text output based on whether you already possess the Torch. - Linear progression limits: The only way to win is to acquire the Key. The only way to spawn the Key is to possess the Torch. This establishes a "Lock and Key" mechanism fundamental to game design, solved entirely via simple local
IFstatements.
Why Build a Text Adventure in Batch?
- Control Flow Masterclass: Managing complex, nested logic evaluations without a dedicated GUI forces a programmer to understand state preservation and error handling perfectly.
- User Experience (UX): Dealing with humans typing
Yes,Y,y,1, or completely accidental gibberish into a raw text box forces the creation of robust input sanitation loops. - Cross-Platform Heritage: This exact architecture, loops, variables, and evaluated user inputs, is precisely how modern deployment menus or localized machine setup sequences function behind the scenes in enterprise environments.
Conclusion
A Text Adventure provides an immensely creative canvas for learning basic system logic. By linking simple interactive prompts to persistent numeric variables, the Windows Command Prompt transforms from a rigid administrative tool into a highly engaging, state-aware storytelling machine.