Advertisement
petromaxa

Untitled

Dec 2nd, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7. using System.Data;
  8.  
  9. namespace _11.FallingRocks
  10. {
  11.  
  12. class Program
  13. {
  14. static void WriteOnPosition(int x, int y, char symbol, ConsoleColor color = ConsoleColor.White)
  15. {
  16. Console.SetCursorPosition(x, y);
  17. Console.ForegroundColor = color;
  18. Console.Write(symbol);
  19. }
  20.  
  21. static void WriteStringOnPosition(int x, int y, string Message, ConsoleColor color = ConsoleColor.White)
  22. {
  23. Console.SetCursorPosition(x, y);
  24. Console.ForegroundColor = color;
  25. Console.Write(Message);
  26. }
  27.  
  28. struct Rock
  29. {
  30. public int x;
  31. public int y;
  32. public ConsoleColor color;
  33. public char symbol;
  34. }
  35.  
  36. struct Dwarf
  37. {
  38. public int x1;
  39. public int x2;
  40. public int x3;
  41. public int y;
  42. public ConsoleColor color;
  43. public char firstSymbol;
  44. public char secondSymbol;
  45. public char thirdSymbol;
  46. }
  47.  
  48. static void Main(string[] args)
  49. {
  50. char[] rockSymbols = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
  51. String[] lightColorNames = { "Cyan", "Gray", "Green", "Magenta", "Red", "White", "Yellow" };
  52. char randomRockSymbol;
  53. string userName;
  54. int score = 0;
  55. Console.BufferHeight = Console.WindowHeight = 30;
  56. Console.BufferWidth = Console.WindowWidth = 60;
  57. int playfildWidth = 35;
  58. Dwarf dwarf = new Dwarf();
  59. int lives = 3;
  60. int rockWidth;
  61. bool lifeTaken = false;
  62. dwarf.x1 = 23;
  63. dwarf.x2 = 24;
  64. dwarf.x3 = 25;
  65. dwarf.y = Console.WindowHeight - 1;
  66. dwarf.firstSymbol = '(';
  67. dwarf.secondSymbol = '0';
  68. dwarf.thirdSymbol = ')';
  69. dwarf.color = ConsoleColor.White;
  70. string[] Names = new string[10];
  71. int[] gameScores = new int[10];
  72.  
  73. Random randomGenerator = new Random();
  74. List<Rock> Rocks = new List<Rock>();
  75. do
  76. {
  77. Console.WriteLine("Please enter your name(maximum 10symbols):");
  78. userName = Console.ReadLine();
  79. }
  80. while (userName.Length > 10);
  81.  
  82. while (true)
  83. {
  84. Rock newRock = new Rock();
  85. ConsoleColor consoleColor = new ConsoleColor();
  86.  
  87. string randomColorName = lightColorNames[randomGenerator.Next(lightColorNames.Length)];
  88. consoleColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), randomColorName);
  89.  
  90. rockWidth = randomGenerator.Next(1, 5);
  91. randomRockSymbol = rockSymbols[randomGenerator.Next(rockSymbols.Length)];
  92. newRock.color = consoleColor;
  93. newRock.x = randomGenerator.Next(0, playfildWidth - 1);
  94. newRock.y = 0;
  95. newRock.symbol = randomRockSymbol;
  96.  
  97. for (int i = 0; i < rockWidth; i++)
  98. {
  99. Rocks.Add(newRock);
  100. if (newRock.x < playfildWidth - 1)
  101. {
  102. newRock.x++;
  103. }
  104. }
  105.  
  106. if (Console.KeyAvailable)
  107. {
  108. ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  109. while (Console.KeyAvailable)
  110. {
  111. Console.ReadKey(true);
  112. }
  113. if (pressedKey.Key == ConsoleKey.LeftArrow)
  114. {
  115. if (dwarf.x1 - 1 >= 0)
  116. {
  117. dwarf.x1--;
  118. dwarf.x2--;
  119. dwarf.x3--;
  120. }
  121. }
  122. else if (pressedKey.Key == ConsoleKey.RightArrow)
  123. {
  124. if (dwarf.x3 + 1 <= playfildWidth - 1)
  125. {
  126. dwarf.x1++;
  127. dwarf.x2++;
  128. dwarf.x3++;
  129. }
  130. }
  131. }
  132.  
  133. List<Rock> newList = new List<Rock>();
  134. for (int i = 0; i < Rocks.Count; i++)
  135. {
  136. Rock oldRock = Rocks[i];
  137. Rock rock = new Rock();
  138. rock.x = oldRock.x;
  139. rock.y = oldRock.y + 1;
  140. rock.symbol = oldRock.symbol;
  141. rock.color = oldRock.color;
  142. if ((rock.x == dwarf.x1 || rock.x == dwarf.x2 || rock.x == dwarf.x3) && rock.y == dwarf.y)
  143. {
  144. if (lifeTaken == false)
  145. {
  146. lives--;
  147. lifeTaken = true;
  148. }
  149. rock.symbol = 'x';
  150. rock.color = ConsoleColor.Red;
  151.  
  152. }
  153. if (rock.y < Console.WindowHeight)
  154. {
  155. newList.Add(rock);
  156. }
  157. }
  158.  
  159. Rocks = newList;
  160. Console.Clear();
  161.  
  162. for (int i = 0; i < Console.WindowHeight; i++)
  163. {
  164. WriteOnPosition(playfildWidth, i, '|', ConsoleColor.Yellow);
  165. }
  166. WriteOnPosition(dwarf.x1, dwarf.y, dwarf.firstSymbol, dwarf.color);
  167. WriteOnPosition(dwarf.x2, dwarf.y, dwarf.secondSymbol, dwarf.color);
  168. WriteOnPosition(dwarf.x3, dwarf.y, dwarf.thirdSymbol, dwarf.color);
  169.  
  170. foreach (Rock rock in Rocks)
  171. {
  172. WriteOnPosition(rock.x, rock.y, rock.symbol, rock.color);
  173. }
  174.  
  175. if (lifeTaken)
  176. {
  177. Rocks.Clear();
  178. Thread.Sleep(300);
  179. }
  180. WriteStringOnPosition(playfildWidth + 10, Console.WindowHeight - 29, "Lives:");
  181. WriteStringOnPosition(playfildWidth + 17, Console.WindowHeight - 29, Convert.ToString(lives));
  182. WriteStringOnPosition(playfildWidth + 2, Console.WindowHeight - 26, userName + ":");
  183. WriteStringOnPosition(playfildWidth + userName.Length + 3, Console.WindowHeight - 26, Convert.ToString(score));
  184.  
  185.  
  186. if (lives < 1)
  187. {
  188. if (!File.Exists("scoring.txt"))
  189. {
  190. var scoringFile = File.Create("scoring.txt");
  191. scoringFile.Close();
  192. TextWriter writeInScoringFile = new StreamWriter("scoring.txt", true);
  193.  
  194. writeInScoringFile.Write("{0}:", userName);
  195. writeInScoringFile.Write(Convert.ToString(score, 10).PadLeft(6, '0'));
  196. writeInScoringFile.Close();
  197. }
  198. else
  199. {
  200. TextWriter writeInScoringFile = new StreamWriter("scoring.txt", true);
  201. writeInScoringFile.Write("\n{0}:", userName);
  202. writeInScoringFile.Write(Convert.ToString(score, 10).PadLeft(6, '0'));
  203. writeInScoringFile.Close();
  204. }
  205.  
  206. DataTable NamesScoresTable = new DataTable();
  207. NamesScoresTable.Columns.Add("Names");
  208. NamesScoresTable.Columns.Add("Scores");
  209.  
  210. string[] scores = System.IO.File.ReadAllLines("scoring.txt");
  211. int i;
  212. for (i = 0; i < scores.Length; i++)
  213. {
  214. string[] nameAndScore = scores[i].Split(':');
  215. DataRow row = NamesScoresTable.NewRow();
  216. row["Names"] = nameAndScore[0];
  217. row["Scores"] = nameAndScore[1];
  218. NamesScoresTable.Rows.Add(row);
  219. }
  220. DataView dataView = NamesScoresTable.DefaultView;
  221. dataView.Sort = "Scores desc";
  222. DataTable sortedTable = dataView.ToTable();
  223. int printPosition = 0;
  224. DataRow[] rows = sortedTable.Select(string.Empty);
  225. WriteStringOnPosition(playfildWidth + 2, Console.WindowHeight - 20, "Game over!", ConsoleColor.Red);
  226. WriteStringOnPosition(playfildWidth + 2, Console.WindowHeight - 13, "Score statistics:", ConsoleColor.Green);
  227. foreach (DataRow row in rows)
  228. {
  229. string scoreInfo;
  230. if (printPosition > 8)
  231. {
  232. scoreInfo = String.Format("{0}.{1}{2}", printPosition + 1, row["Names"].ToString().PadRight(9, ' '), Convert.ToString(int.Parse(row["Scores"].ToString())).PadLeft(9, ' '));
  233. WriteStringOnPosition(playfildWidth + 2, Console.WindowHeight - 12 + printPosition, scoreInfo);
  234. }
  235. else
  236. {
  237. scoreInfo = String.Format("{0}.{1}{2}", printPosition + 1, row["Names"].ToString().PadRight(10, ' '), Convert.ToString(int.Parse(row["Scores"].ToString())).PadLeft(9, ' '));
  238. WriteStringOnPosition(playfildWidth + 2, Console.WindowHeight - 12 + printPosition, scoreInfo);
  239. }
  240. printPosition++;
  241. if (printPosition > 9)
  242. {
  243. break;
  244. }
  245. }
  246. break;
  247. }
  248.  
  249. lifeTaken = false;
  250. score++;
  251. Thread.Sleep(150);
  252. }
  253. WriteStringOnPosition(playfildWidth + 2, Console.WindowHeight - 19, "Press any key to exit.");
  254. Console.ReadKey();
  255. }
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement