Advertisement
Nikitaantiov

Змейка228

Feb 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 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 Snake
  8. {
  9. class Program
  10. {
  11. static Point apple = null;
  12. static bool AppleE = false;
  13. class Snake
  14. {
  15. public Point head;
  16. public List<Point> points;
  17.  
  18. public Snake(Point start)
  19. {
  20. points = new List<Point> { start };
  21. }
  22.  
  23. public void Move(Point point)
  24. {
  25. points.Add(point);
  26. Console.SetCursorPosition(point.x, point.y);
  27. Console.Write("#");
  28.  
  29. if (point == apple)
  30. {
  31. AppleE = false;
  32. }
  33. else
  34. {
  35. Console.SetCursorPosition(points[0].x, points[0].y);
  36.  
  37. Console.Write(" ");
  38. points.RemoveAt(0);
  39. }
  40. }
  41. }
  42. class Point
  43. {
  44. public int x, y;
  45.  
  46. public Point()
  47. {
  48. y = size / 2;
  49. x = size;
  50. }
  51.  
  52. public Point(int x, int y)
  53. {
  54. this.x = x;
  55. this.y = y;
  56. }
  57.  
  58. static public bool operator ==(Point left, Point right)
  59. {
  60. if (left is null || right is null)
  61. {
  62. return false;
  63. }
  64. return ((left.x == right.x) && (left.y == right.y));
  65. }
  66. static public bool operator !=(Point left, Point right)
  67. {
  68. if (left is null || right is null)
  69. {
  70. return false;
  71. }
  72. return ((left.x != right.x) || (left.y != right.y));
  73. }
  74.  
  75. }
  76. static int size = 20;
  77. static void Map()
  78. {
  79. for (int i = 0; i <= size + 1; i++)
  80. {
  81. Console.SetCursorPosition(i * 2, 0); //Верхняя горизонталь
  82. Console.Write("X");
  83. Console.SetCursorPosition(0, i); //Левая горизонталь
  84. Console.Write("X");
  85. Console.SetCursorPosition(i * 2, size + 1); //Нижняя горизонталь
  86. Console.Write("X");
  87. Console.SetCursorPosition((size + 1) * 2, i); //Правая горизонталь
  88. Console.Write("X");
  89. }
  90. }
  91. static void GameOver()
  92. {
  93. Console.Clear();
  94. Console.ForegroundColor = ConsoleColor.Red;
  95. Console.WriteLine("Game Over!");
  96. }
  97. static bool Cont(Snake player)
  98. {
  99. ConsoleKeyInfo button;
  100.  
  101. Point HeadPos = new Point(player.points[0].x, player.points[0].y);
  102. button = Console.ReadKey(true);
  103. switch (button.Key)
  104. {
  105. case ConsoleKey.W:
  106. if (HeadPos.y == 1)
  107. {
  108. GameOver();
  109. return false;
  110. }
  111. else
  112. {
  113. HeadPos.y--;
  114. }
  115. break;
  116. case ConsoleKey.A:
  117. if (HeadPos.x == 1)
  118. {
  119. GameOver();
  120. return false;
  121. }
  122. else
  123. {
  124. HeadPos.x -= 2;
  125. }
  126. break;
  127. case ConsoleKey.S:
  128. if (HeadPos.y == size)
  129. {
  130. GameOver();
  131. return false;
  132. }
  133. else
  134. {
  135. HeadPos.y++;
  136. }
  137. break;
  138. case ConsoleKey.D:
  139. if (HeadPos.x == size * 2 + 1)
  140. {
  141. GameOver();
  142. return false;
  143. }
  144. else
  145. {
  146. HeadPos.x += 2;
  147. }
  148. break;
  149. }
  150. player.Move(HeadPos);
  151.  
  152. return true;
  153. }
  154. static void Main(string[] args)
  155. {
  156. Console.CursorVisible = false;
  157. Map();
  158.  
  159. Random r = new Random();
  160. Snake snake = new Snake(new Point(r.Next(1, size + 1) * 2 - 1, r.Next(1, size + 1)));
  161. Console.SetCursorPosition(snake.points[0].x, snake.points[0].y);
  162. Console.Write("#");
  163.  
  164.  
  165.  
  166.  
  167. while (Cont(snake))
  168. {
  169. if (snake.points[0] == apple)
  170. {
  171. AppleE = false;
  172. }
  173. if (!AppleE)
  174. {
  175. apple = new Point(r.Next(1, size + 1) * 2 - 1, r.Next(1, size + 1));
  176. Console.SetCursorPosition(apple.x, apple.y);
  177. Console.Write("*");
  178. AppleE = true;
  179. }
  180. }
  181. }
  182.  
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement