Falling Rocks


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:03 p.m.

FORMAT: Text only

SIZE: 7.9 kB

HITS: 794

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace _11.FallingRocks
  7. {
  8. struct Position
  9. {
  10. public int row;
  11. public int col;
  12. public Position(int row, int col)
  13. {
  14. this.row = row;
  15. this.col = col;
  16. }
  17. }
  18. struct Rock
  19. {
  20. public string symbol;
  21. public int colour;
  22. public int length;
  23. public Position position;
  24. public Rock(string symbol, int colour, int length, Position position)
  25. {
  26. this.symbol = symbol;
  27. this.colour = colour;
  28. this.length = length;
  29. this.position = position;
  30. }
  31. }
  32. class FallingRocks
  33. {
  34. static void Main(string[] args)
  35. {
  36. byte right = 0;
  37. byte left = 1;
  38. byte stop = 2;
  39. Position[] directions = new Position[]
  40. {
  41. new Position(0, 1), // right
  42. new Position(0, -1), // left
  43. new Position(0, 0), // stop
  44. };
  45. Rock[] rocks = new Rock[Console.BufferHeight];
  46. bool isAlive = true;
  47. bool collision = false;
  48. int points = 0;
  49. int randomSymbol,randomColour,randomLength;
  50. int index = 0;
  51. string rockSymbol = "";
  52. double sleepTime = 150;
  53. int direction = stop;
  54. Random randomNumbersGenerator = new Random();
  55. Console.CursorVisible = false;
  56. Console.BufferHeight = Console.WindowHeight;
  57. Console.BufferWidth = Console.WindowWidth;
  58. Position Dwarf = new Position(Console.BufferHeight-2, Console.BufferWidth/2);
  59. do
  60. {
  61. if (Console.KeyAvailable)
  62. {
  63. ConsoleKeyInfo userInput = Console.ReadKey();
  64. if (userInput.Key == ConsoleKey.LeftArrow)
  65. {
  66. direction = left;
  67. }
  68. if (userInput.Key == ConsoleKey.RightArrow)
  69. {
  70. direction = right;
  71. }
  72. }
  73. Position nextDirection = directions[direction];
  74. Position newDwarf = new Position(Dwarf.row, Dwarf.col + nextDirection.col);
  75. if (newDwarf.col < 0 || newDwarf.col > Console.BufferWidth - 3)
  76. {
  77. Console.SetCursorPosition(Dwarf.col, Dwarf.row);
  78. Console.Write("(O)");
  79. }
  80. else
  81. {
  82. Console.SetCursorPosition(newDwarf.col, newDwarf.row);
  83. Console.Write("(O)");
  84. Dwarf = newDwarf;
  85. }
  86. randomSymbol = randomNumbersGenerator.Next(0, 11);
  87. switch (randomSymbol)
  88. {
  89. case 0: rockSymbol = "^"; break;
  90. case 1: rockSymbol = "@"; break;
  91. case 2: rockSymbol = "*"; break;
  92. case 3: rockSymbol = "&"; break;
  93. case 4: rockSymbol = "+"; break;
  94. case 5: rockSymbol = "%"; break;
  95. case 6: rockSymbol = "$"; break;
  96. case 7: rockSymbol = "#"; break;
  97. case 8: rockSymbol = "!"; break;
  98. case 9: rockSymbol = "."; break;
  99. case 10: rockSymbol = ";"; break;
  100. case 11: rockSymbol = "-"; break;
  101. default: Console.Write("Something's wrong with the symbols!"); break;
  102. }
  103. randomColour = randomNumbersGenerator.Next(0, 14);
  104. randomLength = randomNumbersGenerator.Next(1, 5);
  105. Position rockPosition = new Position(0, randomNumbersGenerator.Next(0, Console.BufferWidth - 1));
  106. rocks[index] = new Rock(rockSymbol, randomColour, randomLength, rockPosition);
  107. for (int i = 0; i < index; i++)
  108. {
  109. Console.SetCursorPosition(rocks[i].position.col, rocks[i].position.row);
  110. switch (rocks[i].colour)
  111. {
  112. case 0: Console.ForegroundColor = ConsoleColor.Blue; break;
  113. case 1: Console.ForegroundColor = ConsoleColor.Cyan; break;
  114. case 2: Console.ForegroundColor = ConsoleColor.DarkBlue; break;
  115. case 3: Console.ForegroundColor = ConsoleColor.DarkCyan; break;
  116. case 4: Console.ForegroundColor = ConsoleColor.DarkGray; break;
  117. case 5: Console.ForegroundColor = ConsoleColor.DarkGreen; break;
  118. case 6: Console.ForegroundColor = ConsoleColor.DarkMagenta; break;
  119. case 7: Console.ForegroundColor = ConsoleColor.DarkRed; break;
  120. case 8: Console.ForegroundColor = ConsoleColor.DarkYellow; break;
  121. case 9: Console.ForegroundColor = ConsoleColor.Gray; break;
  122. case 10: Console.ForegroundColor = ConsoleColor.Green; break;
  123. case 11: Console.ForegroundColor = ConsoleColor.Magenta; break;
  124. case 12: Console.ForegroundColor = ConsoleColor.Red; break;
  125. case 13: Console.ForegroundColor = ConsoleColor.White; break;
  126. case 14: Console.ForegroundColor = ConsoleColor.Yellow; break;
  127. default: Console.Write("Something's wrong with the colour!"); break;
  128. }
  129. for (int j = 0; j < rocks[i].length; j++)
  130. {
  131. Console.Write(rocks[i].symbol);
  132. }
  133. rocks[i].position.row++;
  134. if (rocks[i].position.row - 1 == Dwarf.row)
  135. {
  136. collision = ((Dwarf.col + 2 >= rocks[i].position.col) && (Dwarf.col + 2 <= rocks[i].position.col + rocks[i].length - 1)) || ((Dwarf.col >= rocks[i].position.col) && (Dwarf.col <= rocks[i].position.col + rocks[i].length - 1));
  137. if (collision)
  138. {
  139. isAlive = false;
  140. break;
  141. }
  142. for (int j = i; j < index ; j++)
  143. {
  144. rocks[j] = rocks[j + 1];
  145. }
  146. points++;
  147. index--;
  148. }
  149. }
  150. Console.ForegroundColor = ConsoleColor.White;
  151. Thread.Sleep((int)sleepTime);
  152. index++;
  153. Console.Clear();
  154. } while (isAlive);
  155. Console.SetCursorPosition(Console.BufferWidth / 2 - 10, 0);
  156. Console.ForegroundColor = ConsoleColor.Green;
  157. Console.WriteLine("You made {0} points.\n\n", points);
  158. Console.CursorLeft = Console.BufferWidth / 2 - 5;
  159. Console.ForegroundColor = ConsoleColor.Red;
  160. Console.WriteLine("Game Over!");
  161. Console.ForegroundColor = ConsoleColor.White;
  162. }
  163. }
  164. }

comments powered by Disqus