Program.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace A_console_ascii_animation
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string AsciiMap = Properties.Resources.ascii_map;
  13. const int AsciiMapHeight = 28; // <-- ASCII Map each line chars count
  14. const int AsciiMapWidth = 42; // <-- ASCII Map lines count
  15. const int AsciiMapLinesOffset = 1; // <-- each line ends with "\n" character
  16. Console.BufferWidth = Console.WindowWidth = Console.LargestWindowHeight - 2;
  17. Console.BufferHeight = Console.WindowHeight = Console.LargestWindowHeight - 1;
  18. Console.WriteLine("Press any key to continue");
  19. Console.ReadKey(true);
  20. Console.Clear();
  21. //Default console color:
  22. Console.ForegroundColor = ConsoleColor.White;
  23. Console.BackgroundColor = ConsoleColor.Black;
  24. //an array of forground-colors for each sequence of animation
  25. ConsoleColor[] palet = { ConsoleColor.White, ConsoleColor.White, ConsoleColor.Gray, ConsoleColor.DarkGray, ConsoleColor.Black };
  26. Random rnd = new Random();
  27. Console.CursorVisible = false;
  28. for (int seq = 0; true; seq++)
  29. {
  30. //counting sequences of animation
  31. //when all posible positions for cursor had been wrote a sequence compeleted
  32. List<int[]> pos = new List<int[]> { new int[2] };
  33. for (int t = 0; t <= Console.BufferHeight - 2; t++)
  34. for (int l = 0; l <= Console.BufferWidth - 1; l++)
  35. pos.Add(new int[] { l, t });
  36. //a list of all posible cursor positions
  37. //I used a list to make sure that each position will use just once and also know when this sequence will finish
  38. //this sequence foreground color
  39. ConsoleColor foregroundColor = palet[seq >= palet.Length ? palet.Length - 1 : seq];
  40. //a dictionary of characters foreground and background color
  41. Dictionary<char, ConsoleColor[]> CharsColorDict = new Dictionary<char, ConsoleColor[]>();
  42. CharsColorDict['.'] = new ConsoleColor[]{
  43. (seq >= 2 ? ConsoleColor.Black : ConsoleColor.White), //foreground color
  44. (seq<=5?ConsoleColor.Blue:(ConsoleColor)(seq%15)) //background color, this will change in each sequence
  45. };
  46. CharsColorDict['#'] = new ConsoleColor[]{
  47. (seq >= 2 ? ConsoleColor.Gray : ConsoleColor.White), //foreground color
  48. ConsoleColor.White //background color
  49. };
  50. while (pos.Count > 0)
  51. {
  52. //choosing a random position:
  53. int r = rnd.Next(pos.Count - 1);
  54. int[] p = pos[r];
  55. pos.RemoveAt(r);
  56. Console.SetCursorPosition(p[0], p[1]);
  57. //calculating postion of cursor in ASCII map:
  58. int l = p[0] - ((Console.BufferWidth - AsciiMapWidth) / 2); //left
  59. int t = p[1] - ((Console.BufferHeight - AsciiMapHeight) / 2); //top
  60. Console.ForegroundColor = foregroundColor;
  61. Console.BackgroundColor = ConsoleColor.Black;
  62. if (seq >= 1 && l >= 0 && t >= 0 && l < 42 && t < 28)
  63. {
  64. // Index = (top * (lines_width + lines_offset) ) + left
  65. int index = t * (AsciiMapWidth + AsciiMapLinesOffset) + l;
  66. char c = AsciiMap[index];
  67. if (CharsColorDict.ContainsKey(c))
  68. {
  69. Console.ForegroundColor = CharsColorDict[c][0];
  70. Console.BackgroundColor = CharsColorDict[c][1];
  71. }
  72. }
  73. Console.Write((char)rnd.Next((int)'!', (int)'z')); //a random character
  74. //System.Threading.Thread.Sleep(1); //wait after each sequence
  75. }
  76. }
  77. }
  78. }
  79. }