Advertisement
TeT91

ДЗ: Brave new world

May 19th, 2024 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLight
  4. {
  5. internal class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. const ConsoleKey CommandMoveUp = ConsoleKey.UpArrow;
  10. const ConsoleKey CommandMoveDown = ConsoleKey.DownArrow;
  11. const ConsoleKey CommandMoveLeft = ConsoleKey.LeftArrow;
  12. const ConsoleKey CommandMoveRight = ConsoleKey.RightArrow;
  13.  
  14. bool isRunnig = true;
  15.  
  16. char[,] map =
  17. {
  18. {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
  19. {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  20. {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  21. {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  22. {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  23. {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  24. {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
  25. };
  26.  
  27. char emptySymbol = ' ';
  28. char playerSymbol = '@';
  29.  
  30. int playerPositionX = 1;
  31. int playerPositionY = 1;
  32. int moveDistance = 1;
  33.  
  34. Console.CursorVisible = false;
  35.  
  36. while (isRunnig)
  37. {
  38. Console.Clear();
  39.  
  40. DrawMap(map);
  41.  
  42. DrawPlayer(playerSymbol, playerPositionX, playerPositionY);
  43.  
  44. ConsoleKeyInfo pressedKey = Console.ReadKey();
  45.  
  46. HandleInput(pressedKey, CommandMoveUp, CommandMoveDown, CommandMoveLeft, CommandMoveRight, ref playerPositionX, ref playerPositionY, map, emptySymbol, moveDistance);
  47. }
  48. }
  49. private static void HandleInput(ConsoleKeyInfo pressedKey, ConsoleKey CommandMoveUp, ConsoleKey CommandMoveDown, ConsoleKey CommandMoveLeft, ConsoleKey CommandMoveRight, ref int positionX, ref int positionY, char[,] map, char emptySymbol, int moveDistance)
  50. {
  51. int[] direction = GetDirection(pressedKey, CommandMoveUp, CommandMoveDown, CommandMoveLeft, CommandMoveRight, moveDistance);
  52.  
  53. int nextPositionX = positionX + direction[0];
  54. int nextPositionY = positionY + direction[1];
  55.  
  56. if (map[nextPositionY, nextPositionX] == emptySymbol)
  57. {
  58. positionX = nextPositionX;
  59. positionY = nextPositionY;
  60. }
  61. }
  62. private static int[] GetDirection(ConsoleKeyInfo pressedKey, ConsoleKey CommandMoveUp, ConsoleKey CommandMoveDown, ConsoleKey CommandMoveLeft, ConsoleKey CommandMoveRight, int moveDistance)
  63. {
  64. int[] direction = { 0, 0 };
  65.  
  66. if (pressedKey.Key == CommandMoveUp)
  67. {
  68. direction[1] = -moveDistance;
  69. }
  70. else if (pressedKey.Key == CommandMoveDown)
  71. {
  72. direction[1] = moveDistance;
  73. }
  74. else if (pressedKey.Key == CommandMoveLeft)
  75. {
  76. direction[0] = -moveDistance;
  77. }
  78. else if (pressedKey.Key == CommandMoveRight)
  79. {
  80. direction[0] = moveDistance;
  81. }
  82.  
  83. return direction;
  84. }
  85. private static void DrawMap(char[,] map)
  86. {
  87. for (int i = 0; i < map.GetLength(0); i++)
  88. {
  89. for (int j = 0; j < map.GetLength(1); j++)
  90. {
  91. Console.Write(map[i, j]);
  92. }
  93.  
  94. Console.WriteLine();
  95. }
  96. }
  97.  
  98. private static void DrawPlayer(char playerSymbol, int positionX, int positionY)
  99. {
  100. Console.SetCursorPosition(positionX, positionY);
  101.  
  102. Console.Write(playerSymbol);
  103. }
  104. }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement