Posted By

MihailGochev on 10/21/12


Tagged

game console


Versions (?)

FallingRocks


 / Published in: C#
 

Falling Rocks console game homework for Telerik Academy

  1. using System;
  2. using System.Threading;
  3.  
  4. class FallingRocks
  5. {
  6. struct Coordinates
  7. {
  8. public int X, Y;
  9. public char Symbol;
  10. public ConsoleColor Color;
  11. public Coordinates(int x, int y, char Symbol, ConsoleColor Color)
  12. {
  13. this.X = x;
  14. this.Y = y;
  15. this.Symbol = Symbol;
  16. this.Color = Color;
  17. }
  18. }
  19.  
  20. static void Main()
  21. {
  22. int rockCount; //number of rocks
  23. int sleepTime; //speed difficulty
  24. int score = 0;
  25. string label; //used to see the lenght of the text so it can be used for positioning
  26.  
  27. Console.CursorVisible = false;
  28. Console.BufferHeight = Console.WindowHeight; //removing the scrolling of the console
  29.  
  30. while (true) //checking if we want to play again
  31. {
  32. Console.Clear();
  33. while (true) //running the game
  34. {
  35. label = "Please choose difficulty:";
  36. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2);
  37. Console.ForegroundColor = ConsoleColor.Magenta;
  38. Console.Write(label);
  39.  
  40. label = "Press 1 for easy.";
  41. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 1);
  42. Console.ForegroundColor = ConsoleColor.White;
  43. Console.Write(label);
  44.  
  45. label = "Press 2 for medium.";
  46. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 2);
  47. Console.ForegroundColor = ConsoleColor.Green;
  48. Console.Write(label);
  49.  
  50. label = "Press 3 for hard.";
  51. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 3);
  52. Console.ForegroundColor = ConsoleColor.Red;
  53. Console.Write(label);
  54.  
  55. if (Console.KeyAvailable)
  56. {
  57. ConsoleKeyInfo menuChoice = Console.ReadKey();
  58. if (menuChoice.Key == ConsoleKey.D1)
  59. {
  60. rockCount = 30;
  61. sleepTime = 150;
  62. break;
  63. }
  64. if (menuChoice.Key == ConsoleKey.D2)
  65. {
  66. rockCount = 60;
  67. sleepTime = 100;
  68. break;
  69. }
  70. if (menuChoice.Key == ConsoleKey.D3)
  71. {
  72. rockCount = 90;
  73. sleepTime = 75;
  74. break;
  75. }
  76. }
  77. }
  78. Console.Clear();
  79. score = game(rockCount, sleepTime); //running the game
  80. while (true)
  81. {
  82. label = "GAME OVER";
  83. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 3) / 2);
  84. Console.ForegroundColor = ConsoleColor.Red;
  85. Console.Write(label);
  86.  
  87. switch (sleepTime)
  88. {
  89. case 150:
  90. label = "Your score is " + score + " on Easy difficulty";
  91. break;
  92. case 100:
  93. label = "Your score is " + score + " on Medium difficulty";
  94. break;
  95. case 75:
  96. label = "Your score is " + score + " on Hard difficulty";
  97. break;
  98. default:
  99. break;
  100. }
  101. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 3) / 2 + 1);
  102. Console.ForegroundColor = ConsoleColor.Yellow;
  103. Console.Write(label);
  104.  
  105. label = "Do you want to play again?";
  106. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 3) / 2 + 2);
  107. Console.ForegroundColor = ConsoleColor.Yellow;
  108. Console.Write(label);
  109.  
  110. label = "Press 1 for YES or 2 for NO.\n";
  111. Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 3) / 2 + 3);
  112. Console.ForegroundColor = ConsoleColor.Cyan;
  113. Console.Write(label);
  114.  
  115. if (Console.KeyAvailable)
  116. {
  117. ConsoleKeyInfo menuChoice = Console.ReadKey();
  118. if (menuChoice.Key == ConsoleKey.D1)
  119. {
  120. break;
  121. }
  122. if (menuChoice.Key == ConsoleKey.D2)
  123. {
  124. return;
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. static int game(int rockCount, int sleepTime)
  132. {
  133. char[] rockSymbols = new char[10] { '#', '+', '&', '!', '=', '$', '~', '^', '?', '%' }; // symbols for the rocks, can be changed
  134. sbyte slowRocks = 1;
  135. double score = 0;
  136. int finalScore = 0;
  137. Random randomGenerator = new Random();
  138. Coordinates[] rocks = new Coordinates[rockCount];
  139.  
  140. Coordinates dwarf = new Coordinates();
  141. dwarf.X = Console.WindowWidth / 2;
  142. dwarf.Y = Console.WindowHeight - 1;
  143.  
  144. for (int i = 0; i < rockCount; i++) //generating random values for the coordinates,the color and the symbol of the rocks
  145. {
  146. rocks[i].X = randomGenerator.Next(1, (Console.WindowWidth - 1));
  147. rocks[i].Y = randomGenerator.Next(1, (Console.WindowHeight - 5));
  148. rocks[i].Color = (ConsoleColor)randomGenerator.Next(10, 16);
  149. rocks[i].Symbol = rockSymbols[randomGenerator.Next(1, rockSymbols.Length)];
  150. }
  151.  
  152. while (true)
  153. {
  154. if (slowRocks > 0) //making sure the dwarf moves twice faster than the rocks
  155. {
  156. for (int i = 0; i < rockCount; i++)
  157. {
  158. if (rocks[i].Y == (Console.WindowHeight - 1))
  159. {
  160. Console.SetCursorPosition(rocks[i].X, rocks[i].Y);
  161. Console.Write(" ");
  162. rocks[i].X = randomGenerator.Next(1, (Console.WindowWidth - 1));
  163. rocks[i].Y = randomGenerator.Next(0, 3);
  164. rocks[i].Color = (ConsoleColor)randomGenerator.Next(10, 16); //changing the color of the "new" rock
  165. rocks[i].Symbol = rockSymbols[randomGenerator.Next(1, rockSymbols.Length)]; //changing the symbol of the "new" rock
  166. }
  167. else
  168. {
  169. Console.SetCursorPosition(rocks[i].X, rocks[i].Y);
  170. Console.Write(" ");
  171. rocks[i].Y++;
  172. }
  173. Console.SetCursorPosition(rocks[i].X, rocks[i].Y);
  174. Console.ForegroundColor = rocks[i].Color;
  175. Console.Write(rocks[i].Symbol);
  176. }
  177. }
  178.  
  179. if (Console.KeyAvailable)
  180. {
  181. Console.SetCursorPosition(dwarf.X - 1, dwarf.Y);
  182. Console.Write(" "+" "+" "); // 3x" " because of bugged copy paste from the site.
  183. ConsoleKeyInfo pressedKey = Console.ReadKey();
  184. if (pressedKey.Key == ConsoleKey.RightArrow) dwarf.X++;
  185. if (pressedKey.Key == ConsoleKey.LeftArrow) dwarf.X--;
  186. }
  187.  
  188. //Checks if the dwarf is out of the console
  189. if ((dwarf.X == Console.WindowWidth - 2) || (dwarf.X < 1))
  190. {
  191. return (finalScore * 10);
  192. }
  193.  
  194. //Checks if there is a collision with the rocks
  195. for (int i = 0; i < rockCount; i++)
  196. {
  197. if (((dwarf.X == rocks[i].X) && (dwarf.Y == rocks[i].Y)) ||
  198. ((dwarf.X == rocks[i].X - 1) && (dwarf.Y == rocks[i].Y)) ||
  199. ((dwarf.X == rocks[i].X + 1) && (dwarf.Y == rocks[i].Y)))
  200. {
  201. Console.ForegroundColor = ConsoleColor.Red;
  202. Console.SetCursorPosition(dwarf.X - 1, dwarf.Y);
  203. Console.Write("\\@/");
  204. return (finalScore * 10);
  205. }
  206. }
  207.  
  208. Console.ForegroundColor = ConsoleColor.Cyan;
  209. Console.SetCursorPosition(dwarf.X - 1, dwarf.Y);
  210. Console.Write("\\@/");
  211.  
  212. Thread.Sleep(sleepTime);
  213. slowRocks *= -1;
  214. score += 0.1;
  215. finalScore = (int)score;
  216. }
  217. }
  218. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: BrockSamsom on April 4, 2013

awesome!

You need to login to post a comment.