coding_giants

l16. FINAL BRICK GAME

Nov 15th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace BrickGame
  8. {
  9. class Program
  10. {
  11. static string[] board;
  12. const string PLAYER = "^";
  13. const string OBSTACLE = "#";
  14. const string NITRO = "N";
  15. const string POINTS = "P";
  16. static int points = 0;
  17. static int highestScore = 0;
  18. static void Main(string[] args)
  19. {
  20. string reset;
  21. do
  22. {
  23. int playerPosition = 1;
  24. Random generatorRandom = new Random();
  25. bool isStruck = false;
  26. int speed = 0;
  27. int nitroTime = -1;
  28. bool isNitroFuel = false;
  29. NewBoard(10);
  30. SetPlayer(playerPosition);
  31. ShowBoard();
  32. //Loop
  33. while (!isStruck)
  34. {
  35. //controls
  36. if (Console.KeyAvailable)
  37. {
  38. ConsoleKeyInfo keyPressed = Console.ReadKey(true);
  39. if (keyPressed.Key == ConsoleKey.RightArrow)
  40. {
  41. if (playerPosition < 2)
  42. {
  43. playerPosition++;
  44. }
  45. }
  46. if (keyPressed.Key == ConsoleKey.LeftArrow)
  47. {
  48. if (playerPosition > 0)
  49. {
  50. playerPosition--;
  51. }
  52. }
  53. if (keyPressed.Key == ConsoleKey.Spacebar && isNitroFuel)
  54. {
  55. if (nitroTime == -1)
  56. {
  57. nitroTime = 10;
  58. isNitroFuel = false;
  59. }
  60. }
  61. while (Console.KeyAvailable)
  62. {
  63. Console.ReadKey(false);
  64. }
  65. }
  66.  
  67. if (nitroTime == 10)
  68. {
  69. speed += 200;
  70. nitroTime--;
  71. }
  72. else if (nitroTime > 0)
  73. {
  74. nitroTime--;
  75. }
  76. else if (nitroTime == 0)
  77. {
  78. speed -= 200;
  79. nitroTime = -1;
  80. }
  81.  
  82. //Collision check
  83. int nearestObstaclePosition = board[board.Length - 2].IndexOf(OBSTACLE);
  84. if (playerPosition == nearestObstaclePosition)
  85. {
  86. isStruck = true;
  87. }
  88. else
  89. {
  90. points++;
  91. }
  92.  
  93. int nearestNitroPosition = board[board.Length - 2].IndexOf(NITRO);
  94. if (playerPosition == nearestNitroPosition)
  95. {
  96. isNitroFuel = true;
  97. }
  98.  
  99. int nearestPointPosition = board[board.Length - 2].IndexOf(POINTS);
  100. if (playerPosition == nearestPointPosition)
  101. {
  102. points += 10;
  103. }
  104.  
  105. //Creating a new obstacle
  106. int obstaclePosition = generatorRandom.Next(3);
  107. string obstacle = SetObstacle(obstaclePosition);
  108.  
  109. if (generatorRandom.Next(20) == 0)
  110. {
  111. int nitroPosition = generatorRandom.Next(3);
  112. obstacle = SetNitro(nitroPosition, obstacle);
  113. }
  114.  
  115.  
  116. if (generatorRandom.Next(20) == 0)
  117. {
  118. int pointPosition = generatorRandom.Next(3);
  119. obstacle = SetPoints(pointPosition, obstacle);
  120. }
  121.  
  122. //Moving the game board down
  123. for (int i = board.Length - 2; i > 0; i--)
  124. {
  125. board[i] = board[i - 1];
  126. }
  127. board[0] = obstacle;
  128.  
  129. SetPlayer(playerPosition);
  130. ShowBoard();
  131. Console.WriteLine($"Nitro: {isNitroFuel}");
  132. speed++;
  133. if (speed > 600)
  134. {
  135. Thread.Sleep(1);
  136. }
  137. else
  138. {
  139. Thread.Sleep(600 - speed);
  140. }
  141. }
  142.  
  143.  
  144. Console.Clear();
  145. Console.WriteLine("GAME OVER");
  146. Console.WriteLine($"You have earned {points} points");
  147. if(points > highestScore)
  148. {
  149. Console.WriteLine("NEW HIGHEST SCORE!!!");
  150. highestScore = points;
  151. }
  152. Thread.Sleep(2000);
  153. Console.WriteLine("Do you want to play again?");
  154. reset = Console.ReadLine();
  155. points = 0;
  156. } while (reset == "yes" || reset == "Yes");
  157. Console.ReadKey();
  158. }
  159.  
  160. private static void NewBoard(int boardSize)
  161. {
  162. board = new string[boardSize];
  163. for (int i = 0; i < board.Length; i++)
  164. {
  165. board[i] = "";
  166. }
  167. }
  168.  
  169. private static string SetObstacle(int position)
  170. {
  171. string line = " ";
  172. line = line.Insert(position, OBSTACLE);
  173. return line;
  174. }
  175.  
  176. private static string SetNitro(int position, string line)
  177. {
  178. line = line.Remove(position,1).Insert(position, NITRO);
  179. return line;
  180. }
  181.  
  182. private static string SetPoints(int position, string line)
  183. {
  184. line = line.Insert(position, POINTS);
  185. return line;
  186. }
  187. private static void SetPlayer(int position)
  188. {
  189. string line = " "; //in quotation marks we insert 3 spaces
  190. line = line.Insert(position, PLAYER);
  191. board[board.Length - 1] = line;
  192. }
  193.  
  194.  
  195.  
  196. private static void ShowBoard()
  197. {
  198. Console.Clear();
  199. for (int i = 0; i < board.Length; i++)
  200. {
  201. Console.WriteLine(board[i]);
  202. }
  203. Console.WriteLine($"Points: {points}");
  204. }
  205. }
  206. }
  207.  
  208.  
Add Comment
Please, Sign In to add comment