Advertisement
TwinFrame

ClassPlayer_PositionXY

Jan 22nd, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_22_OOP_ClassPlayer_xy
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Player player = new Player();
  10. Draw drawPlayer = new Draw();
  11.  
  12. int frameLines = 20;
  13. int frameRows = 10;
  14. char frameSymbol = '#';
  15.  
  16. bool isDraw = true;
  17.  
  18. while (isDraw)
  19. {
  20. Console.CursorVisible = false;
  21. DrawFrame(frameLines, frameRows, frameSymbol);
  22.  
  23. Console.WriteLine("\nПеремещайте игрока с помощью стрелок.\n");
  24. Console.WriteLine("F1 - Изменить размеры карты.");
  25. Console.WriteLine("F2 - Задать координаты игрока вручную.");
  26. Console.WriteLine("F3 - Сменить внешний вид игрока.");
  27. Console.WriteLine("F5 - Выход.");
  28.  
  29. drawPlayer.DrawPlayer(player.PlayerX, player.PlayerY, player.PlayerTexture);
  30.  
  31. int dX = 0;
  32. int dY = 0;
  33.  
  34. ConsoleKeyInfo key = Console.ReadKey(true);
  35. switch (key.Key)
  36. {
  37. case ConsoleKey.UpArrow:
  38. dX = 0; dY = -1;
  39. break;
  40. case ConsoleKey.DownArrow:
  41. dX = 0; dY = 1;
  42. break;
  43. case ConsoleKey.RightArrow:
  44. dX = 1; dY = 0;
  45. break;
  46. case ConsoleKey.LeftArrow:
  47. dX = -1; dY = 0;
  48. break;
  49.  
  50. case ConsoleKey.F1:
  51. int lenghtLine = CheckInputUser("Введите длину карты", 3, 50, frameRows);
  52. Console.WriteLine();
  53. int lenghtRow = CheckInputUser("Введите ширину карты", 3, 15, frameRows);
  54.  
  55. frameLines = lenghtLine;
  56. frameRows = lenghtRow;
  57.  
  58. player.DefaultXY();
  59. break;
  60.  
  61. case ConsoleKey.F2:
  62. int userX = CheckInputUser("Введите значение по X", 1, frameLines - 2, frameRows);
  63. int userY = CheckInputUser("Введите значение по Y", 1, frameRows - 2, frameRows);
  64.  
  65. player.DefaultXY(); //player.PlayerX и player.PlayerY = 1
  66. player.SetXY(userX - 1, userY - 1, frameLines, frameRows);
  67. break;
  68.  
  69. case ConsoleKey.F3:
  70. char userTexture = CheckInputUser("Введите символ игрока", frameRows);
  71.  
  72. player.SetTexture(userTexture);
  73. break;
  74.  
  75. case ConsoleKey.F5:
  76. Console.SetCursorPosition(0, frameRows + 8);
  77. Console.WriteLine("Пока!");
  78. Console.ReadKey();
  79. Environment.Exit(0);
  80. break;
  81. }
  82. player.SetXY(dX, dY, frameLines, frameRows);
  83. Console.Clear();
  84. }
  85. }
  86. static void DrawFrame(int frameLine, int frameRow, char symbol)
  87. {
  88. for (int j = 0; j < frameRow; j++)
  89. {
  90. for (int i = 0; i < frameLine; i++)
  91. {
  92. if ((i == 0 || j == 0) || (i == frameLine - 1 || j == frameRow - 1))
  93. {
  94. Console.SetCursorPosition(i, j);
  95. Console.Write(symbol);
  96. }
  97. }
  98. Console.WriteLine();
  99. }
  100. }
  101. static int CheckInputUser(string text, int minValue, int maxValue, int frameRows)
  102. {
  103. bool goodCheckInput;
  104. bool isCheckInput = true;
  105. int valueInput = 1;
  106. Console.CursorVisible = true;
  107.  
  108. while (isCheckInput)
  109. {
  110. Console.SetCursorPosition(0, frameRows + 8);
  111. Console.Write($"{text} от {minValue} до {maxValue}: ");
  112. string userInput = Console.ReadLine();
  113.  
  114. goodCheckInput = Int32.TryParse(userInput, out int value);
  115.  
  116. if (goodCheckInput != true || value < minValue || value > maxValue)
  117. {
  118. Console.WriteLine("Введите корректное число.");
  119. Console.ReadKey();
  120. Console.SetCursorPosition(0, frameRows + 8);
  121. Console.WriteLine(" ");
  122. Console.WriteLine(" ");
  123. }
  124. else
  125. {
  126. valueInput = value;
  127. isCheckInput = false;
  128. Console.SetCursorPosition(0, frameRows + 8);
  129. Console.WriteLine(" ");
  130. Console.WriteLine(" ");
  131.  
  132. }
  133. }
  134. return valueInput;
  135. }
  136. static char CheckInputUser(string text, int frameRows)
  137. {
  138. bool goodCheckInput;
  139. bool isCheckInput = true;
  140. char charInput = ' ';
  141. Console.CursorVisible = true;
  142.  
  143. while (isCheckInput)
  144. {
  145. Console.SetCursorPosition(0, frameRows + 8);
  146. Console.Write($"{text}: ");
  147. string userInput = Console.ReadLine();
  148.  
  149. goodCheckInput = Char.TryParse(userInput, out char symbol);
  150.  
  151. if (goodCheckInput != true)
  152. {
  153. Console.WriteLine("Введите любой, но только один символ.");
  154. Console.ReadKey();
  155. Console.SetCursorPosition(0, frameRows + 8);
  156. Console.WriteLine(" ");
  157. Console.WriteLine(" ");
  158. }
  159. else
  160. {
  161. charInput = symbol;
  162. isCheckInput = false;
  163. Console.SetCursorPosition(0, frameRows + 8);
  164. Console.WriteLine(" ");
  165. Console.WriteLine(" ");
  166.  
  167. }
  168. }
  169. return charInput;
  170. }
  171. }
  172. class Player
  173. {
  174. private int _playerX;
  175. private int _playerY;
  176. private char _playerTexture;
  177. public int PlayerX { get; }
  178. public int PlayerY { get; }
  179. public char PlayerTexture { get; }
  180. public Player(int x, int y, char texture)
  181. {
  182. _playerX = x;
  183. _playerY = y;
  184. _playerTexture = texture;
  185. }
  186. public Player()
  187. {
  188. _playerX = 1;
  189. _playerY = 1;
  190. _playerTexture = '@';
  191.  
  192. }
  193. public void SetXY(int dX, int dY, int frameLines, int frameRows)
  194. {
  195.  
  196. if ((_playerX + dX > 0 && _playerY + dY > 0) &&
  197. (_playerX + dX < frameLines - 1 && _playerY + dY < frameRows - 1))
  198. {
  199. _playerX += dX;
  200. _playerY += dY;
  201. }
  202. }
  203. public void DefaultXY()
  204. {
  205. _playerX = 1;
  206. _playerY = 1;
  207. }
  208. public void SetTexture(char symbol)
  209. {
  210. _playerTexture = symbol;
  211. }
  212. }
  213. class Draw
  214. {
  215. public void DrawPlayer(int x, int y, char texture)
  216. {
  217. Console.SetCursorPosition(x, y);
  218. Console.Write(texture);
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement