if statements are fundamental to any kind of game programming. Without them, you really can't make any kind of game at all. This Brutus2d Tutorial will introduce you to if and its friend then.
Cake or Death Tutorial
Cake or Death? This question was first posed by Eddie Izzard in his stand-up routine "Dressed to Kill". It's not a very hard choice. However, believe it or not, when you ask someone whether they want Cake or Death, you'd be surprised how many people answer "death". In this tutorial, we're going to look at how to ask the question of "Cake or Death" and give a proper response using an if statement.
The Code
dim answer
dim response
console.Initialize
console.WriteLine "Would you like cake or death?"
console.WriteLine "Type c for cake, d for death."
console.WriteLine
answer = console.ReadLine
response = "Very well, I see you want neither."
if answer = "c" then response = "Very well then, Cake it is."
if answer = "d" then response = "Very well then, You are dead."
console.WriteLine
console.WriteLine response
console.Wait
console.Terminate
Compared to the first two tutorials, that's alot of code. But don't worry. Take a look at it and you'll realize that you already know how most of it works. There's only a few lines that are new, and a few used in a new way.
Go ahead and copy and paste this into the Brutus2D IDE and hit F5 to test it out. Make your choice between cake or death and then come back to this tutorial.
Dimming Variables
We need two variables so we declare them up front. Remember, it's almost always a good idea to do this.
dim answer
dim response
answer will hold the player's answer to the question and response will be used to hold the answer (until we display it later). Both of these lines are complete statements.
So, Cake or Death?
In the below code, we start up the console object with console.Initialize as we do in every text game created with Brutus2D. Then, console.WriteLine is used to ask the player the immortal question —- "Would you like cake or death." This is then followed by another bit of text displayed to the screen informing the player on how to make his choice.
console.Initialize
console.WriteLine "Would you like cake or death?"
console.WriteLine "Type c for cake, d for death."
console.WriteLine
And now for something new. When console.WriteLine is used by itself, it displays a blank line. So, if you need a blank line somewhere, just use console.WriteLine this way.
The Player's Answer
Using code similar to the last tutorial, the player's answer to the immortal question is taken using console.ReadLine.
answer = console.ReadLine
Just In Case
Even though we haven't checked what the player has answered, we go ahead and put something in the response variable. This way, if he answers nothing, something will be there to display. Obviously, if he types nothing, or types another letter other than c or d, then he doesn't want cake or death. In this case, the game is ready to respond no matter what.
response = "Very well, I see you want neither."
c is for cake, d is for death
Here comes the "hard" part of this tutorial. The fabled if statement. The if is known in more anal retentive circles as a "conditional statement" or as a "branching statement". Sure, it's both, but I want you to forget those terms for now. Right now, I want you to pretend you are talking to a robot —- a robot guard dog.
You need to give your robot guard dog instructions on how to guard your house. Your instructions may sound like this :
if its Dad then let him through.
if its Mom then let her through.
if its someone you dont know then bark like hell.
Now, let's turn those simple instructions into valid code :
if person = "Dad" then pass = true
if person = "Mom" then pass = true
if person = "unknown" then action = "bark"
As you can see, the basic if statement takes this form :
if (condition) then (do something)
Finally, here's the if statements from the Cake or Death code.
if answer = "c" then response = "Very well then, Cake it is."
if answer = "d" then response = "Very well then, You are dead."
I bet you can already see how these work, but just in case, let's go over the first one. If the player answers c then Very well then, Cake it is." is put into the variable {{response.
But, what if the player types nothing? Or, types in a letter other than c or d? We're already prepared. In the section just before this we put some text into the response variable just in case. If the player answers c or d, then this "default" text is replaced with an appropriate response.
Final Part
All of the rest of the code you know :
console.WriteLine
console.WriteLine response
console.Wait
console.Terminate
What Next?
In the next tutorial, you learn how to make a game loop.
Related Pages
Backlinks
These pages link back to this page. You may find them helpful.
| This article is a stub. You can help us improve Brutus2D Wiki Complete by expanding it. |
Discuss