Advertisement
Guest User

FallingRocks

a guest
Apr 3rd, 2014
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 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.  
  7. namespace FallingRocks
  8. {
  9. class FallingRocks
  10. {
  11. class Item
  12. {
  13. public int X, Y;
  14. public string Type, Empty;
  15. public ConsoleColor Color;
  16. public Item(int x, int y, ConsoleColor color, string type)
  17. {
  18. this.X = x;
  19. this.Y = y;
  20. this.Color = color;
  21. this.Type = type;
  22. for (int i = 0; i < this.Type.Length; i++)
  23. {
  24. this.Empty += " ";
  25. }
  26. }
  27. public void Display()
  28. {
  29. Console.SetCursorPosition(this.X, this.Y);
  30. Console.ForegroundColor = this.Color;
  31. Console.Write(this.Type);
  32. }
  33. public void Clear()
  34. {
  35. Console.SetCursorPosition(this.X, this.Y);
  36. Console.Write(this.Empty);
  37. }
  38. }
  39. class Dwarf : Item
  40. {
  41. public Dwarf()
  42. : base(Console.WindowWidth / 2,
  43. Console.WindowHeight - 1,
  44. ConsoleColor.DarkYellow,
  45. "(0)")
  46. {}
  47. public void Move(ConsoleKeyInfo pressedKey)
  48. {
  49. switch (pressedKey.Key)
  50. {
  51. case ConsoleKey.LeftArrow:
  52. if (this.X > 0)
  53. {
  54. this.Clear();
  55. this.X--;
  56. this.Display();
  57. }
  58. break;
  59. case ConsoleKey.RightArrow:
  60. if (this.X < Console.WindowWidth - 2 - 2)
  61. {
  62. this.Clear();
  63. this.X++;
  64. this.Display();
  65. }
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. }
  72. static void Main()
  73. {
  74. Random rnd = new Random();
  75. string[] rockTypes = { "!", "@", "#", "$", "%", "^", "&", "*", ".", ";" };
  76. List<Item> rocks = new List<Item>();
  77. long score = 0;
  78. double SleepTime = 200;
  79. Console.CursorVisible = false;
  80. Console.BufferHeight = Console.WindowHeight;
  81. Console.WriteLine("Falling rocks game!");
  82. Console.WriteLine("Use the arrow keys to move the dwarf (0) at the bottom.");
  83. Console.WriteLine("Try to avoid being hit!");
  84. Console.WriteLine("Press one of the arrows to start...");
  85. ConsoleKeyInfo pressedKey = Console.ReadKey();
  86. if (pressedKey.Key == ConsoleKey.LeftArrow ||
  87. pressedKey.Key == ConsoleKey.RightArrow)
  88. {
  89. Dwarf dwarf = new Dwarf();
  90. dwarf.Display();
  91. while (true)
  92. {
  93. for (int i = rnd.Next(0,3); i < 4; i++) //randomly generate 1-4 new rocks
  94. {
  95. Item rock = new Item(rnd.Next(0, Console.WindowWidth - 1),
  96. 0,
  97. (ConsoleColor)rnd.Next((int)ConsoleColor.Blue,(int)ConsoleColor.White),
  98. rockTypes[rnd.Next(rockTypes.Length)]);
  99. rock.Display(); //displaying the rocks
  100. rocks.Add(rock); //adding the rocks to a list
  101. }
  102. foreach (Item i in rocks) //generating falling of rocks
  103. {
  104. i.Clear();
  105. i.Y++;
  106. if (i.Y < (Console.WindowHeight)) i.Display(); //showing the fallen rock only in the field
  107. else score++; //adding score for each fallen rock
  108. if ((i.X >= dwarf.X) &&
  109. (i.X <= dwarf.X + 2) &&
  110. (i.Y >= dwarf.Y) &&
  111. (i.Y <= dwarf.Y + 2))
  112. {
  113. Console.Clear();
  114. Console.SetCursorPosition(0, 0);
  115. Console.ForegroundColor = ConsoleColor.White;
  116. Console.WriteLine("Game over!!!");
  117. Console.WriteLine("Your score is {0} points!", score);
  118. return;
  119. }
  120. }
  121. rocks.RemoveAll(i => i.Y >= (Console.WindowHeight));
  122. if (Console.KeyAvailable)
  123. {
  124. dwarf.Move(Console.ReadKey());
  125. }
  126. Thread.Sleep((int)SleepTime);
  127. SleepTime -= 0.05;
  128. }
  129. }
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement