Blog Archive

Showing posts with label text adventure game. Show all posts
Showing posts with label text adventure game. Show all posts

Friday, December 6, 2013

Validating Player Input: Loops

In this tutorial, we will learn how to give players more than one chance to input a response into our text games. When players enter a wrong number, our text games currently output an error message before quitting. Players may accidentally enter a wrong number while playing a text game. If we want players to be able to play through our text games even after they enter a wrong number, we should give them another opportunity to enter a correct response when they enter a wrong number. We can give players another opportunity to enter a correct response using a loop, which is a control structure that executes a series of statements until some condition is met. The loop we will use is called a while loop, and the condition will require that the player enters a correct response before the loop stops getting new responses from the player.

Part 1: Creating a While Loop That Gets Player Input


  1. Run Code::Blocks.
  2. Open the project file for your text game.
  3. Create an int variable called “validChoice”.
  4. int validChoice;
    
  5. Create a bool variable called “valid”.
  6. Set valid to false using an equals sign.
  7. bool valid = false;
    
  8. Add a line below the two variables and write
  9. while (valid == false)
    {
    
    }
    
  10. Use cout statements between the while loop's braces to give players a problem, two numbered choices, and a prompt for a response.
  11. {
        cout << "You encounter an unattended pot of gold." << endl
             << "1. Try to take the gold." << endl
             << "2. Leave the gold alone." << endl;
        cout << "Enter 1 or 2: ";
    }
    
  12. Use a cin statement between the while loop's braces to get the player’s response and read it into “validChoice”.
  13. cin >> validChoice;
    


We have a while loop now, but if we build and run our program we will encounter a problem called an infinite loop. An infinite loop happens when a loop’s condition is met and then never changes within the loop. Our while loop never changes “valid” to true, so our text game will never stop prompting the player for the a response, even if they input a correct response. The infinite loop in our text game can be fixed by creating a selection statement that changes “valid” to true if the player enters a correct response.

Part 2: Updating A While Loop’s Condition


  1. Create a selection statement with an if portion, and else if portion, and an else portion within the braces for the while loop. Make “validChoice” the condition for the selection statement.
  2. while (valid == false)
    {
        cout << "You encounter an unattended pot of gold." << endl
             << "1. Try to take the gold." << endl
             << "2. Leave the gold alone." << endl;
        cout << "Enter 1 or 2: ";
        cin >> validChoice;
        
        if (validChoice == 1)
        {
            
        }
        else if (validChoice == 2)
        {
            
        }
        else
        {
            
        }
    }
    
  3. Use a cout statement to output an outcome in the if portion of the selection statement.
  4. Set “valid” to true in the if portion of your selection statement.
  5. if (validChoice == 1)
    {
            cout << "A fairy appears!" << endl
                 << "She thanks you for guarding her treasure." << endl
                 << "You walk away feeling guilty." << endl;
            valid = true;
    }
    
  6. Use a cout statement to output an outcome in the else if portion of the selection statement.
  7. Set “valid” to true in the else if portion of your selection statement.
  8. else if (validChoice == 2)
    {
            cout << "You walk away, feeling righteous but " << endl
                 << "a bit foolish." << endl;
            valid = true;
    }
    
  9. Use a cout statement to output a message to the player in the else portion of the selection statement to let the player know that they entered a wrong response.
  10. else
    {
        cout << "You made a response in error. Try again." << endl;
    }
    
  11. Choose "Build" -> "Build."
  12. Choose "Build" -> "Run", if no errors were generated in Step 7.


Play your text game and try to input a few different numbers to see how the while loop works. The while loop should output an outcome for your choice if it’s correct, but it will repeat the prompt if you enter an incorrect response. You can place a while loop around any selection statement to give your players more than one chance to input a response.

I placed all of my text game’s selection statements inside of while loops that validate player input. My text game is in this Google Drive file, so you can check your code against mine if your text game generated any errors. In the next post, I will interview a student of game design who has advice about writing stories for games.

Thursday, December 5, 2013

Giving Players Choices: Nested If Statements

In this tutorial, we will learn how to create branching storylines that reflect your players' choices. We can create branching storylines by placing selection statements inside of other selection statements; this allows the story to change in ways that depend on the choices that players made earlier in the game. Selection statements placed within other selection statements are called nested if statements.

Let’s begin.

Part 1: Coding the Outer Selection Statement

  1. Run Code::Blocks
  2. Open the project file for your text game.
  3. Create two int variables named “firstChoice” and “secondChoice”.
  4. int firstChoice, secondChoice;
    
  5. Use a cout statement to present the player with a problem.
  6. cout << "After you deal with the wolf, you set off to your grandmother's house." << endl
         << "You travel the path until you come to a fork in the road." << endl;
    
  7. Use a cout statement to present the player with two numbered choices.
  8. cout << "1. Take the left fork." << endl
         << "2. Take the right fork." << endl;
    
  9. Use a cout statement to prompt the player for their choice.
  10. cout << "Enter 1 or 2: ";
    
  11. Use a cin statement to get the player’s choice and read it into “firstChoice”.
  12. cin >> firstChoice;
    
  13. Add an if portion to the selection statement that checks whether “firstChoice” is equal to 1.
  14. if (firstChoice == 1)
    {
        
    }
    
  15. Add an else if portion to the selection statement that checks whether “firstChoice” is equal to 2.
  16. else if (firstChoice == 2)
    {
        
    }
    
  17. Add an else portion to the selection statement.
  18. else
    {
        
    }
    
  19. Use a cout statement between the braces for the else portion of the selection statement to present a default outcome. This cout statement will only output this outcome if the player enters a choice other than 1 or 2.
  20. else
    {
       cout << "You're not sure what to do." << endl
            << "You turn around and head back home." << endl;
    }
    


Next, we will nest inner selection statements within our outer selection statement. The inner selection statements will give our players different choices to make, depending on which options they chose for the outer selection statement.

Part 2: Coding the Inner Selection Statements

  1. Use a cout statement inside of the braces for the if portion of your selection statement to present your player with a problem.
  2. cout << "You come upon a beautiful spring. You suddenly feel very thirsty." << endl
             << "Will you drink?" << endl;
    
  3. Use a cout statement to present your player with two numbered choices.
  4. 
    cout << "1. Drink." << endl
             << "2. Resist the urge to drink." << endl;
    
  5. Use a cout statement to prompt your player for their choice.
  6. cout << "Enter 1 or 2: ";
    
  7. Use a cin statement to get the player’s choice and read it into “secondChoice”.
  8. cin >> secondChoice;
    
  9. Add an if portion to the inner selection statement that checks whether “secondChoice” is equal to 1.
  10. 
    if (secondChoice == 1)
    {
            
    }
    
  11. Use a cout statement between the braces of the if portion to output the outcome for a choice of 1.
  12. {
           cout << "You drink and feel refreshed." << endl
                << "You continue on your way, picking up your pace." << endl;
    }
    
  13. Add an else portion to the inner selection statement.
  14. else
    {
    
    }
    
  15. Use a cout statement between the braces of the else portion to output the outcome of a choice of 2.
  16. {
       cout << "Reluctantly, you leave without drinking." << endl
            << "You continue on your way, thirsty and cranky." << endl;
    }
    
  17. Repeat steps 1 through 8 inside of the braces for the else if portion of the outer selection statement. Use a different problem, a different set of choices, and different outcomes.
  18. else if (firstChoice == 2) //player chose to take the right fork
    {
        cout << "There's a cute little bunny frolicking in a clearing." << endl
             << "Will you pet it?" << endl;
        cout << "1. Pet the bunny." << endl
             << "2. Leave the bunny alone." << endl;
        cout << "Enter 1 or 2: ";
        cin >> secondChoice;
    
        if (secondChoice == 1) //player chose to pet the rabbit
        {
            cout << "The bunny you're petting turns out to be a wizard, " << endl
                 << "who finds your petting offensive. He turns you into " << endl
                 << "a newt." << endl;
        }
        else //player did not choose to pet the rabbit
        {
            cout << "You go on your way without petting the rabbit, but " << endl
                 << "you know, somewhere in the back of your mind, that " << endl
                 << "you will always wonder about what could have been." << endl;
        }
    }
    
  19. Choose "Build" -> "Build."
  20. Choose "Build" -> "Run", if no errors were generated in Step 10.

Now, your text game’s story change as players make different choices. To make your text game’s story longer, you can place as many nested if statements inside of nested if statements as you need; experiment until you find something that works well for your text game.

If your code generated any errors, I've included my text game’s code in this Google Drive file, so you can check your text game’s code against mine. In our next tutorial, we will learn about how to give players more than one chance to input a valid choice with loops.

Tuesday, December 3, 2013

Giving Players Choices: Control Structures

In this post, we will learn how to use control structures to let our players choose what happens in our text game. Our text game will present players with two options to choose from, and will let them know the outcome of their choices.

Let’s begin.


Part 1: Using a Variable to Hold Players' Choices

  1. Run Code::Blocks.
  2. Choose “Open An Existing Project.”
  3. Find the project file for your text game (this file will have the name you chose for your project last time with a .cbp extension) and open it.
    This image shows the listing for a text game's project file. It is named "RedRidingHood.cbp"
    My text game's project file is named "RedRidingHood.cbp."
  4. Add a line after the end of the last cout statement in your text game.
  5. Create an int variable named "choice."
  6. int choice;
    
  7. Use a cout statement to present the player with a problem.
  8. cout << "A wild wolf appears! What will you do?" << endl;
    
  9. Use a cout statement to present the player with two numbered choices.
  10. cout << "1. Run from the wolf." << endl 
         << "2. Attempt to engage the wolf in meaningful dialogue." << endl;
    
  11. Use a cout statement to prompt the player for a choice.
  12. cout << "Enter 1 or 2: ";
    
  13. Use a cin statement to get the player's choice.
  14. cin >> choice;
    
  15. Choose "Build" -> "Build."
  16. Choose "Build" -> "Run", if no errors were generated in Step 10.

Part 2: Using a Selection Statement to Determine a Choice's Outcome


A control structure in C++ determines if a series of statements should be executed based on whether a condition is true or false. The control structure we will use is called a selection statement. If the condition is true, then the program executes the statements associated with the if portion of the selection statement (which means that the program will execute any statements between the brackets below the line with the condition). If the condition is false, then the program executes the statements below the else portion of the selection statement.The condition we will use for our control structure is the “choice” variable. If the “choice” variable holds 1, the program will execute statements that output the outcome of the first choice; if the “choice” variable holds 2, the program will execute statements that outputs the outcome of the second choice.


  1. Add a new line and write
  2. if (choice == 1)
    
  3. Add an opening brace on the next line. Code::Blocks will automatically add a closing brace for you two lines down.
  4. Use a cout statement to output the outcome of the first choice between the two braces.
  5. {
        cout << "You flee from the wolf." << endl
             << "The wolf wanders off dejectedly into the forest." << endl;
    }
    
  6. Add a new line below the closing brace and write
  7. else
    
  8. Add another opening brace and closing brace.
  9. Use a cout statement between the two braces to output the outcome of the second choice.
  10. {
        cout << "You speak with the wolf, who got lost in the forest looking for his friend, the huntsman." << endl
             << "You give the wolf directions and he thanks you profusely before continuing on his way." << endl;
    }
    
  11. Choose "Build" -> "Build."
  12. Choose "Build" -> "Run", if no errors were generated in Step 7.
We wrote an if portion of a selection statement in Step 1. The if portion of the selection statement contained the condition inside of a set of parentheses. The condition for our selection statement checked whether our “choice” variable held a value of 1 using a pair of equal signs. This pair of equal signs is called the equal to operator in C++. The equal to operator checks whether the value on the left is equal to the value on the right. If the value on the left is equal to the value on the right, then the program evaluates the condition as true. If the value on the left is not equal to the value on the right, then the program evaluates the condition to false. In the example from my Red Riding Hood game, if the player enters 1 then the condition will evaluate to true and the player will flee from the wolf. If the player enters 2 then the condition will evaluate to false and the player will speak with the wolf.


Part 3: Giving Players More Choices with Else If


Unfortunately, if players enter any number but 1 for their choice, then the condition will evaluate to false and the game will execute the statements associated with the else portion of the selection statement. If players choose another number, we want our game to output an outcome that makes sense. We can use an else if portion to the selection statement to provide players with more than two possible outcomes.
  1. Find your else statement and change it to
  2. else if (choice == 2)
    
  3. Add another line below the brackets for the else if statement, and write
  4. else
    
  5. Add another opening brace and closing brace.
  6. Use a cout statement between the two braces to output the outcome of an invalid choice.
  7. {
        cout << "You're not sure what to do, so you hide behind a tree until the wolf leaves." << endl;
    }
    
  8. Choose "Build" -> "Build."
  9. Choose "Build" -> "Run", if no errors were generated in Step 5.

In this control structure, the program checks the condition for the if portion of the selection statement first. If the condition for the if portion of the selection statement evaluates to false, then the program checks the condition for the else if statement. If the condition for the else if portion of the selection statement evaluates to false, then the program executes the statements associated with the else portion of the selection statement.


You now have an interactive text game that lets players make choices. If your text game generated any errors, I have the code from today’s text game in this Google Drive file so you can check your code against mine. In our next post, we will learn more about control structures, and how to use them to create branching storylines affected by players' choices.


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. 

Friday, October 18, 2013

About Text Game Tutor

Are you interested in learning C++? Do you want to make a video game using only your own work? Would you like to practice writing interactive stories like those in video games? You can accomplish all of that by making text games—which are games that convey all the plot and action through text instead of graphics—and Text Game Tutor will help you learn how to do it.

Programming text games lets you:
      • Learn basic programming concepts by using them.
      • Make a complete game before you attempt complicated graphical programming.
      • Practice writing engaging, nonlinear stories. 
      • Create accessible games for a large, diverse audience.
I'm Ada Fisher, and I'm studying game design and programming at a university in Texas. To learn to program, I endured making more loan interest calculators in C++ than I care to count. C++ lets programmers do wonderful things, and C++ lessons must convey that truth to interest beginners in programming. Beginners need C++ lessons that reflect what interests them, and writing a text game indulges their inner artists, gamers, and writers while they learn C++.

Text Game Tutor exists to provide a comprehensive beginner’s guide to writing text games in C++.  This blog will feature detailed tutorials that explain each part of the process of making a text game, as well as interviews on writing text games with experienced programmers and teachers. These tutorials will include how to install the software you need to start writing C++ programs, how to create C++ programs that tell stories, and how to let players make choices in a text game. Each tutorial will improve on the program that you started with, until you have finished an entire text game. 

Subscribe to Text Game Tutor if you are a beginner who wants to learn to program in C++, if you are interested in making your own game, or if you want to make a text game to practice writing for video games. Although Text Game Tutor is a beginner’s guide, feedback from experienced programmers can help make this blog better. This blog’s tutorials will also increase in difficulty as times goes on; if you find one week’s tutorials a little too easy, you can join in later to learn more complicated concepts.