In this tutorial, you will learn about basic input, output, and variables. Yes, you will also learn how to make a computer insult people as well.
Input, output, and variables are essential to game programming. Input involves getting information into your game, output involves getting information out. In the meantime, variables are used to hold it all.
Input
Input is getting information into your game. As you progress in your quest to become a game programmer, you will learn that input comes in many forms. For example, your game can take input in the form of pressing buttons on a gamepad, or, your game could open up a file that contains the layout of your game world. All of it is information that is put into your game.
For now, we're going to focus on getting input from the player in the most fundamental way —- directly from the keyboard.
Output
If you read the last tutorial, you've already dealt with output to some extent. In that tutorial, you used console.WriteLine to output text to the console window. We're going to do pretty much the same here.
Variables
Variables are like boxes that hold information. In later game programming tutorials, you will be using variables to hold the name of a video game character or his hit points. Any information you game needs to remember will almost always be stored in a variable. In this tutorial, we're just going to use a variable to hold the name of the player, just so we know who to insult.
The Code
Here's the game code. Go ahead and copy and paste it into the Brutus2D IDE and press F5 to run it. When you're done, come back to this tutorial and you'll learn exactly how it all works.
dim insultee
console.Initialize
console.WriteLine "What's your name?"
insultee = console.ReadLine
console.WriteLine "You look funny, " & insultee & ", I never liked you."
console.Wait
console.Terminate
dim
Right away, we've encountered something new : dim.
dim insultee
dim is used to create a variable. The name after dim is the name of the variable to be created, which is insultee. This is alot like saying to Brutus2D :
"Hey, create a variable named insultee so I can use it later!"
It's always a good idea to name your variables so you now what information they hold. In this case, we've named it insultee because it will hold the name of the person we are going to insult. If you were using variables in a similar way in an RPG, you would use something like this :
dim characterName
It's also a good idea to declare your variables at the beginning of your game code to make sure they are there when you need them. This is why this line comes first, even before console.Initialize.
Things You Already Know
After declaring the insultee variable, we're back in familiar territory.
console.Initialize
console.WriteLine "What's your name?"
console.Initalize wakes up the console object and gets it ready to do other, more useful things.
console.WriteLine is used to print out a question, asking for the insultee's name. Remember, since your game is sending it out, this is considered output.
console.ReadLine
console.ReadLine is the opposite of console.WriteLine. Instead of displaying output to the player, gets input from the player. When it is used, it will patiently wait for the player to type in something and press ENTER. Once the player presses enter, the equals sign ( = ) puts whatever the player typed into insultee.
insultee = console.ReadLine
Naturally, using console.ReadLine to get text input like this is fundamental to all text games.
console.WriteLine revisited
You already know how to print simple messages to the screen using console.WriteLine. In the next line of code, it's used a bit differently :
console.WriteLine "You look funny, " & insultee & ", I never liked you."
The "and" symbol ( & ) is what is known as a concatenation operator. Basically, what it does is join two pieces of text together. For example, this code
console.WriteLine "Hello " & "World."
will display Hello World.. Since we are also throwing a variable in the mix, it includes the contents of the variable as well. In this case, whatever the player typed in as his name. Assuming that the player typed in "Bob" as his name, the first example of console.WriteLine will print out : You look funny, Bob, I never liked you.
console.Wait
console.Wait is an extremely useful little method. It waits displays Press any key to continue … in the console window and pauses the game until the player presses a key. This gives the player a chance to read some text before the game ends. In the last tutorial, system.Pause was used for this purpose. However, console.Wait is a much more elegant solution.
Ending the Game
The last line, console.Terminate, kills the console object and since Brutus2D has no more code to run, the game ends.
What's Next?
- Why don't you try our thrilling Cake or Death Tutorial?