Advertisement
Guest User

Falling Rocks

a guest
Oct 20th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. class Program
  7. {
  8. struct Player
  9. {
  10.  
  11. public int x;
  12. public int y;
  13. public string c;
  14. public ConsoleColor color;
  15. }
  16. struct Text
  17. {
  18.  
  19. public int x;
  20. public int y;
  21. public string c;
  22. public ConsoleColor color;
  23. }
  24. struct Rocks
  25. {
  26.  
  27. public int x;
  28. public int y;
  29. public string c;
  30. public ConsoleColor color;
  31.  
  32.  
  33. }
  34.  
  35. static void PrintOnPosition(int x, int y, string c, ConsoleColor color = ConsoleColor.Gray)
  36. {
  37. Console.SetCursorPosition(x, y);
  38. Console.ForegroundColor = color;
  39. Console.WriteLine(c);
  40. }
  41. static void TextPrinting(int x, int y, string c, ConsoleColor color = ConsoleColor.Gray)
  42. {
  43. Console.SetCursorPosition(x, y);
  44. Console.ForegroundColor = color;
  45. Console.WriteLine(c);
  46. }
  47.  
  48. static void Main()
  49. {
  50.  
  51. int playField = 20;
  52. int score = 0;
  53. int bonusScore = 0;
  54. Console.BufferHeight = Console.WindowHeight = 30;
  55. Console.BufferWidth = Console.WindowWidth = 30;
  56. Player PlayerInfo = new Player();
  57. PlayerInfo.x = 10;
  58. PlayerInfo.y = 20;
  59. PlayerInfo.c = "_0_";
  60. Random randomGenerator = new Random();
  61. switch (randomGenerator.Next(0, 5))
  62. {
  63. case 0: PlayerInfo.color = ConsoleColor.Gray;
  64. break;
  65. case 1: PlayerInfo.color = ConsoleColor.Green;
  66. break;
  67. case 2: PlayerInfo.color = ConsoleColor.Yellow;
  68. break;
  69. case 3: PlayerInfo.color = ConsoleColor.Blue;
  70. break;
  71. case 4: PlayerInfo.color = ConsoleColor.Red;
  72. break;
  73. case 5: PlayerInfo.color = ConsoleColor.White;
  74. break;
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. List<Rocks> rock = new List<Rocks>();
  82. // kakuvto kamuk hvane6 takuv cvqt da ti stava 4ove4eto i samo ot tezi cvetove kato subira6 da ti dava to4ki.
  83.  
  84.  
  85.  
  86. while (true)
  87. {
  88.  
  89. Rocks newRock = new Rocks();
  90. newRock.x = randomGenerator.Next(0, playField);
  91. newRock.y = 0;
  92. switch (randomGenerator.Next(0, 5))
  93. {
  94. case 0: newRock.color = ConsoleColor.Gray;
  95. break;
  96. case 1: newRock.color = ConsoleColor.Green;
  97. break;
  98. case 2: newRock.color = ConsoleColor.Yellow;
  99. break;
  100. case 3: newRock.color = ConsoleColor.Blue;
  101. break;
  102. case 4: newRock.color = ConsoleColor.Red;
  103. break;
  104. case 5: newRock.color = ConsoleColor.White;
  105. break;
  106.  
  107. }
  108. switch (randomGenerator.Next(0, 11))
  109. {
  110. case 0: newRock.c = "^";
  111. break;
  112. case 1: newRock.c = "@";
  113. break;
  114. case 2: newRock.c = "*";
  115. break;
  116. case 3: newRock.c = "&";
  117. break;
  118. case 4: newRock.c = "+";
  119. break;
  120. case 5: newRock.c = "%";
  121. break;
  122. case 6: newRock.c = "$";
  123. break;
  124. case 7: newRock.c = "#";
  125. break;
  126. case 8: newRock.c = "!";
  127. break;
  128. case 9: newRock.c = ".";
  129. break;
  130. case 10: newRock.c = ";";
  131. break;
  132. }
  133. rock.Add(newRock);
  134.  
  135.  
  136. if (Console.KeyAvailable)
  137. {
  138. ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  139. while (Console.KeyAvailable)
  140. {
  141. Console.ReadKey();
  142. }
  143. if (pressedKey.Key == ConsoleKey.LeftArrow)
  144. {
  145. if (PlayerInfo.x - 1 >= 0)
  146. {
  147. PlayerInfo.x--;
  148. }
  149. }
  150. else if (pressedKey.Key == ConsoleKey.RightArrow)
  151. {
  152. if (PlayerInfo.x + 1 < playField - 1)
  153. {
  154. PlayerInfo.x++;
  155. }
  156.  
  157. }
  158.  
  159. }
  160.  
  161. //Moving Rocks
  162. List<Rocks> newList = new List<Rocks>();
  163. for (int i = 0; i < rock.Count; i++)
  164. {
  165.  
  166. Rocks oldRocks = rock[i];
  167. Rocks newRocks = new Rocks();
  168. newRocks.x = oldRocks.x;
  169. newRocks.y = oldRocks.y + 1;
  170. newRocks.c = oldRocks.c;
  171. newRocks.color = oldRocks.color;
  172. if (newRocks.y == PlayerInfo.y + 1)
  173. {
  174. score++;
  175. }
  176. if (newRocks.y == PlayerInfo.y && newRocks.color == PlayerInfo.color && newRocks.x == PlayerInfo.x + 1)
  177. {
  178. Console.WriteLine();
  179. PrintOnPosition(1, 2, "Bonus Won!!", ConsoleColor.Red);
  180. bonusScore++;
  181. }
  182. else if (newRocks.y == PlayerInfo.y && newRocks.x == PlayerInfo.x + 1)
  183. {
  184. PrintOnPosition(1, 2, "Game Over!!", ConsoleColor.Red);
  185. Console.WriteLine();
  186. return;
  187. }
  188.  
  189. if (newRocks.y <= PlayerInfo.y + 1)
  190. {
  191. newList.Add(newRocks);
  192. }
  193.  
  194.  
  195. }
  196. rock = newList;
  197. Console.Clear();
  198. //Redraw Playfield
  199. PrintOnPosition(PlayerInfo.x, PlayerInfo.y, PlayerInfo.c, PlayerInfo.color);
  200. foreach (var rocks in rock)
  201. {
  202. PrintOnPosition(rocks.x, rocks.y, rocks.c, rocks.color);
  203. }
  204.  
  205. //Spawn text
  206. Text TextInfo = new Text();
  207. TextInfo.x = 1;
  208. TextInfo.y = 23;
  209. TextInfo.c = "Your score:"+score+" Bonus Score:"+bonusScore;
  210. TextInfo.color = ConsoleColor.Blue;
  211. TextPrinting(TextInfo.x, TextInfo.y, TextInfo.c, TextInfo.color);
  212. //Slow down moving rocks
  213. Thread.Sleep(200);
  214. }
  215.  
  216. }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement