Blog Archive

Sunday, November 17, 2013

Starting Our Text Game: Input and Variables

In this post, we will start building our text game. Our text game will output a title, greet the player by name, and determine how fast the player can run away from an enemy.

Let’s begin.

Part 1: Making a New Project for Your Text Game 

1. Run Code::Blocks.
2. Choose "Create a New Project."
3. Choose "Projects" -> "Console application."
4. Click "Next" if a welcome message appears.
5. Select "C++" and click "Next."
6. Give your project a meaningful name. You may want to create a new folder for it. Click "Next" once you're done with this.
This image shows a window from the "Create a New Project" wizard for Code::Blocks. It shows the Project title ("RedRidingHood"), the Folder the new project will be created in ("Red Riding Hood Game"), the project filename ("RedRidingHood.cbp"), and the resulting filepath.
My text game is titled "RedRidingHood" and it's in a file called
"Red Riding Hood Game."

7. Click "Finish."
8. In the "Project Management" window, you will see an option called "Sources" under the name of your project. Click the "+" button next to this option, and open the file named "main.cpp."

The file that opens will look similar to the "Hello World" program we wrote earlier. We can edit the code in this file to display our text game’s title and greet players by name.

Part 2: Adding a Title to Your Text Game and Getting a Player’s Name

1. Add a line below line 1 and write
#include <string>
2. Replace “Hello World”  on line 7 with the title of your text game between the quotation marks.
The image is of a C++ program that outputs my text game's title--"Red Riding Hood"--to the screen with the line "cout << "Red Riding Hood" << endl;"
My text game's title--"Red Riding Hood"--is a string literal like
"Hello World" from the last program we wrote.
3. Add a line below the statement with your title and write
string name;
4. Add another line and write 
cout << "Please enter your name: ";
5. Add another line and write
cin >> name;
6. Add another line and write
cout << "Hello " << name << "." << endl;
7. Choose Build -> Build.
8. Choose Build -> Run, if no errors were generated in step 7.
The line we typed in step 3 is a variable declaration. This variable declaration creates a variable called “name.” A variable is a name for a place in the computer’s memory that can hold data. These names give programmers a meaningful way to store data in computer memory and access that data later. A variable also has a data type which determines how much memory it sets aside for data and what kind of data the variable can hold. The data type for “name” is string; this means that “name” can hold text data.

The cin statement we typed in step 5 stores anything that your player types--up until they hit "Enter"--into name; your text game will interact with players using input from cin. The cin statement and the cout statement are each followed by a set of two carats called stream operators. The cout statement uses output stream operators, which point towards the left. The cin statement uses input stream operators, which point towards the right. Using the output operators where with a cin statement--or vice versa--causes errors that prevent C++ programs from running.

Let's create another kind of variable. 

Part 3: Getting Numerical Input

1. Enter the following code underneath your last cout statement:
int mph;
cout << "Enter your speed (in miles per hour): ";
cin >> mph;
cout << name << " ran away from the wolf at " << mph << " miles per hour." << endl;

2. Choose "Build" -> "Build."
3. Choose "Build" -> "Run," if no errors were generated in Step 2.

Variables with an int data type (like “mph”) hold integers. Variables with an int data type store numerical input within a certain range, but ignore anything entered after a decimal point. Numerical input can be stored in variables with several different data types; we'll learn more about different data types in the next post.

The output from the program we wrote fits in with my Red Riding Hood theme, but your text game might have a different theme. You can change the output to fit your theme by changing the text inside the quotation marks; experiment with it until you find something that you like.

You just wrote the beginning of your text game. If your text game generated any errors, I have the code from my text game in this Google Drive file so you can check your code against mine. In the next post, we'll learn more about the different data types C++ uses for variables. 

No comments:

Post a Comment