Choose Your Adventure Game - Part 1
Build the introduction to your adventure game: change the colors, print some text, and create some variables!
Getting Started
Fork this Repl to begin. It is a simple C# program that prints out a message. Update the program so that it prints a new message; instead of "Hello World," it should say "You awaken in a strange place."
Run the program, and verify that the message appears in the console.
using System;
class Program {
public static void Main (string[] args) {
Console.WriteLine("You awaken in a strange place.");
}
}
More Lines
Next up, it's time to add a couple more lines to print in the console.
- "You see a barn, a big tree, and a path through the woods"
- An empty message - just for extra space
The code for the next two lines should look something like this:
Console.WriteLine("You see a barn, a big tree, and a path through the woods.");
Console.WriteLine();
New Colors
Now the text is there, but the colors are still fairly boring. Luckily, it is possible to change the colors of the console in C# by setting variables.
- Make a new line above the existing
Console.WriteLine
- Set the
Console.ForegroundColor
variable to a value ofConsoleColor.White
- Under that, set the
Console.BackgroundColor
variable toConsoleColor.DarkRed
- Under that, clear the console with
Console.Clear();
- This sets the whole console to the colors
Run the program and verify that the new colors appear in the console! The code should look something like this:
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Clear();
Creating Variables
Next, add some more text. This time, create new variables to track the information.
- Create a new
string
variable namedname
set to"Jules"
- Create a new
int
variable namedhealth
set to5
- Under that, use
Console.WriteLine
to print out thename
value- "Hello " +
name
- "Hello " +
- Under that, use
Console.WriteLine
to print out thehealth
value- "Your health is " +
health
- "Your health is " +
Run the program and verify that the new text appears in the console! The code should look something like this:
string name = "Jules";
int health = 5;
Console.WriteLine("\nHello " + name);
Console.WriteLine("Your health is " + health);
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.");
Console.WriteLine();
string name = "Jules";
int health = 5;
Console.WriteLine("\nHello " + name);
Console.WriteLine("Your health is " + health);
}
}