Advertisement
zontak

FallingRocks

Mar 26th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. struct Object
  5. {
  6. public int x;
  7. public int y;
  8. public char c;
  9. public ConsoleColor color;
  10. }
  11. class FallingRocks
  12. {
  13. static void PrintOnPosition(int x, int y, char c, ConsoleColor color = ConsoleColor.White)
  14. {
  15. Console.SetCursorPosition(x, y);
  16. Console.ForegroundColor = color;
  17. Console.Write(c);
  18. }
  19. static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Gray)
  20. {
  21. Console.SetCursorPosition(x, y);
  22. Console.ForegroundColor = color;
  23. Console.Write(str);
  24. }
  25. static void Main()
  26. {
  27. Console.BufferHeight = Console.WindowHeight = 20;
  28. Console.BufferWidth = Console.WindowWidth = 50;
  29.  
  30. int playField = 25;
  31. int livesCount = 5;
  32. int score = 0;
  33. int chance;
  34. int rockTypes = 12;
  35.  
  36. Object body = new Object();
  37. body.x = 12;
  38. body.y = Console.WindowHeight - 1;
  39. body.c = 'O';
  40. body.color = ConsoleColor.Gray;
  41.  
  42. Object leftHand = new Object();
  43. leftHand.x = 11;
  44. leftHand.y = Console.WindowHeight - 1;
  45. leftHand.c = '(';
  46. leftHand.color = ConsoleColor.Gray;
  47.  
  48. Object rightHand = new Object();
  49. rightHand.x = 13;
  50. rightHand.y = Console.WindowHeight - 1;
  51. rightHand.c = ')';
  52. rightHand.color = ConsoleColor.Gray;
  53.  
  54. Random rockDispenser = new Random();
  55. List<Object> rocks = new List<Object>();
  56.  
  57. Console.WriteLine("Welcome to Falling Rocks!");
  58. Console.WriteLine();
  59. Console.WriteLine("You are a dwarf \"(O)\" climbing a mountain but from the top of it a rock slide is heading your way!");
  60. Console.WriteLine(@"Your job is to avoid getting hit by any of the
  61. falling rocks.");
  62. Console.WriteLine(@"But everyone knows that despite being short,
  63. dwarfs are very strong and have incredibly tough
  64. bodies. " +
  65. @"You are able to suffer only 5 hits before finally getting swept away with the rest of the
  66. rocks.");
  67. Console.WriteLine("Good luck!");
  68. Console.WriteLine();
  69. Console.WriteLine("Press [enter] to begin.");
  70. Console.ReadLine();
  71.  
  72. while (true)
  73. {
  74. bool collision = false;
  75.  
  76. Object rock = new Object();
  77. chance = rockDispenser.Next(1, rockTypes + 1);
  78. switch (chance)
  79. {
  80. case 1:
  81. rock.x = rockDispenser.Next(0, playField + 1);
  82. rock.y = 0;
  83. rock.c = '^';
  84. rock.color = ConsoleColor.Blue;
  85. rocks.Add(rock);
  86. break;
  87. case 2:
  88. rock.x = rockDispenser.Next(0, playField + 1);
  89. rock.y = 0;
  90. rock.c = '@';
  91. rock.color = ConsoleColor.Gray;
  92. rocks.Add(rock);
  93. break;
  94. case 3:
  95. rock.x = rockDispenser.Next(0, playField + 1);
  96. rock.y = 0;
  97. rock.c = '*';
  98. rock.color = ConsoleColor.Cyan;
  99. rocks.Add(rock);
  100. break;
  101. case 4:
  102. rock.x = rockDispenser.Next(0, playField + 1);
  103. rock.y = 0;
  104. rock.c = '&';
  105. rock.color = ConsoleColor.Yellow;
  106. rocks.Add(rock);
  107. break;
  108. case 5:
  109. rock.x = rockDispenser.Next(0, playField + 1);
  110. rock.y = 0;
  111. rock.c = '+';
  112. rock.color = ConsoleColor.Cyan;
  113. rocks.Add(rock);
  114. break;
  115. case 6:
  116. rock.x = rockDispenser.Next(0, playField + 1);
  117. rock.y = 0;
  118. rock.c = '%';
  119. rock.color = ConsoleColor.Green;
  120. rocks.Add(rock);
  121. break;
  122. case 7:
  123. rock.x = rockDispenser.Next(0, playField + 1);
  124. rock.y = 0;
  125. rock.c = '$';
  126. rock.color = ConsoleColor.Red;
  127. rocks.Add(rock);
  128. break;
  129. case 8:
  130. rock.x = rockDispenser.Next(0, playField + 1);
  131. rock.y = 0;
  132. rock.c = '#';
  133. rock.color = ConsoleColor.DarkYellow;
  134. rocks.Add(rock);
  135. break;
  136. case 9:
  137. rock.x = rockDispenser.Next(0, playField + 1);
  138. rock.y = 0;
  139. rock.c = '!';
  140. rock.color = ConsoleColor.DarkRed;
  141. rocks.Add(rock);
  142. break;
  143. case 10:
  144. rock.x = rockDispenser.Next(0, playField + 1);
  145. rock.y = 0;
  146. rock.c = '.';
  147. rock.color = ConsoleColor.White;
  148. rocks.Add(rock);
  149. break;
  150. case 11:
  151. rock.x = rockDispenser.Next(0, playField + 1);
  152. rock.y = 0;
  153. rock.c = ';';
  154. rock.color = ConsoleColor.Magenta;
  155. rocks.Add(rock);
  156. break;
  157. case 12:
  158. rock.x = rockDispenser.Next(0, playField + 1);
  159. rock.y = 0;
  160. rock.c = '-';
  161. rock.color = ConsoleColor.White;
  162. rocks.Add(rock);
  163. break;
  164. default:
  165. break;
  166. }
  167.  
  168. while (Console.KeyAvailable)
  169. {
  170. ConsoleKeyInfo pressedKey = Console.ReadKey();
  171.  
  172. if (pressedKey.Key == ConsoleKey.LeftArrow)
  173. {
  174. if (leftHand.x - 1 >= 0)
  175. {
  176. leftHand.x -= 1;
  177. body.x -= 1;
  178. rightHand.x -= 1;
  179. }
  180. }
  181. else if (pressedKey.Key == ConsoleKey.RightArrow)
  182. {
  183. if (rightHand.x + 1 <= playField)
  184. {
  185. leftHand.x += 1;
  186. body.x += 1;
  187. rightHand.x += 1;
  188. }
  189. }
  190. }
  191.  
  192. List<Object> newRocks = new List<Object>();
  193.  
  194. for (int i = 0; i < rocks.Count; i++)
  195. {
  196. Object oldRock = rocks[i];
  197. Object newRock = new Object();
  198. newRock.x = oldRock.x;
  199. newRock.y = oldRock.y + 1;
  200. newRock.c = oldRock.c;
  201. newRock.color = oldRock.color;
  202.  
  203. if ((newRock.x == leftHand.x || newRock.x == body.x || newRock.x == rightHand.x) &&
  204. newRock.y == body.y)
  205. {
  206. livesCount--;
  207. collision = true;
  208.  
  209. if (livesCount <= 0)
  210. {
  211. //Console.Clear(); //If you want clear end-game screen
  212.  
  213. PrintStringOnPosition(15, 8, "GAME OVER!", ConsoleColor.Red);
  214. PrintStringOnPosition(13, 10, "Your score is " + score, ConsoleColor.White);
  215. PrintStringOnPosition(11, 11, "Press [enter] to exit.", ConsoleColor.White);
  216. Console.ReadLine();
  217. Environment.Exit(0);
  218. }
  219. }
  220.  
  221. if (newRock.y < Console.WindowHeight)
  222. {
  223. newRocks.Add(newRock);
  224. }
  225. }
  226.  
  227. rocks = newRocks;
  228. Console.Clear();
  229.  
  230. if (collision)
  231. {
  232. //rocks.Clear(); //clears the rocks if you get hit
  233.  
  234. PrintOnPosition(leftHand.x, leftHand.y, 'X', ConsoleColor.Red);
  235. PrintOnPosition(body.x, body.y, 'X', ConsoleColor.Red);
  236. PrintOnPosition(rightHand.x, rightHand.y, 'X', ConsoleColor.Red);
  237. }
  238. else
  239. {
  240. PrintOnPosition(leftHand.x, leftHand.y, leftHand.c, leftHand.color);
  241. PrintOnPosition(body.x, body.y, body.c, body.color);
  242. PrintOnPosition(rightHand.x, rightHand.y, rightHand.c, rightHand.color);
  243. }
  244.  
  245. foreach (Object stone in rocks)
  246. {
  247. PrintOnPosition(stone.x, stone.y, stone.c, stone.color);
  248. }
  249.  
  250. score++;
  251.  
  252. PrintStringOnPosition(33, 0, "Falling Rocks", ConsoleColor.White);
  253. PrintStringOnPosition(33, 6, "Lives: " + livesCount, ConsoleColor.White);
  254. PrintStringOnPosition(33, 7, "Score: " + score, ConsoleColor.White);
  255.  
  256. Thread.Sleep(150);
  257. }
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement