How to make a random number guessing game with Colorful.Console C#

[By JayDev64x] [Index]

Colorful.Console allows for the usage of colors within the console and there is no better way to show this off by making a very simple random number guessing game. It only uses a few variables and since it's random, it allows for a unique experience for every new game as opposed to using a preset variable for the answer.

using System; using System.Drawing; using Console = Colorful.Console; namespace Script { class Example { int lives = 10; int answer; Random random; void Start() { // Randomly selects the answer. random = new Random(); answer = random.Next(1, 100); Console.WriteLine("Try to guess a number 1-100!"); Console.WriteLine("Lives: " + lives, Color.Green); Game(); } // Main game loop. void Game() { int guess = Convert.ToInt32(Console.ReadLine()); Console.Clear(); // Used to determine if the guess is to high or to low. if (guess > answer) { Console.WriteLine("Your number is to high.", Color.Red); lives -= 1; } if (guess < answer) { Console.WriteLine("Your number is to low.", Color.Blue); lives -= 1; } // Used to determine if the guess is correct or you run out of lives. if (guess == answer) { Console.WriteLine("You won :D", Color.Green); Console.WriteLine("Press enter to reset.", Color.Green); Console.ReadLine(); Reset(); } if (lives == 0) { Console.WriteLine("You lost :(", Color.Red); Console.WriteLine("The number was " + answer + ".", Color.Red); Console.WriteLine("Press enter to reset.", Color.Red); Console.ReadLine(); Reset(); } // Determines the dynamic color of the life counter. if (lives <= 2) { Console.WriteLine("Lives: " + lives, Color.Red); } else if (lives <= 4) { Console.WriteLine("Lives: " + lives, Color.Orange); } else if (lives <= 7) { Console.WriteLine("Lives: " + lives, Color.Yellow); } else if (lives > 7) { Console.WriteLine("Lives: " + lives, Color.Green); } Game(); } // Used to reset the game after the player either wins or loses. void Reset() { Console.Clear(); lives = 10; Start(); } // Takes the program out of a static function. static void Main() { Example example = new Example(); example.Start(); } } }

Created [September 5, 2021]