Advertisement
TargeTPoweR

Brave new world2

Apr 3rd, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Tasks
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. char[,] map = { { '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*' },
  14. { '*', ' ', ' ', ' ', '*',' ', ' ', ' ', ' ', ' ', '*' },
  15. { '*', ' ', ' ', ' ', '*',' ', ' ', ' ', ' ', ' ', '*' },
  16. { '*', ' ', ' ', ' ', '*',' ', ' ', ' ', ' ', ' ', '*' },
  17. { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
  18. { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
  19. { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
  20. { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
  21. { '*', ' ', '*', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
  22. { '*', ' ', '*', ' ', ' ',' ', ' ', ' ', '*', ' ', '*' },
  23. { '*', ' ', '*', ' ', ' ',' ', ' ', ' ', '*', ' ', '*' },
  24. { '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*' }};
  25.  
  26. Console.CursorVisible = false;
  27. int userX = 6, userY = 6;
  28. while (true)
  29. {
  30. DrawMap(ref map);
  31. Console.SetCursorPosition(userY, userX);
  32. Console.Write("$");
  33. ConsoleKeyInfo playerKey = Console.ReadKey();
  34. if (CheckWall(playerKey, ref map, ref userX, ref userY))
  35. {
  36. MovePlayer(playerKey,ref userX, ref userY);
  37. }
  38.  
  39. Console.Clear();
  40. }
  41. }
  42.  
  43. static void DrawMap(ref char[,] array)
  44. {
  45. for (int i = 0; i < array.GetLength(0); i++)
  46. {
  47. for (int j = 0; j < array.GetLength(1); j++)
  48. {
  49. Console.Write(array[i, j]);
  50. }
  51. Console.WriteLine();
  52. }
  53. }
  54.  
  55. static void MovePlayer(ConsoleKeyInfo playerInput, ref int xposition, ref int yPosition)
  56. {
  57. switch (playerInput.Key)
  58. {
  59. case ConsoleKey.UpArrow:
  60. xposition--;
  61. break;
  62. case ConsoleKey.DownArrow:
  63. xposition++;
  64. break;
  65. case ConsoleKey.LeftArrow:
  66. yPosition--;
  67. break;
  68. case ConsoleKey.RightArrow:
  69. yPosition++;
  70. break;
  71. }
  72. }
  73.  
  74. static bool CheckWall (ConsoleKeyInfo playerMove, ref char[,] playGround, ref int xposition, ref int yPosition)
  75. {
  76. char newPositionUp = playGround[xposition - 1, yPosition];
  77. char newPositionDown = playGround[xposition + 1, yPosition];
  78. char newPositionLeft = playGround[xposition, yPosition - 1];
  79. char newPositionRight = playGround[xposition, yPosition + 1];
  80.  
  81. bool notWall;
  82.  
  83. if (playerMove.Key == ConsoleKey.UpArrow && newPositionUp != '*')
  84. {
  85. notWall = true;
  86. }
  87. else if(playerMove.Key == ConsoleKey.DownArrow && newPositionDown != '*')
  88. {
  89. notWall = true;
  90. }
  91. else if(playerMove.Key == ConsoleKey.LeftArrow && newPositionLeft != '*')
  92. {
  93. notWall = true;
  94. }
  95. else if (playerMove.Key == ConsoleKey.RightArrow && newPositionRight != '*')
  96. {
  97. notWall = true;
  98. }
  99. else
  100. {
  101. notWall = false;
  102. }
  103.  
  104. return notWall;
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement