Choose Your Path
In this code-along activity, create a basic game that allows the player to choose from a number of possible paths. In the story, the player will make choices that will determine their fate. The game will track their health
, which will change based on the choices. At the end, if the player survives, they will find a treasure. Otherwise, they will not be so lucky.
Setup
First, do some basic setup for the game.
- Create a new C# Repl project
- Name it "Choose Your Path"
- In the main.cs file, in the
Main
body, set the text color:Console.ForegroundColor = ConsoleColor.Red;
- Under that, set the background color:
Console.BackgroundColor = ConsoleColor.White;
- Under that, clear the console to make it a little cleaner:
Console.Clear();
- Next, use
Console.WriteLine
to display a message saying "Welcome to the Path" - Under that, add an empty
Console.WriteLine
to make some extra space
Run the program, and verify that the message appears in the proper color.
using System;
class MainClass
{
public static void Main (string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("Welcome to the Path.");
Console.WriteLine("");
}
}
Using a Variable
The game should track the health
of the player throughout the story. This will help determine the ultimate outcome.
- In the main.cs file, find the bottom of the
Main
body - There, declare a new
int
variable namedhealth
- Set the
health
variable to5
- Under that, display a message printing the current
health
value
Run the program, and verify that the proper health
value appears.
int health = 5;
Console.WriteLine("Your current health level is: " + health);
Asking a Question
Now it's time to display a choice for the player! This can be done using Console.WriteLine
statements. For this question, say the player comes across a large pastry, and they have to decide whether or not to eat it.
- Display a message asking the question
- Display option
"A"
: eat the pastry- Add some space in front of the option text to make it a little more readable
- Display option
"B"
: do not eat the pastry - Display an empty line just for some space
Run the program, and verify that the new messages appear in a readable format.
Console.WriteLine("You see a giant donut. What do you do?");
Console.WriteLine(" A. Eat it");
Console.WriteLine(" B. Ignore it");
Console.WriteLine("");
Getting an Answer
Next, the player should be able to answer the question. This can be done using Console.ReadLine
.
- First, use
Console.Write
(notWriteLine
) to show the user where to type:"> "
- Create a new
string
variable namedchoice
- Set the
choice
variable to the result of aConsole.ReadLine()
- Under that, use
Console.Clear()
to clean things up
Run the program again, and verify that it is possible to enter a choice!
Console.Write("> ");
string choice = Console.ReadLine();
Console.Clear();
Handling the Player's Choice
Now comes the actual game part. The code should look at what the player chose, and do different things depending on the choice.
- If the player chose "A", their
health
should go down by1
, and a message should say the pastry tasted good. - If the player chose "B", their
health
should go down by2
, and a message should say they are dealing with emotional pain.
This will be possible using conditional statements.
if
Setup
Start with an if
statement. This statement should check if the player chose "A" as their option.
- Under the
Console.Clear()
, make a new line - Type in
if
- After that, type in parentheses
()
- After the parentheses, type in curly brackets
{}
- Press enter within the curly brackets to space them out
- Within the
(
and)
, add a boolean expression checking if thechoice
variable value is equal to"A"
if (choice == "A")
{
}
if
Body
Now the if
condition is set up, but it needs to do something if the condition is true
!
- Within the
{
and}
, make a new line - Add code to set the
health
variable to equal itself minus1
- Under that, still within the
{
and}
, add aConsole.WriteLine
- Make the
WriteLine
say that the pastry tasted good
Run the program, and verify that entering "A" as the choice will make the message display! Entering anything else won't do anything yet...
if (choice == "A")
{
health = health - 1;
Console.WriteLine("You eat the donut, and it is delicious.");
}
else if
Now the "A"
choice has been handled, but what about "B"
? Use an else if
clause to handle the second option.
- Under the
}
from theif
, type inelse if
- After the
else if
, add parentheses()
- After the parentheses, add curly brackets
{}
- Make the
if
condition (between parentheses) check ifchoice
is"B"
- In the body (between curly brackets), set
health
to itself minus2
- Under that, still in the
else if
body, add aConsole.WriteLine
- Make the
WriteLine
say that the player is dealing with emotional pain
Run the program again, and verify that entering either option will display the proper result!
else if (choice == "B")
{
health = health - 2;
Console.WriteLine("You do not eat the donut, and the emotional pain takes its toll.");
}
Another Question
Adding another question to the game will be very similar to the first question. For the next question, ask the player how they want to get to school.
- Under the end of the
else if
block (after the}
), make a new line - Display a blank line, and then print out the current
health
variable valuable - Under that, display the question - ask the player how they get to school.
- Under that, display the
" A. Walk"
option - Similarly, display the
" B. Take the bus"
option - Display a new empty line
- Write the
"> "
for the player to answer usingConsole.Write
- Create a new variable named
choice2
of typestring
- Set the
choice2
variable toConsole.ReadLine()
- Clear the console to clean things up
Run the program again, and verify that the second question appears and they player can answer.
Console.WriteLine("");
Console.WriteLine("Your current health level is: " + health);
Console.WriteLine("You need to get to school. How do you get there?");
Console.WriteLine(" A. Walk");
Console.WriteLine(" B. Take the bus");
Console.WriteLine("");
Console.Write("> ");
string choice2 = Console.ReadLine();
Console.Clear();
Handling the Player's Second Choice
So what should happen based on the answers? If the player walks to school, their health should go up because they get some exercise. If they take the bus, they will inevitably trip and everyone will laugh at them. Write code to handle these results.
- Under the
Console.Clear
, create anif
structureif
keyword- Parentheses
- Curly brackets
- For the
if
condition, check ifchoice2
is"A"
- In the body of the
if
(between curly brackets):- Set
health
tohealth + 1
- Display a message saying that the exercise helped the health
- Set
- After the
}
, create anelse if
structureelse if
- Parentheses
- Curly brackets
- For the
else if
condition, check ifchoice2
is"B"
- In the body of the
else if
(between curly brackets):- Set
health
tohealth - 3
- Display a message saying that the player tripped and people laughed
- Set
Run the program again, and verify that both questions can be answered!
if (choice2 == "A")
{
health = health + 1;
Console.WriteLine("You get some exercise by walking.");
}
else if (choice2 == "B")
{
health = health - 3;
Console.WriteLine("You trip as you get on the bus, and everyone laughs.");
}
The End
Now that the player has gone through a decent amount of their day, it's time to end the game. If the player's health
value has dipped below 1
, they will lose the game. Otherwise, they wil find a bunch of money.
- Under the
else if
structure, make a new line - Use
Console.WriteLine
to display an empty line - Under that, create an entire
if
/else
structureif
keyword- Parentheses
- Curly brackets
else
keyword- Curly brackets
- For the
if
condition, check if thehealth
value is less than1
- In the body of the
if
, print a message saying the player has lost the game- This will happen if the condition is
true
- This will happen if the condition is
- In the body of the
else
, print a message saying the player has found some money- This will happen if the condition is
false
- This will happen if the condition is
Run the program, and verify that the proper results happen based on the choices made! The player should only lose if they choose not to eat the donut, and they choose to take the bus.
Console.WriteLine("");
if (health < 1)
{
Console.WriteLine("You have died.");
}
else
{
Console.WriteLine("When you get to school, you immediately find $500.");
}
Final Code
using System;
class MainClass
{
public static void Main (string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("Welcome to the Path.");
Console.WriteLine("");
int health = 5;
Console.WriteLine("Your current health level is: " + health);
Console.WriteLine("You see a giant donut. What do you do?");
Console.WriteLine(" A. Eat it");
Console.WriteLine(" B. Ignore it");
Console.WriteLine("");
Console.Write("> ");
string choice = Console.ReadLine();
Console.Clear();
if (choice == "A")
{
health = health - 1;
Console.WriteLine("You eat the donut, and it is delicious.");
}
else if (choice == "B")
{
health = health - 2;
Console.WriteLine("You do not eat the donut, and the emotional pain takes its toll.");
}
Console.WriteLine("");
Console.WriteLine("Your current health level is: " + health);
Console.WriteLine("You need to get to school. How do you get there?");
Console.WriteLine(" A. Walk");
Console.WriteLine(" B. Take the bus");
Console.WriteLine("");
Console.Write("> ");
string choice2 = Console.ReadLine();
Console.Clear();
if (choice2 == "A")
{
health = health + 1;
Console.WriteLine("You get some exercise by walking.");
}
else if (choice2 == "B")
{
health = health - 3;
Console.WriteLine("You trip as you get on the bus, and everyone laughs.");
}
Console.WriteLine("");
if (health < 1)
{
Console.WriteLine("You have died.");
}
else
{
Console.WriteLine("When you get to school, you immediately find $500.");
}
}
}