Advertisement
o91leg

Untitled

Mar 31st, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 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. using System.IO;
  7.  
  8. namespace ConsoleApp4
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Random random = new Random();
  15. Console.CursorVisible = false;
  16.  
  17. bool isPlaying = true;
  18. int pacmanX, pacmanY;
  19. int pacmanDX = 0, pacmanDY = 1;
  20. bool isAlive = true;
  21.  
  22. int ghostX, ghostY;
  23. int ghostDX = 0, ghostDY = -1 ;
  24. int allDots = 0;
  25. int collectDots = 0;
  26.  
  27. char[,] map = ReadMap("Map1", out pacmanX, out pacmanY, out ghostX, out ghostY, ref allDots);
  28.  
  29. DrawMap(map);
  30.  
  31. while (isPlaying)
  32. {
  33. Console.SetCursorPosition(0, 20);
  34. Console.WriteLine($"Собрано {collectDots}/{allDots}");
  35.  
  36. if (Console.KeyAvailable)
  37. {
  38. ConsoleKeyInfo key = Console.ReadKey(true);
  39. ChangeDirection(key,ref pacmanDX, ref pacmanDY);
  40.  
  41. }
  42. if (map[pacmanX + pacmanDX, pacmanY + pacmanDY] != '#')
  43. {
  44. CollectDots(map, pacmanX, pacmanY, ref collectDots);
  45. Move(map,'@',ref pacmanX, ref pacmanY, pacmanDX, pacmanDY);
  46. }
  47.  
  48. if (map[ghostX + ghostDX, ghostY + ghostDY] != '#')
  49. {
  50. Move(map,'$', ref ghostX, ref ghostY, ghostDX, ghostDY);
  51. }else
  52. {
  53. ChangeDirection(random, ref ghostDX, ref ghostDY);
  54. }
  55. System.Threading.Thread.Sleep(250);
  56.  
  57. if (ghostX == pacmanX && ghostY == pacmanY)
  58. {
  59. isAlive = false;
  60. }
  61. if (collectDots == allDots || !isAlive)
  62. {
  63. isPlaying = false;
  64. }
  65. }
  66. Console.SetCursorPosition(0, 21);
  67. if (collectDots== allDots)
  68. {
  69. Console.WriteLine("Вы победили! ");
  70. }
  71. else if (!isAlive)
  72. {
  73. Console.WriteLine("Вас съели");
  74. }
  75. }
  76.  
  77. static void Move(char[,] map, char symbol, ref int X, ref int Y, int DX, int DY)
  78. {
  79. Console.SetCursorPosition(Y, X);
  80. Console.Write(map[X,Y]);
  81.  
  82. X += DX;
  83. Y += DY;
  84.  
  85. Console.SetCursorPosition(Y, X);
  86. Console.Write(symbol);
  87. }
  88.  
  89. static void CollectDots (char [,] map, int pacmanX,int pacmanY, ref int collectDots )
  90. {
  91. if (map[pacmanX, pacmanY] == '.')
  92. {
  93. collectDots++;
  94. map[pacmanX, pacmanY] = ' ';
  95. }
  96. }
  97. static void ChangeDirection (ConsoleKeyInfo key, ref int DX, ref int DY)
  98. {
  99. switch (key.Key)
  100. {
  101. case ConsoleKey.UpArrow:
  102. DX = -1; DY = 0;
  103. break;
  104. case ConsoleKey.DownArrow:
  105. DX = 1; DY = 0;
  106. break;
  107. case ConsoleKey.LeftArrow:
  108. DX = 0; DY = -1;
  109. break;
  110. case ConsoleKey.RightArrow:
  111. DX = 0; DY = 1;
  112. break;
  113. }
  114. }
  115. static void ChangeDirection (Random random, ref int DX, ref int DY)
  116. {
  117. int ghostDir = random.Next(1, 5);
  118.  
  119. switch (ghostDir)
  120. {
  121. case 1:
  122. DX = -1; DY = 0;
  123. break;
  124. case 2:
  125. DX = 1; DY = 0;
  126. break;
  127. case 3:
  128. DX = 0; DY = -1;
  129. break;
  130. case 4:
  131. DX = 0; DY = 1;
  132. break;
  133. }
  134. }
  135. static void DrawMap(char[,] map)
  136. {
  137. for (int i = 0; i < map.GetLength(0); i++)
  138. {
  139. for (int j = 0; j < map.GetLength(1); j++)
  140. {
  141. Console.Write(map[i, j]);
  142. }
  143. Console.WriteLine();
  144. }
  145. }
  146. static char[,] ReadMap(string mapName, out int pacmanX, out int pacmanY,out int ghostX,out int ghostY, ref int allDots)
  147. {
  148. pacmanX = 0;
  149. pacmanY = 0;
  150. ghostX = 0;
  151. ghostY = 0;
  152. string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
  153. char[,] map = new char[newFile.Length, newFile[0].Length];
  154.  
  155. for (int i = 0; i < map.GetLength(0); i++)
  156. {
  157. for (int j = 0; j < map.GetLength(1); j++)
  158. {
  159. map[i, j] = newFile[i][j];
  160. if(map[i,j]== '@')
  161. {
  162. pacmanX = i;
  163. pacmanY = j;
  164. map[i, j] = '.';
  165. }
  166. else if(map[i, j] == '$')
  167. {
  168. ghostX = i;
  169. ghostY = j;
  170. map[i, j] = '.';
  171. }
  172. else if(map [i,j]== ' ')
  173. {
  174. map[i, j] = '.';
  175. allDots++;
  176. }
  177. }
  178. }
  179. return map;
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement