Advertisement
LeRoY_Go

Untitled

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