Advertisement
Blokatt

Bouncing text in .NET console using C#

Apr 15th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static int ColorIndex = 0;
  11. static double sinAngle = 0;
  12. static int xx = 0;
  13. static int yy = 0;
  14. static int hsp = 0;
  15. static int vsp = 0;
  16. private static ConsoleColor getCol(int index)
  17. {
  18. ConsoleColor col = ConsoleColor.Black;
  19. switch(index){
  20. case 0: col = ConsoleColor.Red; break;
  21. case 1: col = ConsoleColor.Green; break;
  22. case 2: col = ConsoleColor.Magenta; break;
  23. case 3: col = ConsoleColor.Cyan; break;
  24. case 4: col = ConsoleColor.Yellow; break;
  25. case 5: col = ConsoleColor.DarkGreen; break;
  26. case 6: col = ConsoleColor.DarkBlue; break;
  27. }
  28. return col;
  29.  
  30. }
  31.  
  32. static void Main(string[] args)
  33. {
  34. String str = Console.ReadLine();
  35. int width = str.Length;
  36.  
  37. Random rnd = new Random();
  38. hsp = rnd.Next(-2, 2);
  39. vsp = rnd.Next(-2, 2);
  40. xx = (int)((Console.WindowWidth / 2) - width / 2);
  41. yy = (int)(Console.WindowHeight / 2);
  42. Console.Clear();
  43. while (true)
  44. {
  45.  
  46.  
  47. if (Console.KeyAvailable)
  48. {
  49. if (Console.ReadKey().Key == ConsoleKey.Escape)
  50. {
  51. return;
  52. }
  53.  
  54. }
  55.  
  56. if ((xx + width + hsp) >= Console.WindowWidth)
  57. {
  58. hsp = -hsp;
  59. }
  60. if ((xx + hsp) <= 0)
  61. {
  62. hsp = -hsp;
  63. }
  64. if ((yy + vsp) >= Console.WindowHeight - 1)
  65. {
  66. vsp = -vsp;
  67. }
  68. if ((yy + vsp) <= 0)
  69. {
  70. vsp = -vsp;
  71. }
  72. xx += hsp;
  73. yy += vsp;
  74. Thread.Sleep(50);
  75. ColorIndex = (ColorIndex + 1) % 6;
  76. Console.Clear();
  77. Console.ForegroundColor = getCol(ColorIndex);
  78.  
  79.  
  80. Console.SetCursorPosition(xx, yy);
  81. for (int i = 0; i < str.Length; i++)
  82. {
  83. ColorIndex = (ColorIndex + 1) % 6;
  84. Console.ForegroundColor = getCol(ColorIndex);
  85. Console.Write(str.Substring(i, 1));
  86. }
  87. sinAngle += .1;
  88. Console.WriteLine();
  89.  
  90. }
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement