12. Colors and background
Displaying a background is simple: we would write different symbols before displaying the player, the ghosts and the dots. Later on, we will check collisions with the background, but let's not worry about that yet:
Console.WriteLine("Points: {0} - Lives: {1}"); Console.WriteLine("|----------------------------------------|"); Console.WriteLine("| |"); Console.WriteLine("| |---| -- |------| -- |---| |"); Console.WriteLine("| |---| || |--||--| || |---| |"); Console.WriteLine("| || || || |"); Console.WriteLine("|-------| ----- -- ----- |-------|"); Console.WriteLine(" | | | | "); Console.WriteLine(" | | |--- ---| | | "); Console.WriteLine("--------- - | | - ---------"); Console.WriteLine(" | | "); Console.WriteLine("--------- - |________| - ---------"); Console.WriteLine(" | | | | "); Console.WriteLine(" | | |--------| | | "); Console.WriteLine("|-------| | |---||---| | |-------|"); Console.WriteLine("| || |"); Console.WriteLine("| ----- |---| || |---| ----- |"); Console.WriteLine("| |-- | |---| -- |---| | --| |"); Console.WriteLine("| || || |"); Console.WriteLine("|--| || |----------------| || |--|"); Console.WriteLine("|--| -- |----------------| -- |--|"); Console.WriteLine("| |"); Console.WriteLine("|----------------------------------------|");
And changing the colors will make the background more distinguishable from the other items. For example, we might draw the background in blue, the dots in white, the ghosts in green and our character in yellow, using "Console.ForegroundColor":
Console.ForegroundColor = ConsoleColor.Yellow; Console.SetCursorPosition(x, y); Console.Write("C");
The result shold be simple:
/* First mini-console-game skeleton Version N: Background and colors */ using System; using System.Threading; // For the pause with Thread.Sleep public class Game01n { public static void Main() { int x=40, y=12; int amountOfDots = 100; int[] xDot = new int[amountOfDots]; int[] yDot = new int[amountOfDots]; bool[] visible = new bool[amountOfDots]; Random randomNumberGenerator = new Random(); for(int i=0; i<amountOfDots; i++) { xDot[i] = randomNumberGenerator.Next(0,80); yDot[i] = randomNumberGenerator.Next(1,25); visible[i] = true; } int amountOfEnemies = 4; float[] xEnemy = { 15, 40, 50, 60 }; float[] yEnemy = { 5, 10, 15, 20 }; float[] incrXEnemy = { 0.4f, 0.35f, 0.6f, 0.3f }; int score = 0; ConsoleKeyInfo userKey; // Game Loop while( 1 == 1 ) { // Draw Console.Clear(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Score: {0}",score); // Example background, contributed by Karol Campo Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("|----------------------------------------|"); Console.WriteLine("| |"); Console.WriteLine("| |---| -- |------| -- |---| |"); Console.WriteLine("| |---| || |--||--| || |---| |"); Console.WriteLine("| || || || |"); Console.WriteLine("|-------| ----- -- ----- |-------|"); Console.WriteLine(" | | | | "); Console.WriteLine(" | | |--- ---| | | "); Console.WriteLine("--------- - | | - ---------"); Console.WriteLine(" | | "); Console.WriteLine("--------- - |________| - ---------"); Console.WriteLine(" | | | | "); Console.WriteLine(" | | |--------| | | "); Console.WriteLine("|-------| | |---||---| | |-------|"); Console.WriteLine("| || |"); Console.WriteLine("| ----- |---| || |---| ----- |"); Console.WriteLine("| |-- | |---| -- |---| | --| |"); Console.WriteLine("| || || |"); Console.WriteLine("|--| || |----------------| || |--|"); Console.WriteLine("|--| -- |----------------| -- |--|"); Console.WriteLine("| |"); Console.WriteLine("|----------------------------------------|"); // Resto of the elements Console.ForegroundColor = ConsoleColor.White; for(int i=0; i<amountOfDots; i++) { if (visible[i]) { Console.SetCursorPosition(xDot[i], yDot[i]); Console.Write("."); } } Console.ForegroundColor = ConsoleColor.Yellow; Console.SetCursorPosition(x, y); Console.Write("C"); Console.ForegroundColor = ConsoleColor.Green; for(int i=0; i<amountOfEnemies; i++) { Console.SetCursorPosition((int)xEnemy[i], (int)yEnemy[i]); Console.Write("A"); } // Read keys and calculate new position if (Console.KeyAvailable) { userKey = Console.ReadKey(false); if(userKey.Key == ConsoleKey.RightArrow) x++; if(userKey.Key == ConsoleKey.LeftArrow) x--; if(userKey.Key == ConsoleKey.DownArrow) y++; if(userKey.Key == ConsoleKey.UpArrow) y--; } // Move enemies and environment for(int i=0; i<amountOfEnemies; i++) { xEnemy[i] += incrXEnemy[i]; if ((xEnemy[i] < 1) || (xEnemy[i] > 78)) incrXEnemy[i] = -incrXEnemy[i]; } // Collisions, lose energy or lives, etc for(int i=0; i<amountOfDots; i++) if ((x == xDot[i]) && (y == yDot[i]) && visible[i]) { score += 10; visible[i] = false; } // Pause till next fotogram Thread.Sleep(40); } } }
And would would look like this: