Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. namespace JustSnake
  7. {
  8. struct Position
  9. {
  10. public int row;
  11. public int column;
  12. public Position(int row, int col)
  13. {
  14. this.row = row;
  15. this.column = col;
  16. }
  17. }
  18.  
  19. class Program
  20. {
  21. static void Main(string[] args)
  22. {
  23. byte right = 0;
  24. byte left = 1;
  25. byte down = 2;
  26. byte up = 3;
  27. int lastcandyTime = 0;
  28. int candyDissapearTime = 10000;
  29.  
  30. Console.BackgroundColor = ConsoleColor.Black;
  31. Console.CursorVisible = (false);
  32. Console.Title = "Spimle Snake ALPHA";
  33.  
  34. Position[] directions = new Position[]
  35. {
  36. new Position(0, 1), // prawo
  37. new Position(0, -1), // lewo
  38. new Position(1, 0), // w dół
  39. new Position(-1, 0), // w górę
  40. };
  41. double sleepTime = 100;
  42. int direction = right;
  43. Random randomNumbersGenerator = new Random();
  44. Console.BufferHeight = Console.WindowHeight;
  45. lastcandyTime = Environment.TickCount;
  46.  
  47. List<Position> obstacles =
  48. new List<Position>()
  49. {
  50. new Position(1, 4),
  51. new Position(3, 12),
  52. new Position(5, 20),
  53. new Position(7, 28),
  54. new Position(9, 36),
  55. new Position(11, 44),
  56. new Position(13, 52),
  57. new Position(15, 60),
  58. new Position(17, 68),
  59. new Position(19, 76),
  60. new Position(21, 84),
  61. new Position(23, 92),
  62. new Position(25, 100),
  63. new Position(27, 108),
  64. };
  65. foreach (Position obstacle in obstacles) //Przeszkody
  66. {
  67. Console.ForegroundColor = ConsoleColor.DarkRed;
  68. Console.SetCursorPosition(obstacle.column, obstacle.row);
  69. Console.Write("X");
  70. }
  71.  
  72. Queue<Position> snakeElements =
  73. new Queue<Position>();
  74. for (int i = 0; i <= 5; i++)
  75. {
  76. snakeElements.Enqueue(new Position(0, i));
  77. }
  78.  
  79. Position candy;
  80. do
  81. {
  82. candy = new Position(
  83. randomNumbersGenerator.Next(0, Console.WindowHeight),
  84. randomNumbersGenerator.Next(0, Console.WindowWidth));
  85. }
  86. while (snakeElements.Contains(candy) || obstacles.Contains(candy));
  87. Console.SetCursorPosition(candy.column, candy.row);
  88. Console.ForegroundColor = ConsoleColor.DarkGreen;
  89. Console.Write("o");
  90.  
  91. foreach (Position position in snakeElements)
  92. {
  93. Console.SetCursorPosition(position.column,position.row);
  94. Console.ForegroundColor = ConsoleColor.DarkGray;
  95. Console.Write("■");
  96. }
  97.  
  98. while (true)
  99. {
  100.  
  101. if (Console.KeyAvailable)
  102. {
  103. ConsoleKeyInfo userInput = Console.ReadKey();
  104. if (userInput.Key == ConsoleKey.LeftArrow)
  105. {
  106. if (direction != right) direction = left;
  107. }
  108. if (userInput.Key == ConsoleKey.RightArrow)
  109. {
  110. if (direction != left) direction = right;
  111. }
  112. if (userInput.Key == ConsoleKey.UpArrow)
  113. {
  114. if (direction != down) direction = up;
  115. }
  116. if (userInput.Key == ConsoleKey.DownArrow)
  117. {
  118. if (direction != up) direction = down;
  119. }
  120. }
  121.  
  122. Position snakeHead = snakeElements.Last();
  123. Position nextDirection = directions[direction];
  124.  
  125. Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,snakeHead.column + nextDirection.column);
  126.  
  127. if (snakeNewHead.column < 0) snakeNewHead.column = Console.WindowWidth - 1;
  128.  
  129. if (snakeNewHead.row < 0)snakeNewHead.row = Console.WindowHeight - 1;
  130.  
  131. if (snakeNewHead.row >= Console.WindowHeight)snakeNewHead.row = 0;
  132.  
  133. if (snakeNewHead.column >= Console.WindowWidth)snakeNewHead.column = 0;
  134.  
  135. if (snakeElements.Contains(snakeNewHead)
  136. || obstacles.Contains(snakeNewHead))
  137. {
  138. Console.SetCursorPosition(0, 0);
  139. Console.ForegroundColor = ConsoleColor.Red;
  140. Console.WriteLine("KONIEC GRY!!!");
  141. int userPoints = (snakeElements.Count - 6);
  142. if (userPoints > 5)
  143. {
  144. Console.WriteLine("Liczba uzyskanych punktów to {0}, gratulacje!", userPoints);
  145. }
  146. else
  147. Console.WriteLine("musisz sie postarac ;/ liczba twoich punktow nie przekroszyla 5! Twoj wynik to {0}", userPoints);
  148. Console.ReadLine();
  149. return;
  150. }
  151.  
  152. Console.SetCursorPosition(snakeHead.column,snakeHead.row);
  153. Console.ForegroundColor = ConsoleColor.DarkGray;
  154. Console.Write("=");
  155.  
  156. snakeElements.Enqueue(snakeNewHead);
  157. Console.SetCursorPosition(snakeNewHead.column,snakeNewHead.row);
  158. Console.ForegroundColor = ConsoleColor.Gray;
  159. if (direction == right) Console.Write(">");
  160. if (direction == left) Console.Write("<");
  161. if (direction == up) Console.Write("^");
  162. if (direction == down) Console.Write("v");
  163.  
  164.  
  165. if (snakeNewHead.column == candy.column &&snakeNewHead.row == candy.row)
  166. {
  167. // Karmienie snake'a
  168. do
  169. {
  170. candy = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth));
  171. }
  172. while (snakeElements.Contains(candy) || obstacles.Contains(candy));
  173. lastcandyTime = Environment.TickCount;
  174. Console.SetCursorPosition(candy.column, candy.row);
  175. Console.ForegroundColor = ConsoleColor.DarkGreen;
  176. Console.Write("o");
  177. sleepTime--;
  178.  
  179. Position obstacle = new Position();
  180. do
  181. {
  182. obstacle = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth));
  183. }
  184. while (snakeElements.Contains(obstacle) || obstacles.Contains(obstacle) || (candy.row != obstacle.row && candy.column != obstacle.row));
  185. obstacles.Add(obstacle);
  186. Console.SetCursorPosition(obstacle.column,obstacle.row);
  187. Console.ForegroundColor = ConsoleColor.Red;
  188. Console.Write("X");
  189. }
  190. else
  191. {
  192. // poruszanie się
  193. Position last = snakeElements.Dequeue();
  194. Console.SetCursorPosition(last.column, last.row);
  195. Console.Write(" ");
  196. }
  197.  
  198. if (Environment.TickCount - lastcandyTime >= candyDissapearTime)
  199. {
  200. Console.SetCursorPosition(candy.column, candy.row);
  201. Console.Write(" ");
  202. do
  203. {
  204. candy = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),randomNumbersGenerator.Next(0, Console.WindowWidth));
  205. }
  206. while (snakeElements.Contains(candy) || obstacles.Contains(candy));
  207. lastcandyTime = Environment.TickCount;
  208. }
  209.  
  210. Console.SetCursorPosition(candy.column, candy.row);
  211. Console.ForegroundColor = ConsoleColor.DarkGreen;
  212. Console.Write("o");
  213.  
  214. sleepTime -= 0.01;
  215.  
  216. Thread.Sleep((int)sleepTime);
  217. }
  218. }
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement