Introduction: Making a Batch File Game on Notepad (part 1)

What is a batch file?

A batch file is simply a computer program that basically gives a list of instructions for a computer to follow.

In this tutorial, I will demonstrate different commands and give a brief explanation on what they do.

Let's get started!

Step 1: The Basics

The first thing you are going to want to know/need is that this has to be performed on a Windows computer. Mac runs on different code (I think; Haven't really checked into it because I don't have a Mac).

Next, you are going to want to open up three things:

  1. Notepad
  2. Cmd (or Command Prompt if you want to be all fancy and stuff)
  3. Windows Explorer (you know, that file explorer thing? It's called different things

Now you are ready to start coding!

Step 2: Your First Few Commands

Your First Few Commands

The first command you are going to learn is:

@echo off

  1. Type this text in the notepad.
  2. Hit File
  3. Save as
  4. Under whatever you name your file, click on the dropdown menu that says "Text Document (*.txt)" and change it to "All Files".
  5. Name your file whatever you want, but include ".bat" as the file extension (Forex, if I were naming my document "Joe, I'd write: 'Joe.bat'"
  6. Hit save.

This first command won't do much by its own. It's complicated to explain what it does, but is always the first command to learn. The next two commands are a lot more simple.

echo [Your text]
  1. Put these two commands under your "@echo off" command.
  2. In the [Your text] box, put anything you like
  3. Hit Save (or ctrl+s)
  4. Now go to your file explorer and open up that file you created.← (This is called "running your code") It should pop up as a batch file program/cmd thing, and should say
[Your text]
Press any key to continue . . .

The "echo" command allows you to print anything you said in the box.
The "pause" command freezes the code, and prints out "Press any key to continue . . .". Notice if you run the code without the pause, the box will open up and close in an instant. This is because there was no pause function to freeze the code.

TIP: Make sure you SAVE your work before your run your code, or else your work will not pop up when running the code.

Congratulations! You created your first batch file program! Next, we will look at some ways we can use these commands.

Step 3: Using Pause, Echo, and @echo Off Commands

With these three commands, we can make a basic book (I don't know what to call it)

First, I will give an example:

@echo off
echo Hello.
echo This is my second batch file program.
echo Bye.

Copy and paste this code into notepad. Make sure you overwrite whatever you put in the last program, or else it will screw your program up.

You know the drill. Save it, and run the code.

After doing this, go back into notepad, and try making one of these your self.

Next, we will move on to three more commands.

Step 4: The Cls, Goto, and :loop Command

cls, goto, and :loop

The next three commands we are going to learn are "cls, goto, and :loop"

The cls command is very basic. It does what many people assume it does: clear the screen.

Take the code from the last step and add 2 "cls" in between your pause and your echo commands, like shown below. (Or you can just copy and paste from below if you're lazy; I won't mind)

@echo off
echo Hello.
echo This is my second batch file program.
echo Bye.

Now, run this code. You'll notice that each time you click a button it says "Press any key to continue . . .", the screen completely clears and echos the next message.

Try some of these on your own. Also, try this command, as well as the other commands in the cmd box you opened previously.

Loop and goto

These two commands go very nice together. One cannot work without the other.

I will put this in a real life situation. Imagine you are at a grocery store, looking for some pineapples. In aisle A, they sell pens. In aisle B, they sell apples. In aisle C, they sell pineapples. Of course you are going to want to aisle C.

This is the same with :loop and goto.

In the code, :loop is the place you want to go.

In the code, goto is the command that states that you want to go to :loop.

Example code:

@echo off
echo This is the menu.
echo This is NOT the menu.
goto loop

In this case, at the end, the goto function tells the computer to send the user back to the top of the code, where the whole code will repeat itself. The :loop function does not need to be named "loop". You can name it almost whatever you want. Just make sure whatever you name it, when you call the "goto" function, you say "goto [whatever you named the :loop". This may be a little confusing. Here is another example code.

@echo off
echo This is the menu.
echo This is NOT the menu.
goto menu

The code above, and the one above that will both do the same thing. The only difference is what you name the "goto" and "loop" command. In the case above, the place is named "menu", and the goto statement will take you to "menu".

I know this is confusing, but I am doing the best I can to explain this in words. I will try to create a video for ya'll to watch that way you can visually see what I am doing.


Simple variables

Step 5: Simple Variables


An exciting topic, don't you think?

You guys remember algebra? You still remember what a variable is?


Basically, a variable is an object that stores data. Here is another real life scenario: You work at the candy store. You have one jar that contains jelly beans, which is labled "Jar A". You have a second jar that contains Chocolate candies, which is labled "Jar B". A customer comes in and says he wants some of those jelly beans. Obviously, you are going to go reach for Jar A. In this case, jar a is the "variable", and the jelly beans are the data.

We are going to create a simple q&a query using variables.

@echo off
echo Do you like grapes?
set /p grapes=
echo You said: %grapes%

If you run this, the cmd box will show up, and will ask you "Do you like grapes?". If you type in a response and press enter (or return), it will say "You said: [Whatever you said here]".

Now let's look at the code. We understand (sort of) the first three lines. The fourth line states "set /p grapes=". You are probably wondering "what the heck?". Well, the word "set" basically says to the computer "we are creating a variable". The / is the type of variable. In this case, we are making a "p" variable. (I do not know what this stands for). The "grapes" is the variable. Maybe you can change it to "x" or "y" or something simpler later on, but for now, keep it as grapes. This whole line creates a (sort of) question, which you need to type something in and press enter to continue. Whatever you type in is the data stored in the variable. That means, if you type in "yes", that would mean that the variable grapes is equal to "yes". Oh yeah, and don't forget the equal sign.

Now on to line 5. Pretty simple, except for the last part. You might think "Why did it print [Whatever you said here] instead of "%grapes%"?. That's because, ladies and gentlemen, putting a variable in between two percent signs basically puts the data there. If you typed in "yes" for the fourth line, it would say "You said: yes".

Phew. That's alot of learning. It might be hard to understand this, but it's a whole different story to make this. I'm sorry, I'm doing the best I can :)

If/else statements are next

Step 6: If/else Statements

Congratulations! You have made it this far in the tutorial! You are awesome! (If you didn't make it this far, you stink. But since they didn't make it this far, they wouldn't see this message, therefore it's okay to say)

Now we are going to make it trickier: if/else statements.

We learned in the last step how to create a question (I guess that's what you call it) in batch code. Now we have to make the computer do something. Forex, if you asked someone "What's two plus two?" Obviously, the answer is 4. Now how would we make that in batch file code?

Simple, use an if/else statement. Basically, it will go like this: The question is asked "What is two plus two?". If the user types in 4, say "Correct!". If the user says anything else, say "Incorrect!". Now, let's make that in code.

@echo off
echo What is two plus two?
set /p answer=
if %answer%==4 (
echo Correct!
)else (
echo Incorrect!

Before you bulge your eyes out, let me try to explain. Hopefully you understand lines 1-3. A brief refresher on 4, whatever the user types in, is basically stored in the variable "answer".

Line 5 Here's where it get's tricky. It starts off withif and then the variable %answer% double equals? What? In batch file code (and in almost any other programming language) double equals (and sometimes triple equals) means = in an if/else statement. This is because the single equal sign is used for many other purposes. So if the answer that the user typed in = 4 then do whatever is in the parentheses. That's what it's saying.

Now in the parentheses, you understand the "echo Correct!" and the "pause". But exit? Well. It was so simple, I forgot to explain. Basically, this will just exit out of Cmd as a whole.

else Here's where the else kicks in. This is saying if the answer is not 4, then do whatever is in the parentheses.

TIP: Always remember, when doing else statements, put a space between the "else" and the opening parentheses. The first one is fine.

And then close the parentheses.


Actually, right now, you have enough knowledge to create a simple game!

Step 7: The Simple Game

This is the end of the tutorial (for part 1)

I will make a part 2 soon. I promise. I swear. I know.