X
This article was co-authored by wikiHow staff writer, Jack Lloyd. Jack Lloyd is a Technology Writer and Editor for wikiHow. He has over two years of experience writing and editing technology-related articles. He is technology enthusiast and an English teacher.
The wikiHow Tech Team also followed the article's instructions and verified that they work.
This article has been viewed 445,363 times.
Learn more...
This wikiHow teaches you how to make a simple, text-based game in Command Prompt on a Windows computer.
Steps
-
1
-
2Add the title text for your game. Copy the following text into Notepad—making sure to replace "[Title]" with whatever you want to name your game—and then press ↵ Enter:[1]
@echo off title [Title]
Advertisement -
3Choose a color for your game's text and background. Command Prompt offers several different colors of text and background which you can trigger by inputting a color-specific code in "0A" format where "0" is the color of the background and "A" is the color of the text. Codes for common colors include the following:[2]
- Text Colors — Use A, B, C, D, E, or F to refer to light-green, light-aqua, light-red, light-purple, light-yellow, or bright-white, respectively.
- Background Colors — Use 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 to refer to black, blue, green, aqua, red, purple, yellow, white, grey, or light-blue, respectively.
- For example, the standard black-and-white Command Prompt interface would use the code "0F".
-
4Set your game's colors. Enter the following text into Notepad—making sure to replace "0A" with your preferred background and text combination—and then press ↵ Enter:
@echo off title OnlineCmag Game color 0A if "%1" neq "" ( goto %1)
-
5Create the game menu. This is essentially the game's startup menu. Enter the following text into Notepad, then press ↵ Enter:
:Menu cls echo 1. Start echo 2. Credits echo 3. Exit set /p answer=Type the number of your option and press enter : if %answer%==1 goto Start_1 if %answer%==2 goto Credits if %answer%==3 goto Exit
-
6Add an "Exit" option. This is how players will be able to exit the Command Prompt. Enter the following text into Notepad, then press ↵ Enter:
:Exit cls echo Thanks for playing! pause exit /b
-
7Add credits for the game. Enter the following text into Notepad—making sure to replace "[Title]" with your game's title—then press ↵ Enter:
:Credits cls echo Credits echo. echo Thank you for playing [Title]! pause goto Menu
-
8Create the "Start" code. This is the code which will allow players to start a new game:
:Start_1 cls echo Oh no! You're surrounded by enemies. echo There are five of them, and they're all armed. echo If you fight them, you are having a high chance of winning. set /p answer=Would you like to fight or run? if %answer%==fight goto Fight_1 if %answer%==run goto Run_1 pause
-
9Add the action code. Finally, you'll enter the following code to dictate the action of the game:
:Run_1 cls echo You live to fight another day. pause goto Start_1 :Fight_1 echo Prepare to fight. echo The enemies suddenly rush you all at once. set /p answer= Type 1 and press Enter to continue. if %answer%==1 goto Fight_1_Loop :Fight_1_Loop set /a num=%random% if %num% gtr 4 goto Fight_1_Loop if %num% lss 1 goto Fight_1_Loop if %num%==1 goto Lose_Fight_1 if %num%==2 goto Win_Fight_1 if %num%==3 goto Win_Fight_1 if %num%==4 goto Win_Fight_1 :Lose_Fight_1 cls echo You were defeated. Play again? pause goto Menu :Win_Fight_1 cls echo You are victorious! set /p answer=Would you like to save? [y/n] if %answer%=='y' goto 'Save' if %answer%=='n' goto 'Start_2' :Save goto Start_2
-
10Click File. It's in the top-left corner of the Notepad window. A drop-down menu will appear.
-
11Click Save As…. It's in the File drop-down menu. Doing so will open a Save As window.
-
12Enter a file name followed by the ".bat" extension. In the "File name" text box that's near the bottom of the window, type in whatever you want to name the game followed by .bat to ensure that the game will save as a Command Prompt file.
- For example, to name your game "Dungeon Crawl", you would type in Dungeon Crawl.bat here.
-
13Change the file type. Click the "Save as type" drop-down box at the bottom of the window, then click All Files in the resulting drop-down menu.
-
14Select the desktop as the save location. Click Desktop in the left-hand sidebar to do so. You may first have to scroll up or down on the sidebar in order to find the Desktop folder.
-
15Click Save. It's in the bottom-right corner of the window. Doing so will save your game as a BAT file.
-
16Run your game. Double-click the BAT file to open your game in Command Prompt, then follow the on-screen prompts.
- For example, you'll press 1 to start the game.
-
17Experiment with the code. Now that you have the basic groundwork laid out for the game, you can edit the code to change the in-game text, add options, and more.
- To edit your game's code, right-click the BAT file and then click Edit in the drop-down menu. You can then press Ctrl+S to save any changes.
- Make sure you read through the code to understand what each line of text does.
Advertisement
Community Q&A
-
QuestionDo I need to put a quote mark before and/or after echoing something?Community AnswerNo. After echoing, make a space and write what you want to display in a single line.
-
QuestionWhat is a bat file, and where do I put it?Community AnswerA bat file is a batch file, and it runs on cmd (command prompt). You can put it on your desktop to make it easier to test your stuff.
-
QuestionWhen I click to open the game it opens and immediately closes. Why does it do that?Community AnswerIt is because the coding is wrong. Actually there are few lines on the codes that are to instruct us and make us understand, but which are not supposed to be written on the source code(code area). So just scan through the code lines and delete those unnecessary lines. Doing this should make your game work.
Advertisement
Warnings
- Always proofread your code before distributing your BAT file.⧼thumbs_response⧽
- Never download and run BAT files from any source you don't trust, as they can be used to harm your computer if they're malicious.⧼thumbs_response⧽
Advertisement
References
About This Article
Article SummaryX
1. Open Notepad.
2. Type your game code.
3. Save your code as a .bat file.
4. Double-click the .bat file to run the game in a CMD window.
Did this summary help you?
Advertisement