/// <summary> /// Returns true if the current match is a team fight /// </summary> /// <returns></returns> public bool IsTeamMatch() { if (Player1.Contains("Team") || Player2.Contains("Team")) { return(true); } else { return(false); } }
public void Play() { // Declare the DateTime variables which will be compared for the gamespeed DateTime dtLoopStart; DateTime dtWaiting; // set the console width and height Console.WindowHeight = Height + 10; Console.WindowWidth = Width; // Set the ball to a random start location and direction SetRandomBallProperties(); // Clear the console Console.Clear(); // Ensure black color Console.BackgroundColor = ConsoleColor.Black; // Set the title of the window Console.Title = $"First to: {MaxScore}"; // enter a do while loop which will continue until 'GameOver' is True do { // Clear and redraw components Console.Clear(); DrawPlayArea(); DrawPaddles(); DrawBall(); DrawScore(); // check whether the ball has passed a players paddle if (Ball.Position.XCoordinate < Player1.Left) { // Increment Player2's score Player2.Score++; // Set the ball to a random start location and direction SetRandomBallProperties(); } else if (Ball.Position.XCoordinate > Player2.Left) { // Increment Player1's score Player1.Score++; // Set the ball to a random start location and direction SetRandomBallProperties(); } // If ball touches edge, invert y direction if (Ball.Position.YCoordinate <= 1) { switch (Ball.Direction) { case EnumDirection.NorthWest: Ball.Direction = EnumDirection.SouthWest; break; case EnumDirection.NorthEast: Ball.Direction = EnumDirection.SouthEast; break; case EnumDirection.North: Ball.Direction = EnumDirection.West; break; } } else if (Ball.Position.YCoordinate >= Height - 1) { switch (Ball.Direction) { case EnumDirection.SouthWest: Ball.Direction = EnumDirection.NorthWest; break; case EnumDirection.SouthEast: Ball.Direction = EnumDirection.NorthEast; break; case EnumDirection.South: Ball.Direction = EnumDirection.West; break; } } // If it's gameover - break if (GameOver) { break; } #region Determine the Ball's future position Vector posBallFuture = new Vector(Ball.Position.XCoordinate, Ball.Position.YCoordinate); switch (Ball.Direction) { case EnumDirection.NorthWest: posBallFuture.XCoordinate--; posBallFuture.YCoordinate--; break; case EnumDirection.SouthWest: posBallFuture.XCoordinate--; posBallFuture.YCoordinate++; break; case EnumDirection.NorthEast: posBallFuture.XCoordinate++; posBallFuture.YCoordinate--; break; case EnumDirection.SouthEast: posBallFuture.XCoordinate++; posBallFuture.YCoordinate++; break; case EnumDirection.East: posBallFuture.XCoordinate++; break; case EnumDirection.West: posBallFuture.XCoordinate--; break; } #endregion // Instantiate the DateTime variables which will be compared for the gamespeed dtLoopStart = DateTime.Now; dtWaiting = DateTime.Now; // While the difference in time is less that the desired game speed while (dtWaiting.Subtract(dtLoopStart).TotalMilliseconds <= GameSpeed) { // Refresh the DateTime Waiting to Now dtWaiting = DateTime.Now; // if there is a keyinfo object in the input stream if (Console.KeyAvailable) { // get the key that has been pressed and intercept this so it doesn't right to the console ConsoleKeyInfo keyPressed = Console.ReadKey(true); // Set the respective player's direction based on the key pressed or pauses the game if (keyPressed.Key.Equals(ConsoleKey.UpArrow)) { Player2.Direction = EnumDirection.North; } else if (keyPressed.Key.Equals(ConsoleKey.DownArrow)) { Player2.Direction = EnumDirection.South; } else if (keyPressed.Key.Equals(ConsoleKey.W)) { Player1.Direction = EnumDirection.North; } else if (keyPressed.Key.Equals(ConsoleKey.S)) { Player1.Direction = EnumDirection.South; } else if (keyPressed.Key.Equals(ConsoleKey.P)) { Pause(); } else if (keyPressed.Key.Equals(ConsoleKey.Escape)) { Environment.Exit(0); } } #region check whether it is possible for the player's paddle to move in the desired direction int iBottom = Player1.Bottom; switch (Player1.Direction) { case EnumDirection.South: if (iBottom + 2 < Height) { Player1.Move(); } break; case EnumDirection.North: if (iBottom - Player1.Pixels.Length > 0) { Player1.Move(); } break; } iBottom = Player2.Bottom; switch (Player2.Direction) { case EnumDirection.South: if (iBottom + 2 < Height) { Player2.Move(); } break; case EnumDirection.North: if (iBottom - Player2.Pixels.Length > 0) { Player2.Move(); } break; } #endregion #region If the ball reaches a player's paddle - reverse the direction if (Player1.Contains(posBallFuture)) { if (Player1.Direction == EnumDirection.North && Ball.Direction == EnumDirection.NorthWest) { Ball.Direction = EnumDirection.East; } else if (Player1.Direction == EnumDirection.South && Ball.Direction == EnumDirection.SouthWest) { Ball.Direction = EnumDirection.East; } else if (Player1.Direction == EnumDirection.North && Ball.Direction == EnumDirection.West) { Ball.Direction = EnumDirection.SouthEast; } else if (Player1.Direction == EnumDirection.South && Ball.Direction == EnumDirection.West) { Ball.Direction = EnumDirection.NorthEast; } else if (Player1.Direction == EnumDirection.Unknown) { string sBallDirection = Ball.Direction.ToString(); if (sBallDirection.Contains("South")) { Ball.Direction = EnumDirection.SouthEast; } else if (sBallDirection.Contains("North")) { Ball.Direction = EnumDirection.NorthEast; } else { Ball.Direction = EnumDirection.East; } } } if (Player2.Contains(posBallFuture)) { if (Player2.Direction == EnumDirection.North && Ball.Direction == EnumDirection.NorthEast) { Ball.Direction = EnumDirection.West; } else if (Player2.Direction == EnumDirection.South && Ball.Direction == EnumDirection.SouthEast) { Ball.Direction = EnumDirection.West; } else if (Player2.Direction == EnumDirection.North && Ball.Direction == EnumDirection.East) { Ball.Direction = EnumDirection.SouthWest; } else if (Player2.Direction == EnumDirection.South && Ball.Direction == EnumDirection.East) { Ball.Direction = EnumDirection.NorthWest; } else if (Player2.Direction == EnumDirection.Unknown) { string sBallDirection = Ball.Direction.ToString(); if (sBallDirection.Contains("South")) { Ball.Direction = EnumDirection.SouthWest; } else if (sBallDirection.Contains("North")) { Ball.Direction = EnumDirection.NorthWest; } else { Ball.Direction = EnumDirection.West; } } } #endregion // reset player's directions to stop them from moving infinitely Player1.Direction = EnumDirection.Unknown; Player2.Direction = EnumDirection.Unknown; } // Set the ball's position to the future position Ball.Position = posBallFuture; } while (!GameOver); // Display GameOverScreen DisplayGameOverScreen(); }