Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //Adapted from TLG Learning C# course, taught by Peter Thorsteinson.
  2. using System;
  3. class Program
  4. {
  5. static int rows = 50;
  6. static int cols = 50;
  7. static int x = rows / 2;
  8. static int y = cols / 2;
  9. static char[,] cells = new char[rows, cols];
  10. static bool isDoubleBuffering = false;
  11. static void Main()
  12. {
  13. Console.WriteLine("Commands: arrow keys to move, s to single buffer, d to double buffer, and q to quit.");
  14. Console.WriteLine("Hit any key to proceed.");
  15. Console.ReadKey();
  16. Console.Clear();
  17. // cols*2 to compensate for font aspect ratio that is not square
  18. // rows+1 to line at bottom to see last line displayed
  19. Console.SetWindowSize(cols * 2, rows + 1);
  20. for (int row = 0; row < rows; row++)
  21. {
  22. for (int col = 0; col < cols; col++)
  23. {
  24. cells[row, col] = ' ';
  25. }
  26. }
  27. Draw(x, y, '*');
  28. while (true)
  29. {
  30. ConsoleKeyInfo keyPressed = Console.ReadKey();
  31. if (keyPressed.Key == ConsoleKey.Q) break;
  32. //Draw(x, y, ' ');
  33. switch (keyPressed.Key)
  34. {
  35. case ConsoleKey.UpArrow:
  36. x--;
  37. if (x == -1) x = rows - 1;
  38. break;
  39. case ConsoleKey.DownArrow:
  40. x++;
  41. if (x == cols) x = 0;
  42. break;
  43. case ConsoleKey.LeftArrow:
  44. y--;
  45. if (y == -1) y = cols - 1;
  46. break;
  47. case ConsoleKey.RightArrow:
  48. y++;
  49. if (y == rows) y = 0;
  50. break;
  51. case ConsoleKey.S:
  52. isDoubleBuffering = false;
  53. break;
  54. case ConsoleKey.D:
  55. isDoubleBuffering = true;
  56. break;
  57. }
  58. Draw(x, y, '|');
  59. }
  60. }
  61. static void Draw(int x, int y, char c)
  62. {
  63. if (isDoubleBuffering)
  64. {
  65. DoubleBufferDraw(x, y, c);
  66. }
  67. else
  68. {
  69. SingleBufferDraw(x, y, c);
  70. }
  71. }
  72. static void SingleBufferDraw(int x, int y, char c)
  73. {
  74. if (x >= 0 && x < rows && y >= 0 && y < cols) cells[x, y] = c;
  75. Console.Clear();
  76. for (int row = 0; row < rows; row++)
  77. {
  78. for (int col = 0; col < cols; col++)
  79. {
  80. Console.Write(cells[row, col] + " ");
  81. }
  82. Console.WriteLine();
  83. }
  84. }
  85. static void DoubleBufferDraw(int x, int y, char c)
  86. {
  87. if (x >= 0 && x < rows && y >= 0 && y < cols) cells[x, y] = c;
  88. Console.Clear();
  89. string screenBuffer = "";
  90. for (int row = 0; row < rows; row++)
  91. {
  92. for (int col = 0; col < cols; col++)
  93. {
  94. //Console.Write(cells[row, col] + " ");
  95. screenBuffer += cells[row, col] + " ";
  96. }
  97. screenBuffer += "\n";
  98. }
  99. Console.WriteLine(screenBuffer);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement