Advertisement
Kolimnared

FallingRocksGame

Oct 7th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. /* Implement the "Falling Rocks" game in the text console.
  6. * A small dwarf stays at the bottom of the screen and can move left and right (by the arrows keys).
  7. * A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
  8. * Rocks are the symbols ^, @, *, &, +, %, $, #, !, ., ;, - distributed with appropriate density.
  9. * The dwarf is (O). Ensure a constant game speed by Thread.Sleep(150).
  10. * Implement collision detection and scoring system. */
  11.  
  12. class Object
  13. {
  14. public Object()
  15. {
  16. }
  17.  
  18. public Object(int x, int y, string body, ConsoleColor color)
  19. {
  20. this.X = x;
  21. this.Y = y;
  22. this.Body = body;
  23. this.Color = color;
  24. }
  25.  
  26. public int X { get; set; }
  27. public int Y { get; set; }
  28. public string Body { get; set; }
  29. public ConsoleColor Color { get; set; }
  30. }
  31.  
  32. class Rock : Object
  33. {
  34. private static string[] rockTypes = { "^", "@", "*", "&", "+", "%", "$", "#", "!", ".", ";" };
  35. private static ConsoleColor[] rockColors = {
  36. ConsoleColor.Blue,
  37. ConsoleColor.Cyan,
  38. ConsoleColor.Gray,
  39. ConsoleColor.Green,
  40. ConsoleColor.Magenta,
  41. ConsoleColor.Red,
  42. ConsoleColor.White,
  43. ConsoleColor.Yellow
  44. };
  45. Random rand = new Random();
  46.  
  47. public Rock(int x, int y, string body, ConsoleColor color)
  48. {
  49. base.X = x;
  50. base.Y = y;
  51. base.Body = body;
  52. base.Color = color;
  53. }
  54.  
  55. public Rock()
  56. {
  57. for (int i = 0; i <= 2; i++)
  58. {
  59. base.X = rand.Next(2, FallingRocks.playGroundWigth - 2);
  60. }
  61. base.Y = 0;
  62. base.Body = rockTypes[rand.Next(0, rockTypes.Length)];
  63. base.Color = rockColors[rand.Next(0, rockColors.Length)];
  64. }
  65.  
  66. public bool Destroyed { get; set; }
  67.  
  68. public void Fall()
  69. {
  70. ++base.Y;
  71. }
  72.  
  73. public void destroy()
  74. {
  75. this.Destroyed = true;
  76. }
  77. }
  78.  
  79. class Dwarf : Object
  80. {
  81. public Dwarf()
  82. {
  83. this.Lives = 5;
  84. base.X = FallingRocks.playGroundWigth / 2 - 1;
  85. base.Y = FallingRocks.playGroundHeight;
  86. base.Body = "0^0";
  87. base.Color = ConsoleColor.Cyan;
  88. }
  89.  
  90. public int Lives { get; set; }
  91.  
  92. public void MoveLeft()
  93. {
  94. if (base.X > 2)
  95. {
  96. --base.X;
  97. }
  98. }
  99.  
  100. public void MoveRight()
  101. {
  102. if (base.X < FallingRocks.playGroundWigth - 3)
  103. {
  104. ++base.X;
  105. }
  106. }
  107.  
  108. }
  109.  
  110. class Rocket : Object
  111. {
  112. public Rocket()
  113. {
  114. base.X = 0;
  115. base.Y = 0;
  116. base.Body = "^";
  117. base.Color = ConsoleColor.Red;
  118. this.IsFired = false;
  119. }
  120.  
  121. public bool IsFired { get; set; }
  122.  
  123. public void moveUp()
  124. {
  125. if (base.Y > 1 && this.IsFired == true)
  126. {
  127. --base.Y;
  128. }
  129. else
  130. {
  131. this.IsFired = false;
  132. }
  133. }
  134. }
  135.  
  136. class FallingRocks
  137. {
  138. public static int consoleWidth = 90;
  139. public static int consoleHeight = 40;
  140. public static int playGroundWigth = 60;
  141. public static int playGroundHeight = 38;
  142. public static int startSpeed = 220;
  143.  
  144. static void startMenu()
  145. {
  146. Console.BufferWidth = Console.WindowWidth = 95;
  147. Console.BufferHeight = Console.WindowHeight = 40;
  148. Console.CursorVisible = false;
  149. Console.SetCursorPosition(0, 10);
  150. string[] logo =
  151. {
  152. " ........ ... ...... .. . ............. ... ... .. ........... .................. " ,
  153. " . .MMMMM...MM...OM. .OM, .MMMMM.NMM..M..MMMM~ .MMMMM..MMMMM..MMMM..M..MI.MMMMM " ,
  154. " . .MM.....OMNM..OM. .OM, . .M. .NMM..M.M.. .. MN..M.NM. .M.MM.....M.M...MM.... " ,
  155. " . .MMMMM..M..M. OM. .OM, . .M. .NM.M.M.M..MM~ .MMMM..MM. .M.MN ....MMM.. ..MMM. " ,
  156. " ...MM....MMMMMM OM....OM,......M...NM.NMM.MM..M~ .MM.MM.~M..MM.MM.. ..M.OM......M. " ,
  157. " ...MM.. .M....M.OMMMM.OMMMMM.MMMMM.MM..MM..MMMM~ .MM..M8.OMMM...~MMM..M..MM.MMMM.. " ,
  158. " ........ ... ...... .. . ............. ... ... .. ........... .................. " ,
  159. };
  160.  
  161. for (int i = 0; i < logo.Length; i++)
  162. {
  163. Console.WriteLine(logo[i]);
  164. }
  165. Console.WriteLine("\n\n");
  166. Console.WriteLine(" SHOOT: [UpArrow]");
  167. Console.WriteLine(" MOVE: [RightArrow][LeftArrow]\n\n");
  168. Console.WriteLine(" PRESS [ENTER] TO START");
  169. Console.WriteLine("\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\tIMPLEMENTED BY KOLIMNARED");
  170. Console.WriteLine("\t\t\t\t\t\t\t\t C# BASICS SEPTEMBER 2014");
  171. Console.ReadLine();
  172. Console.Clear();
  173. }
  174.  
  175. static void printObject(Object obj)
  176. {
  177. Console.SetCursorPosition(obj.X, obj.Y);
  178. Console.ForegroundColor = obj.Color;
  179. Console.WriteLine(obj.Body);
  180. }
  181.  
  182. static void clearPlayGround(int mode)
  183. {
  184. Console.ForegroundColor = ConsoleColor.Black;
  185. if (mode == 1)
  186. {
  187. Console.SetCursorPosition(2, 0);
  188. Console.WriteLine(new string(' ', playGroundWigth - 4));
  189. return;
  190. }
  191. Console.SetCursorPosition(playGroundWigth + 8, 17);
  192. Console.Write(new string(' ', 15));
  193. }
  194.  
  195. static void displayMenuAndFrame(int lives, int speed, int level, int score)
  196. {
  197. int middleOfMenu = playGroundWigth + (consoleWidth - playGroundWigth) / 2;
  198. ConsoleColor defaulfColor = ConsoleColor.Yellow;
  199. Console.ForegroundColor = defaulfColor;
  200. Console.SetCursorPosition(0, 0);
  201. for (int i = 0; i <= playGroundHeight; i++)
  202. {
  203. if (i == 6)
  204. {
  205. Console.WriteLine("||{0}|| __________________", new string(' ', playGroundWigth - 4));
  206. }
  207. else if (i > 6 && i < 23)
  208. {
  209. Console.WriteLine("||{0}|| | |", new string(' ', playGroundWigth - 4));
  210. }
  211. else if (i == 23)
  212. {
  213. Console.WriteLine("||{0}|| |__________________|", new string(' ', playGroundWigth - 4));
  214. }
  215. else
  216. {
  217. Console.WriteLine("||{0}||", new string(' ', playGroundWigth - 4));
  218. }
  219. }
  220. Console.Write(new string('=', playGroundWigth));
  221. printObject(new Object(middleOfMenu - ("_____SCORE_____".Length / 2), 8, "_____SCORE_____", defaulfColor));
  222. printObject(new Object(middleOfMenu - (score.ToString().Length / 2), 9, score.ToString(), defaulfColor));
  223.  
  224. printObject(new Object(middleOfMenu - ("_____LEVEL_____".Length / 2), 12, "_____LEVEL_____", defaulfColor));
  225. printObject(new Object(middleOfMenu - (level.ToString().Length / 2), 13, level.ToString(), defaulfColor));
  226.  
  227. printObject(new Object(middleOfMenu - ("_PLAYERS LIVES_".Length / 2), 16, "_PLAYERS LIVES_", defaulfColor));
  228. string livesString = new string('*', lives);
  229. printObject(new Object(middleOfMenu - (livesString.Length / 2), 17, livesString, defaulfColor));
  230.  
  231. printObject(new Object(middleOfMenu - ("_____SPEED_____".Length / 2), 20, "_____SPEED_____", defaulfColor));
  232. string speedString = new string('-', ((startSpeed - speed) / 15) + 1);
  233. printObject(new Object(middleOfMenu - (speedString.Length / 2), 21, speedString, defaulfColor));
  234. }
  235.  
  236. static void Main(string[] args)
  237. {
  238. startMenu();
  239. Console.BufferWidth = Console.WindowWidth = consoleWidth;
  240. Console.BufferHeight = Console.WindowHeight = consoleHeight;
  241. Console.CursorVisible = false;
  242. Dwarf Toshko = new Dwarf();
  243. Rocket littleBoy = new Rocket();
  244. List<Rock> rocks = new List<Rock>();
  245.  
  246. int level = 1;
  247. int score = 0;
  248. int speed = startSpeed;
  249.  
  250. while (Toshko.Lives > 0)
  251. {
  252. displayMenuAndFrame(Toshko.Lives, speed, level, score < playGroundHeight ? 0 : score - 40);
  253. printObject(Toshko);
  254. if (littleBoy.IsFired == true)
  255. {
  256. printObject(littleBoy);
  257. }
  258.  
  259. while (Console.KeyAvailable)
  260. {
  261. ConsoleKeyInfo pressedKey = Console.ReadKey();
  262. if (pressedKey.Key == ConsoleKey.RightArrow)
  263. {
  264. Toshko.MoveRight();
  265. }
  266. else if (pressedKey.Key == ConsoleKey.LeftArrow)
  267. {
  268. Toshko.MoveLeft();
  269. }
  270. else if (pressedKey.Key == ConsoleKey.UpArrow)
  271. {
  272. if (littleBoy.IsFired == false)
  273. {
  274. littleBoy.IsFired = true;
  275. littleBoy.X = Toshko.X + 1;
  276. littleBoy.Y = Toshko.Y - 1;
  277. printObject(littleBoy);
  278. }
  279. }
  280. }
  281.  
  282. Random rand = new Random();
  283. int rocksPerRow = rand.Next(1, 5 + level);
  284.  
  285. for (int i = 0; i < rocksPerRow; i++)
  286. {
  287. Rock rock = new Rock();
  288. rocks.Add(rock);
  289. }
  290.  
  291. bool collision = false;
  292. Rock hitedRock = new Rock(0, 0, "*", ConsoleColor.Red);
  293. for (int i = 0; i < rocks.Count; i++)
  294. {
  295. if (rocks[i].Y <= playGroundHeight)
  296. {
  297. printObject(rocks[i]);
  298. }
  299. else
  300. {
  301. rocks[i].destroy();
  302. }
  303. if ((rocks[i].Y == Toshko.Y) && (rocks[i].X >= Toshko.X && rocks[i].X <= Toshko.X + 2))
  304. {
  305. collision = true;
  306. }
  307. if (littleBoy.IsFired == true && littleBoy.X == rocks[i].X &&
  308. (littleBoy.Y == rocks[i].Y || littleBoy.Y == rocks[i].Y + 1))
  309. {
  310. littleBoy.IsFired = false;
  311. hitedRock.X = rocks[i].X;
  312. hitedRock.Y = rocks[i].Y;
  313. printObject(hitedRock);
  314. }
  315.  
  316. if (rocks[i].Destroyed == true || (rocks[i].X == hitedRock.X && rocks[i].Y == hitedRock.Y))
  317. {
  318. rocks.RemoveAt(i--);
  319. }
  320. else
  321. {
  322. rocks[i].Fall();
  323. }
  324. }
  325.  
  326. if (collision)
  327. {
  328. clearPlayGround(2);
  329. printObject(new Object(Toshko.X, Toshko.Y, "XXX", ConsoleColor.Red));
  330. Toshko.Lives--;
  331. Console.Beep();
  332. }
  333.  
  334. if (littleBoy.IsFired)
  335. {
  336. littleBoy.moveUp();
  337. }
  338.  
  339. if (((score - playGroundHeight) % 50 == 0) && (score > playGroundHeight) && (speed > 30))
  340. {
  341. speed -= 5;
  342. }
  343. if ((score - playGroundHeight) == Math.Pow(2, level) * 30)
  344. {
  345. level++;
  346. }
  347.  
  348. score++;
  349. Thread.Sleep(speed);
  350. clearPlayGround(1);
  351. }
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement