Choose Your Adventure Game - Part 2
For the next part of the game, get some input from the user. This code-along covers Console.ReadLine
.
Starting Point
For this code-along, start from the ending point of part one. If needed, fork this Repl project to begin.
Asking for the Name
Start by customizing the game based on the user's name.
- Make a new line in the code above where the
name
variable is set - Use a
Console.WriteLine
statement to ask the user for their name - Under that, use
Console.ReadLine
to get the name the user puts in- Replace the
name
variable value withConsole.ReadLine
- Replace the
Run the code, and verify that it is possible to enter a name! The name should then be used to greet the user.
The code should look something like this:
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Asking Where to Go
Next, it's time to start making the game more interactive. So far, the program lists the player's surroundings, but does not let them do anything with them. In this section, ask the user where they would like to go.
- Make a new line under the line that prints the user's health
- Use
Console.WriteLine
to ask the user where they want to go- List the options: barn, tree, path
- Make a new line under that, and create a new
string
variable namedgo
- Set the
go
variable to aConsole.ReadLine()
value
Run the code, and verify that it is possible to enter an interaction! Nothing should happen with it yet, though.
The code should look something like this:
Console.WriteLine("\nWhere do you go? (barn, tree, path)");
string go = Console.ReadLine();
Showing Where to Go
Now the user has chosen where to go, so the program can show them what they chose. In the future, the program will be able to do different things based on the user's choice. For now, just print out a message with the choice.
- Make a new line under the existing lines
- Use
Console.WriteLine
to display a message showing the value of thego
variable
Run the code, and verify that the program can show the user where they choose to go! The code should look something like this:
Console.WriteLine("\nYou go to the " + go);
Final Program
By the end of this code-along, the main.cs file should look something like this:
using System;
class Program {
public static void Main (string[] args) {
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Clear();
Console.WriteLine("You awaken in a strange place.");
Console.WriteLine("You see a barn, a big tree, and a path through the woods.\n");
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
int health = 5;
Console.WriteLine("\nHello " + name);
Console.WriteLine("Your health is " + health);
Console.WriteLine("\nWhere do you go? (barn, tree, path)");
string go = Console.ReadLine();
Console.WriteLine("\nYou go to the " + go);
}
}