Advertisement
Kamigawa

4.12.FallingRocks

Oct 6th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 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.Threading.Tasks;
  7.  
  8. class FallingRocks
  9. {
  10.  
  11. private static bool IsGameOver(int rockX, int rockY, int dwarfX, int dwarfY)
  12. {
  13. bool isOver = false;
  14. if (rockY == dwarfY - 1 && (rockX == dwarfX || rockX == dwarfX + 1 || rockX == dwarfX + 2))
  15. {
  16. Console.WriteLine("GAME OVER");
  17. isOver = true;
  18. }
  19. return isOver;
  20. }
  21.  
  22. private static void PrintFigures(int dwarfX, int dwarfY, int rockX, int rockY, int rockStyle,
  23. char[] rocks, string dwarf, long score)
  24. {
  25. Console.SetCursorPosition(dwarfX, dwarfY);
  26. Console.WriteLine(dwarf);
  27. Console.SetCursorPosition(rockX, rockY);
  28. Console.WriteLine(rocks[rockStyle]);
  29. Console.SetCursorPosition(0, 0);
  30. Console.WriteLine("Your score is: " + score);
  31. }
  32.  
  33. static void Main(string[] args)
  34. {
  35. const int PLAYGROUNG_HEIGHT = 20;
  36. const int PLAYGROUND_WIDTH = 40;
  37. string dwarf = "(O)";
  38. int dwarfLen = dwarf.Length;
  39. char[] rocks = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
  40. int dwarfX = PLAYGROUND_WIDTH / 2;
  41. int dwarfY = PLAYGROUNG_HEIGHT - 1;
  42. int len = rocks.Length;
  43. long score = 0;
  44.  
  45. Random rng = new Random();
  46. Console.CursorVisible = false;
  47. Console.WindowHeight = PLAYGROUNG_HEIGHT;
  48. Console.WindowWidth = PLAYGROUND_WIDTH;
  49. Console.BufferHeight = Console.WindowHeight;
  50.  
  51. //Start possition for the dwarf.
  52. Console.SetCursorPosition(dwarfX, dwarfY);
  53. Console.WriteLine(dwarf);
  54.  
  55. while (true)
  56. {
  57. //Falling stuff.
  58. int rockX = rng.Next(0, (PLAYGROUND_WIDTH));
  59. int rockY = 0;
  60. int rockStyle = rng.Next(0, len);
  61. score++;
  62. for (int y = 0; y < dwarfY - 1; y++)
  63. {
  64. Thread.Sleep(150);//Set the speed.
  65. rockY++;
  66. Console.Clear();
  67. PrintFigures(dwarfX, dwarfY, rockX, rockY, rockStyle, rocks, dwarf, score);
  68. if (IsGameOver(rockX, rockY, dwarfX, dwarfY))
  69. {
  70. return;
  71. }
  72. //Movement of the dwarf.
  73. if (Console.KeyAvailable)
  74. {
  75. Console.Clear();
  76. ConsoleKeyInfo key = Console.ReadKey();
  77. if (key.Key == ConsoleKey.LeftArrow)
  78. {
  79. dwarfX -= 2;
  80. if (dwarfX < 0)
  81. {
  82. dwarfX = 0;
  83. }
  84. PrintFigures(dwarfX, dwarfY, rockX, rockY, rockStyle, rocks, dwarf, score);
  85. if (IsGameOver(rockX, rockY, dwarfX, dwarfY))
  86. {
  87. return;
  88. }
  89. }
  90. else if (key.Key == ConsoleKey.RightArrow)
  91. {
  92. dwarfX += 2;
  93. if (dwarfX > PLAYGROUND_WIDTH - dwarfLen)
  94. {
  95. dwarfX = PLAYGROUND_WIDTH - dwarfLen;
  96. }
  97. PrintFigures(dwarfX, dwarfY, rockX, rockY, rockStyle, rocks, dwarf, score);
  98. if (IsGameOver(rockX, rockY, dwarfX, dwarfY))
  99. {
  100. return;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement