ellapt

4.11.FallingRocks

Dec 18th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Text;
  5.  
  6. class FallingRocks
  7. {
  8. public static int newWidth;
  9. public static int newHeight;
  10. public static int oldWindowSizeX;
  11. public static int oldWindowSizeY;
  12. public static int oldBufferSizeX;
  13. public static int oldBufferSizeY;
  14.  
  15. public static StringBuilder randString;
  16. public static Random rand = new Random();
  17. public static ConsoleKey key = ConsoleKey.Spacebar;
  18. public static ConsoleKeyInfo keyInfo;
  19. public static int sleep = 150;
  20.  
  21. public static int startCount = 2;
  22. public static int difficulty = 0;
  23. public static int playerX = 0;
  24. public static int playerY = 0;
  25. public static bool movePlayer = true;
  26. public static int score = 0;
  27. public static bool redrawScore = true;
  28. public static bool playGame = true;
  29.  
  30. public static int lastDensity = 0;
  31. public static readonly char[] rockTypes = { 'X', '@', 'W', '&', '+', '%', '$', '#', 'D', '\u0001', '\u0002', 'O' };
  32. public static Queue<int> rockX = new Queue<int>();
  33. public static Queue<int> rockDensity = new Queue<int>();
  34. public static List<int> goodX = new List<int>();
  35. public static List<int> goodY = new List<int>();
  36. public static bool evenTurn = true;
  37.  
  38. public static void Main()
  39. {
  40. WelcomeScreen();
  41. while (playGame)
  42. {
  43. StartGame();
  44. key = Console.ReadKey(true).Key;
  45. while (key != ConsoleKey.Escape)
  46. {
  47. if (Console.KeyAvailable)
  48. {
  49. movePlayer = true;
  50. keyInfo = Console.ReadKey(true);
  51. key = keyInfo.Key;
  52. ClearInputStreamBuffer();
  53. }
  54. if (key == ConsoleKey.Spacebar) { PauseGame(); }
  55. Console.MoveBufferArea(0, 2, newWidth, newHeight - 3, 0, 3);
  56. DrawNewRocks();
  57. GetNewData();
  58. CheckCollision();
  59. DrawScore();
  60. MovePlayer();
  61. Thread.Sleep(sleep);
  62. if (!playGame) { break; }
  63. }
  64. ResetGame();
  65. }
  66. EndGame();
  67. }
  68.  
  69. public static void PauseGame()
  70. {
  71. Console.ReadKey(true);
  72. key = ConsoleKey.Enter;
  73. }
  74.  
  75. public static void ClearInputStreamBuffer()
  76. {
  77. while (Console.KeyAvailable)
  78. {
  79. Console.ReadKey(true);
  80. }
  81. }
  82.  
  83. public static void ResetGame()
  84. {
  85. if (key != ConsoleKey.Escape)
  86. {
  87. redrawScore = true;
  88. DrawScore();
  89. Console.Beep(100, 1000);
  90. Console.ForegroundColor = ConsoleColor.Magenta;
  91.  
  92. // Game Over
  93. Console.Clear();
  94. Console.SetCursorPosition(newWidth / 2 - 19, newHeight / 2 - 4 );
  95. Console.Write("Sorry! Try again!");
  96. Console.SetCursorPosition(newWidth / 2 - 19, newHeight / 2 -2);
  97. Console.Write("Your score is: {0}", score.ToString().PadRight(10, ' '));
  98. Console.SetCursorPosition(newWidth / 2 - 19, newHeight / 2);
  99. Console.Write("Enter:\t Play again");
  100. Console.SetCursorPosition(newWidth / 2 - 19, newHeight / 2 + 1 );
  101. Console.Write("Escape:\t Quit");
  102.  
  103. while (key != ConsoleKey.Enter && key != ConsoleKey.Escape)
  104. {
  105. key = Console.ReadKey(true).Key;
  106. if (key == ConsoleKey.Enter) { playGame = true; }
  107. if (key == ConsoleKey.Escape) { playGame = false; }
  108. }
  109. }
  110. else
  111. {
  112. playGame = false;
  113. }
  114.  
  115.  
  116. }
  117.  
  118. public static void WelcomeScreen()
  119. {
  120. Console.CursorVisible = true;
  121. // Set Colors
  122. Console.BackgroundColor = ConsoleColor.DarkGreen;
  123. Console.ForegroundColor = ConsoleColor.Magenta;
  124. Console.Clear();
  125.  
  126. // Get Original Window Size
  127. oldWindowSizeX = Console.WindowWidth;
  128. oldWindowSizeY = Console.WindowHeight;
  129. oldBufferSizeX = Console.BufferWidth;
  130. oldBufferSizeY = Console.BufferHeight;
  131.  
  132. // Set Program New Window Size
  133. newWidth = Math.Min(Console.LargestWindowWidth, 80);
  134. newHeight = Math.Min(Console.LargestWindowHeight, 40);
  135. Console.SetWindowSize(newWidth, newHeight);
  136. Console.BufferWidth = newWidth;
  137. Console.BufferHeight = newHeight;
  138.  
  139. // Set Difficulty
  140. difficulty = Math.Max(2, newWidth / 20);
  141.  
  142. // Welcome Screen
  143. Console.SetCursorPosition(newWidth / 2 - 18, newHeight / 2 - 9);
  144. Console.Write("WELCOME TO THE FALLING ROCKS GAME!");
  145. Console.SetCursorPosition(newWidth / 2 - 18, newHeight / 2 - 7);
  146. Console.Write("Arrow keys:\t Move left and right");
  147. Console.SetCursorPosition(newWidth / 2 - 18, newHeight / 2 - 6);
  148. Console.Write("Shift:\t\t Move faster");
  149. Console.SetCursorPosition(newWidth / 2 - 18, newHeight / 2 - 4);
  150. Console.Write("Press Spacebar to Pause the game.");
  151. Console.SetCursorPosition(newWidth / 2 - 18, newHeight / 2 - 2);
  152. Console.Write("Press any key to continue.");
  153. Console.ReadKey(true);
  154. }
  155.  
  156. public static void CheckCollision()
  157. {
  158. if (startCount < newHeight - 1)
  159. {
  160. startCount++;
  161. }
  162. else
  163. {
  164. for (int i = 0; i < rockDensity.Peek(); i++)
  165. {
  166. if (rockX.Peek() % newWidth >= (playerX - 1) && rockX.Peek() % newWidth <= (playerX + 1))
  167. {
  168. playGame = false;
  169. }
  170. if (rockX.Peek() >= newWidth)
  171. {
  172. goodX.RemoveAt(0);
  173. goodY.RemoveAt(0);
  174. }
  175. rockX.Dequeue();
  176. }
  177. rockDensity.Dequeue();
  178. }
  179. }
  180.  
  181. public static void GetNewData()
  182. {
  183. for (int i = 0; i < randString.Length; i++)
  184. {
  185. if (randString[i] != ' ')
  186. {
  187. rockX.Enqueue(i + newWidth);
  188. goodX.Add(i);
  189. goodY.Add(2);
  190. }
  191. }
  192. }
  193.  
  194. public static void DrawNewRocks()
  195. {
  196. lastDensity = rand.Next(difficulty);
  197. rockDensity.Enqueue(lastDensity);
  198. randString = new StringBuilder(lastDensity);
  199. for (int i = 0; i < (newWidth - lastDensity - 1); i++)
  200. {
  201. randString.Append(' ');
  202. }
  203. for (int i = 0; i < lastDensity; i++)
  204. {
  205. randString.Insert(1 + rand.Next(randString.Length - 1), rockTypes[rand.Next(rockTypes.Length)] );
  206. }
  207. Console.SetCursorPosition(0, 2);
  208. Console.ForegroundColor = ConsoleColor.Yellow;
  209. Console.Write(randString.ToString());
  210. }
  211.  
  212. public static void MovePlayer()
  213. {
  214. if (movePlayer)
  215. {
  216. movePlayer = false;
  217. if (key == ConsoleKey.LeftArrow && playerX > 2)
  218. {
  219. //ErasePlayer();
  220. if (keyInfo.Modifiers != ConsoleModifiers.Shift)
  221. {
  222. playerX--;
  223. }
  224. else
  225. {
  226. playerX = playerX - 2;
  227. }
  228. }
  229. else if (key == ConsoleKey.RightArrow && playerX < (newWidth - 3))
  230. {
  231. //ErasePlayer();
  232. if (keyInfo.Modifiers != ConsoleModifiers.Shift)
  233. {
  234. playerX++;
  235. }
  236. else
  237. {
  238. playerX = playerX + 2;
  239. }
  240. }
  241. }
  242. if (playGame) { DrawPlayer(); }
  243. }
  244.  
  245. public static void DrawPlayer()
  246. {
  247. Console.SetCursorPosition(playerX - 1, playerY);
  248. Console.ForegroundColor = ConsoleColor.Magenta;
  249. Console.Write("(0)");
  250. }
  251.  
  252. public static void ErasePlayer()
  253. {
  254. Console.SetCursorPosition(playerX - 1, playerY);
  255. Console.Write(" ");
  256. }
  257.  
  258. public static void DrawScore()
  259. {
  260. if (redrawScore)
  261. {
  262. redrawScore = false;
  263. Console.SetCursorPosition(0, 0);
  264. Console.ForegroundColor = ConsoleColor.Magenta;
  265. Console.Write("Score: {0} ", score);
  266. }
  267. }
  268.  
  269. public static void SetBeginValues()
  270. {
  271. startCount = 2;
  272. score = 0;
  273. redrawScore = true;
  274. rockX.Clear();
  275. rockDensity.Clear();
  276. goodX.Clear();
  277. goodY.Clear();
  278. evenTurn = true;
  279. }
  280.  
  281. public static void StartGame()
  282. {
  283. Console.CursorVisible = false;
  284. SetBeginValues();
  285. Console.Clear();
  286. Console.ForegroundColor = ConsoleColor.Magenta;
  287.  
  288. //------First Drawings
  289. for (int i = 0; i < newWidth; i++)
  290. {
  291. Console.SetCursorPosition(i, 1);
  292. Console.Write('.');
  293. }
  294. Console.SetCursorPosition(newWidth - 30, 0);
  295. Console.Write("Space = Pause Escape = Quit");
  296. DrawScore();
  297. playerX = newWidth / 2;
  298. playerY = newHeight - 1;
  299. DrawPlayer();
  300. }
  301.  
  302. public static void EndGame()
  303. {
  304. Console.CursorVisible = true;
  305. // Restore Original Window Size And Colors
  306. Console.ResetColor();
  307. Console.Clear();
  308. Console.SetWindowSize(oldWindowSizeX, oldWindowSizeY);
  309. Console.BufferWidth = oldBufferSizeX;
  310. Console.BufferHeight = oldBufferSizeY;
  311. }
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment