How Do You Make a Command-Line Game with Notepad?

TIME : 2016-01-07
How Do You Make a Command-Line Game with Notepad?

Creating a game in Notepad means writing a batch script (a .bat file) using simple text commands. It takes a little learning, but the basics are approachable. Here's a quick primer:

  • Start with @echo off so commands don't clutter the screen.
  • Create a "page" with a label like :menu followed by cls (which clears the screen).
  • Use echo before text to display it, e.g., echo Hello, what's your name:.
  • Capture input with set /p name= to remember what the player types.

Here's a complete tic-tac-toe game you can paste into Notepad and save as a .bat file:

@echo off
color 2
title Tic Tac Toe Game
setlocal enabledelayedexpansion
:menu
cls
echo Main Menu
echo To Play A 1-Player Game, Enter 1
echo To Play A 2-Player Game, Enter 2
echo To Exit, Enter 3
set /p menu=
if not defined menu goto menu
if /iequ 1 ( set menu= & cls & goto 1p )
if /iequ 2 ( set menu= & cls & goto 2p )
if /iequ 3 goto end
set menu=
goto menu
:1p
set turns=0
for %%i in (1 2 3 4 5 6 7 8 9) do ( set x%%i=0 & set o%%i=0 & set d%%i=%%i )
set go=1xgame
goto 1display
:1display
cls
echo %d1% %d2% %d3%
echo %d4% %d5% %d6%
echo %d7% %d8% %d9%
if /iequ 9 (goto draw)
echo Your Turn
goto 1ogame
:1ogame
echo Choose the number of the space you'd like to choose
set guess=
set /p guess=
if not defined guess goto 1display
set guess=%guess:~0,1%
if /i notleq 9 goto 1display
if /i notgtr 0 goto 1display
if /i !d%guess%! neq %guess% goto 1display
goto 1o%guess%
:1oprocess
set /a line1=%o1%+%o2%+%o3%
if /i %line1% equ 111 (goto owin)
set /a line2=%o4%+%o5%+%o6%
if /i %line2% equ 111 (goto owin)
set /a line3=%o7%+%o8%+%o9%
if /i %line3% equ 111 (goto owin)
set /a line4=%o1%+%o5%+%o9%
if /i %line4% equ 111 (goto owin)
set /a line5=%o3%+%o5%+%o7%
if /i %line5% equ 111 (goto owin)
set go=1xgame
set /a turns+=1
goto 1display
:1xgame
set guess=%time:~9,1%
:1ac
if /i %guess% leq 0 goto 1display
if /i !d%guess%! neq %guess% goto 1display
goto 1x%guess%
:1xprocess
set /a line1=%x1%+%x2%+%x3%
if /i %line1% equ 111 (goto xwin)
set /a line2=%x4%+%x5%+%x6%
if /i %line2% equ 111 (goto xwin)
set /a line3=%x7%+%x8%+%x9%
if /i %line3% equ 111 (goto xwin)
set /a line4=%x1%+%x5%+%x9%
if /i %line4% equ 111 (goto xwin)
set /a line5=%x3%+%x5%+%x7%
if /i %line5% equ 111 (goto xwin)
set go=1ogame
set /a turns+=1
goto 1display
:1o1 & set o1=1 & set x1=1000 & set d1=O & goto 1oprocess
:1o2 & set o2=10 & set x2=1000 & set d2=O & goto 1oprocess
:1o3 & set o3=100 & set x3=1000 & set d3=O & goto 1oprocess
:1o4 & set o4=1 & set x4=1000 & set d4=O & goto 1oprocess
:1o5 & set o5=10 & set x5=1000 & set d5=O & goto 1oprocess
:1o6 & set o6=100 & set x6=1000 & set d6=O & goto 1oprocess
:1o7 & set o7=1 & set x7=1000 & set d7=O & goto 1oprocess
:1o8 & set o8=10 & set x8=1000 & set d8=O & goto 1oprocess
:1o9 & set o9=100 & set x9=1000 & set d9=O & goto 1oprocess
:1x1 & set x1=1 & set o1=1000 & set d1=X & goto 1xprocess
:1x2 & set x2=10 & set o2=1000 & set d2=X & goto 1xprocess
:1x3 & set x3=100 & set o3=1000 & set d3=X & goto 1xprocess
:1x4 & set x4=1 & set o4=1000 & set d4=X & goto 1xprocess
:1x5 & set x5=10 & set o5=1000 & set d5=X & goto 1xprocess
:1x6 & set x6=100 & set o6=1000 & set d6=X & goto 1xprocess
:1x7 & set x7=1 & set o7=1000 & set d7=X & goto 1xprocess
:1x8 & set x8=10 & set o8=1000 & set d8=X & goto 1xprocess
:1x9 & set x9=100 & set o9=1000 & set d9=X & goto 1xprocess
:2p
set turns=0
for %%i in (1 2 3 4 5 6 7 8 9) do ( set x%%i=0 & set o%%i=0 & set d%%i=%%i )
goto 2display
:2display
cls
echo %d1% %d2% %d3%
echo %d4% %d5% %d6%
echo %d7% %d8% %d9%
if /iequ 9 (goto draw)
goto 2ogame
:2ogame
echo Choose the number of the space you'd like to choose
set guess=
set /p guess=
if not defined guess goto 2display
set guess=%guess:~0,1%
if /i notleq 9 goto 2display
if /i notgtr 0 goto 2display
if /i !d%guess%! neq %guess% goto 2display
goto 2o%guess%
:2oprocess
set /a line1=%o1%+%o2%+%o3%
if /i %line1% equ 111 (goto owin)
set /a line2=%o4%+%o5%+%o6%
if /i %line2% equ 111 (goto owin)
set /a line3=%o7%+%o8%+%o9%
if /i %line3% equ 111 (goto owin)
set go=2xgame
set /a turns+=1
goto 2display
:2xgame
echo Choose the number of the space you'd like to choose
set guess=
set /p guess=
if not defined guess goto 2display
set guess=%guess:~0,1%
if /i notleq 9 goto 2display
if /i notgtr 0 goto 2display
if /i !d%guess%! neq %guess% goto 2display
goto 2x%guess%
:2xprocess
set /a line1=%x1%+%x2%+%x3%
if /i %line1% equ 111 (goto xwin)
set /a line2=%x4%+%x5%+%x6%
if /i %line2% equ 111 (goto xwin)
set /a line3=%x7%+%x8%+%x9%
if /i %line3% equ 111 (goto xwin)
set go=2ogame
set /a turns+=1
goto 2display
:2o1 & set o1=1 & set d1=O & goto 2oprocess
:2o2 & set o2=10 & set d2=O & goto 2oprocess
:2o3 & set o3=100 & set d3=O & goto 2oprocess
:2o4 & set o4=1 & set d4=O & goto 2oprocess
:2o5 & set o5=10 & set d5=O & goto 2oprocess
:2o6 & set o6=100 & set d6=O & goto 2oprocess
:2o7 & set o7=1 & set d7=O & goto 2oprocess
:2o8 & set o8=10 & set d8=O & goto 2oprocess
:2o9 & set o9=100 & set d9=O & goto 2oprocess
:2x1 & set x1=1 & set d1=X & goto 2xprocess
:2x2 & set x2=10 & set d2=X & goto 2xprocess
:2x3 & set x3=100 & set d3=X & goto 2xprocess
:2x4 & set x4=1 & set d4=X & goto 2xprocess
:2x5 & set x5=10 & set d5=X & goto 2xprocess
:2x6 & set x6=100 & set d6=X & goto 2xprocess
:2x7 & set x7=1 & set d7=X & goto 2xprocess
:2x8 & set x8=10 & set d8=X & goto 2xprocess
:2x9 & set x9=100 & set d9=X & goto 2xprocess
:draw
echo It's A Draw!
pause>nul
goto menu
:owin
echo Noughts Win!
pause>nul
goto menu
:xwin
echo Crosses Win!
pause>nul
goto menu
:end
exit

And here's a text-adventure example—a first, self-taught project that demonstrates the fundamentals of menus, choices, and items:

@echo off
Title adventure program
color 0a
:beginning
cls
echo Select a Direction
echo 1) Walk left
echo 2) Walk right
set /p direction=Type option:
if %direction%==1 goto WalkingLeft
if %direction%==2 goto WalkingRight
:WalkingLeft
cls
echo You see a pig!
echo 1) Punch the pig
echo 2) Run Away
set /p action=Type option:
if %action%==1 goto Punchthepig
if %action%==2 goto Run Away
:WalkingRight
cls
echo You found one gold coin!
echo 1) Walk to the Shop
echo 2) Walk Home
set /p direction=Type option:
if %direction%==1 goto Shop
if %direction%==2 goto Home
:Punchthepig
cls
echo You killed the pig and picked up a gold coin
echo 1) Walk Home
echo 2) Walk to the Shop
echo 3) Walk to the Field
set /p walk=Type option:
if %walk%==1 goto Home
if %walk%==2 goto Shop
if %walk%==3 goto Field
:Home
cls
echo You are now at home
echo 1) Walk to the Shop
echo 2) Walk to the Field
echo 3) Walk to the Wizard
echo 4) Use a Potion
echo 5) Equip an item
echo 6) Talk to the old Man
echo 7) Explore the Mines
echo 8) Walk to the Village Square
set /p walk=Type option:
if %walk%==1 goto Shop
if %walk%==2 goto Field
if %walk%==3 goto Wizard
if %walk%==4 goto UPotion
if %walk%==5 goto Equip
if %walk%==6 goto OldMan
if %walk%==7 goto Mines
if %walk%==8 goto Village
:Field
cls
echo You have walked to the Field, full of monsters and animals!
echo 1) Pig  2) Cow  3) Chicken  4) Sheep  5) Mooshroom
echo 6) Mermaid  7) Slime Monster  8) Skeleton  9) Witch
echo 10) Golden Golem  11) Wizard  12) Shop
set /p attack=Type option:
if %attack%==1 goto Pig
if %attack%==2 goto Cow
if %attack%==3 goto Chicken
if %attack%==4 goto Sheep
if %attack%==5 goto Mooshroom
if %attack%==6 goto Mermaid
if %attack%==7 goto Slimeball
if %attack%==8 goto Skeleton
if %attack%==9 goto Witch
if %attack%==10 goto GoldenGolem
if %attack%==11 goto Wizard
if %attack%==12 goto Shop
:Pig
cls
echo A wild Pig appeared!
echo 1) Attack the pig  2) Run away
set /p wild=Type option:
if %wild%==1 goto APig
if %wild%==2 goto Home
:APig
cls
echo You attacked the Pig! It died and left Experience and Meat.
goto Home
:Cow
cls
echo A wild Cow appeared!
echo 1) Attack the Cow  2) Run away
set /p wild=Type option:
if %wild%==1 goto ACow
if %wild%==2 goto Home
:ACow
cls
echo You attacked the Cow! It died and left Experience and Meat.
goto Home
:Chicken
cls
echo A wild Chicken appeared!
echo 1) Attack the Chicken  2) Run away
set /p wild=Type option:
if %wild%==1 goto AChicken
if %wild%==2 goto Home
:AChicken
cls
echo You attacked the Chicken! It died and left Experience and an Egg.
goto Home
:Sheep
cls
echo A wild Sheep appeared!
echo 1) Attack the Sheep  2) Run away
set /p wild=Type option:
if %wild%==1 goto ASheep
if %wild%==2 goto Home
:ASheep
cls
echo You attacked the Sheep! It died and left Experience and Wool.
goto Home
:Mooshroom
cls
echo A wild Mooshroom appeared!
echo 1) Attack the Mooshroom  2) Run away
set /p wild=Type option:
if %wild%==1 goto AMooshroom
if %wild%==2 goto Home
:AMooshroom
cls
echo You attacked the Mooshroom! It died and left Experience and a Mushroom.
goto Home
:Mermaid
cls
echo The Mermaid is Friendly! She gave you Coral and Green Fire!
pause
goto Home
:Slimeball
cls
echo A wild Slimeball appeared!
echo 1) Attack the Slimeball  2) Run away
set /p wild=Type option:
if %wild%==1 goto Slimeball1
if %wild%==2 goto Home
:Slimeball1
cls
echo You attacked the Slimeball! It died and left Experience and Slime.
goto Home
:Skeleton
cls
echo A wild Skeleton appeared!
echo 1) Attack the Skeleton  2) Run away
set /p wild=Type option:
if %wild%==1 goto Skeleton1
if %wild%==2 goto Home
:Skeleton1
cls
echo You attacked the Skeleton! It died and left Experience and a Bone.
goto Home
:Witch
cls
echo A wild Witch appeared!
echo 1) Attack the Witch  2) Run away
set /p wild=Type option:
if %wild%==1 goto Witch1
if %wild%==2 goto Home
:Witch1
cls
echo You attacked the Witch! It died and left Experience and the Secret Potion.
goto Home
:GoldenGolem
cls
echo A wild Golden Golem appeared!
echo 1) Attack the Golden Golem  2) Run away
set /p wild=Type option:
if %wild%==1 goto GoldenGolem1
if %wild%==2 goto Home
:GoldenGolem1
cls
echo You attacked the Golden Golem! It died and left Experience and a Gold Coin.
goto Home
:Wizard
cls
echo You are now in the Wizard's House!
echo 1) Talk to the Wizard  2) Walk Home
set /p walk=Type option:
if %walk%==1 goto TalkWizard
if %walk%==2 goto Home
:Shop
cls
echo You are now at the Shop
echo 1) Approach the shopkeeper  2) Walk home
set /p walk=Type option:
if %walk%==1 goto Buy
if %walk%==2 goto Home
:Buy
cls
echo What would you like to buy?
echo 1) Short Sword (1 Gold)  2) Long Sword (5 Gold)
echo 3) Meat (2 Gold)  4) Shield (3 Gold)  5) Armor (7 Gold)
set /p buy=Type option:
if %buy%==1 goto ShortSword
if %buy%==2 goto LongSword
if %buy%==3 goto Meat
if %buy%==4 goto Shield
if %buy%==5 goto Armor
:ShortSword
cls
echo You bought a Short Sword!
pause
goto Home
:LongSword
cls
echo You bought a Long Sword!
pause
goto Home
:Meat
cls
echo You bought meat!
pause
goto Home
:Shield
cls
echo You bought a Shield!
pause
goto Home
:Armor
cls
echo You bought Armor!
pause
goto Home
:TalkWizard
cls
echo 1) Get a Potion  2) Level Up  3) Walk home
set /p walk=Type option:
if %walk%==1 goto Potion
if %walk%==2 goto Level
if %walk%==3 goto Home
:Potion
cls
echo Which Potion? 1-5
set /p potion=Type option:
if %potion%==1 goto CPotion1
if %potion%==2 goto CPotion2
if %potion%==3 goto CPotion3
if %potion%==4 goto CPotion4
if %potion%==5 goto CPotion5
:CPotion1
cls
echo You have made a Healing potion!
pause
goto Wizard
:CPotion2
cls
echo You have made a Strength potion!
pause
goto Wizard
:CPotion3
cls
echo You have made a Death potion!
pause
goto Wizard
:CPotion4
cls
echo You have made a Riches potion!
pause
goto Wizard
:CPotion5
cls
echo You have made a Knowledge potion!
pause
goto Wizard
:UPotion
cls
echo Which potion? 1) Healing 2) Strength 3) Death 4) Riches 5) Knowledge 6) Secret 7) None
set /p use=Type option:
if %use%==1 goto UPotion1
if %use%==2 goto UPotion2
if %use%==3 goto UPotion3
if %use%==4 goto UPotion4
if %use%==5 goto UPotion5
if %use%==6 goto USecretPotion
if %use%==7 goto Home
:UPotion1
cls
echo You used a Healing Potion! You are fully healed!
pause
goto Home
:UPotion2
cls
echo You used a Strength Potion! You are now Super Strong!
pause
goto Home
:UPotion3
cls
echo You used a Death Potion! You are now Dead!
pause
goto Death
:UPotion4
cls
echo You used a Riches Potion! You now have 50 Gold Coins!
pause
goto Home
:UPotion5
cls
echo You used a Knowledge Potion! You are now super smart!
pause
goto Home
:USecretPotion
cls
echo You used a Secret Potion! It didn't do anything...
pause
goto Home
:Level
cls
echo The wizard says some strange words. You leveled up! :D
pause
goto Wizard
:Equip
cls
echo Equip an item! 1) Short Sword 2) Long Sword 3) Shield 4) Armor
set /p equip=Type option:
if %equip%==1 goto ShortSwordE
if %equip%==2 goto LongSwordE
if %equip%==3 goto ShieldE
if %equip%==4 goto ArmorE
:ShortSwordE
cls
echo You equipped a Short Sword!
pause
goto Home
:LongSwordE
cls
echo You equipped a Long Sword!
pause
goto Home
:ShieldE
cls
echo You equipped a Shield!
pause
goto Home
:ArmorE
cls
echo You equipped Armor!
pause
goto Home
:Death
cls
echo The World blew up, and you died
pause
goto beginning
:Run Away
cls
echo PANSY!
pause
exit
:OldMan
cls
echo Old man: Ehhhhhh...... Which riddle do you want to hear? 1-10, or 11) Go home
set /p riddle=Type option:
if %riddle%==1 goto Riddle01
if %riddle%==2 goto Riddle2
if %riddle%==11 goto Home
:Riddle01
cls
echo Give it food, and it will live. Give it water, and it will die. What is it?
set /p yes=Type option:
if %yes%==Fire goto ARiddle1
if %yes%==1 goto OldMan
:Riddle2
cls
echo The more you take, the more you leave behind. What is it?
set /p yes=Type option:
if %yes%==Footsteps goto ARiddle2
if %yes%==1 goto OldMan
:ARiddle1
cls
echo You are correct! The old man gives you a Key.
pause
goto OldMan
:ARiddle2
cls
echo You are correct! The old man gives you a pocketwatch.
pause
goto OldMan
:Mines
cls
echo You have walked to the mines.
echo 1) Dig Down  2) Dig Left  3) Dig Right  4) Walk Home
set /p dig=Type option:
if %dig%==1 goto DigDown1
if %dig%==2 goto DigLeft1
if %dig%==3 goto DigRight1
if %dig%==4 goto Home
:DigDown1
cls
echo You have dug down.
echo 1) Dig Down  2) Dig Left  3) Dig Right  4) Go back to the surface
set /p dig=Type option:
if %dig%==1 goto DigDown2
if %dig%==4 goto Mines
:DigDown2
cls
echo You have dug down further and struck bedrock.
pause
goto Mines
:DigLeft1
cls
echo You have dug left.
pause
goto Mines
:DigRight1
cls
echo You have dug right.
pause
goto Mines
:Village
cls
echo You have walked to the Village Square.
echo 1) George  2) Alexis  3) Zeke  7) Leave
set /p talk=Type option:
if %talk%==1 goto NPC1A
if %talk%==2 goto NPC2A
if %talk%==3 goto NPC3A
if %talk%==7 goto Home
:NPC1A
cls
echo In the mines, you will find a girl lost at nine.
pause
goto Village
:NPC2A
cls
echo For you, I will sing a song of Momiji!
pause
goto Village
:NPC3A
cls
echo This Quest is called The Epicus Ultima. Talk to James and Mariah.
pause
goto Village

With these examples you can see the core idea of batch coding—labels, goto jumps, echo output, and set /p input—which is all you need to start building your own text games.