Advertisement
LeRoY_Go

Untitled

Jun 13th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp2
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Console.CursorVisible = false;
  10. int playerX = 1, playerY = 1;
  11. int playerDX = 1, playerDY = 1;
  12. char[,] map = {{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  13. {'#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  14. {'#',' ','#','#','#',' ','#',' ',' ','#','#','#','#','#','#',' ','#'},
  15. {'#',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  16. {'#','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
  17. {'#',' ','#',' ','#','#','#',' ',' ','#','#',' ','#','#','#',' ','#'},
  18. {'#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ','#',' ','#'},
  19. {'#',' ',' ',' ','#','#','#',' ',' ','#',' ',' ','#',' ',' ',' ','#'},
  20. {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};
  21. DrawMap(map);
  22. while (true)
  23. {
  24. DrawPlayer(playerX, playerY);
  25. GetDirection(ref playerDX, ref playerDY);
  26. if (CheckCollision(map, ref playerX, ref playerY, playerDX, playerDY))
  27. {
  28. Move(map, ref playerX, ref playerY, playerDX, playerDY);
  29. }
  30. }
  31. }
  32. static void DrawMap(char[,] map)
  33. {
  34. for (int i = 0; i < map.GetLength(0); i++)
  35. {
  36. for (int j = 0; j < map.GetLength(1); j++)
  37. {
  38. Console.Write(map[i, j]);
  39. }
  40. Console.WriteLine();
  41. }
  42. }
  43. static bool CheckCollision(char[,] map, ref int playerX, ref int playerY, int playerDX, int playerDY)
  44. {
  45. return map[playerX + playerDX, playerY + playerDY] != '#';
  46. }
  47. static void Move(char[,] map, ref int playerX, ref int playerY, int playerDX, int playerDY)
  48. {
  49. Console.SetCursorPosition(playerY, playerX);
  50. Console.Write(" ");
  51. playerX += playerDX;
  52. playerY += playerDY;
  53. Console.SetCursorPosition(playerY, playerX);
  54. Console.Write("@");
  55. }
  56. static void GetDirection(ref int playerDX, ref int playerDY)
  57. {
  58. while (true)
  59. {
  60. if (Console.KeyAvailable)
  61. {
  62. ConsoleKeyInfo key = Console.ReadKey(true);
  63. switch (key.Key)
  64. {
  65. case ConsoleKey.UpArrow:
  66. playerDX = -1; playerDY = 0;
  67. break;
  68. case ConsoleKey.DownArrow:
  69. playerDX = 1; playerDY = 0;
  70. break;
  71. case ConsoleKey.LeftArrow:
  72. playerDX = 0; playerDY = -1;
  73. break;
  74. case ConsoleKey.RightArrow:
  75. playerDX = 0; playerDY = 1;
  76. break;
  77. }
  78. break;
  79. }
  80. }
  81. }
  82. static void DrawPlayer(int playerX, int playerY)
  83. {
  84. Console.SetCursorPosition(playerY, playerX);
  85. Console.Write("@");
  86. Console.ReadKey(true);
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement