/// <summary> /// Handles the event when the ball falls down. /// </summary> private void DisposeOfBall() { try { // Dispose balls, rackets, bonuses. racketList.Clear(); ballList.Clear(); bonusList.Clear(); // Add a new racket. Racket racket = new Racket(canvasWidth / 2 - racketWidth / 2, canvasHeight - racketHeight, racketWidth, racketHeight, @"..\..\Resources\Media\Racket\normalracket.jpg"); racket.Direction = Racket.Directions.Stay; racket.StickyRacket = false; racket.IsDeleted = false; canvas.Children.Add(racket.GetRectangle()); racketList.Add(racket); // Add a new ball. Ball ball = new Ball((canvasWidth / 2) - ballRadius, canvasHeight - racketHeight - (ballRadius * 2), ballRadius * 2, ballRadius * 2, @"..\..\Resources\Media\Ball\normalball.jpg", ballHorizontalMovement, ballVerticalMovement, Ball.BallsType.Normal); ball.VerticalMovement = ballVerticalMovement > 0 ? ballVerticalMovement : -ballVerticalMovement; ball.HorizontalMovement = ballHorizontalMovement < 0 ? ballHorizontalMovement : -ballHorizontalMovement; ball.BallInMove = false; ball.RelativePosition = ball.Area.X + ball.Area.Width / 2 - racketList[0].Area.X; ball.IsDeleted = false; canvas.Children.Add(ball.GetEllipse()); ballList.Add(ball); } catch (Exception e) { errorLogViewModel.LogError(e); } }
/// <summary> /// Checks if the racket is in contact with a bonus. /// </summary> public void RacketAtContactWithBonus() { try { // There is at least one racket. if (racketList.Count > 0) { // Check each racket. foreach (Racket oneRacket in racketList) { // The racket is not deleted. if (!oneRacket.IsDeleted) { // There is at least one bonus. if (bonusList.Count > 0) { // Check each bonus. for (int i = 0; i < bonusList.Count; i++) { // The bonus is not deleted. if (!bonusList[i].IsDeleted) { // The racket and the bonus overleap somewhere. if (oneRacket.Area.X < bonusList[i].Area.X + bonusList[i].Area.Width && /* bonus rigth side */ oneRacket.Area.X + oneRacket.Area.Width > bonusList[i].Area.X && /* bonus left side */ oneRacket.Area.Y < bonusList[i].Area.Y + bonusList[i].Area.Height) /* bonus bottom */ { // Add the score of the bonus to the player's bonus. playerScorePoint += bonusList[i].ScorePoint; #region AddBonusEffect // Add the bonus effect. switch (bonusList[i].BonusType) { case Bonus.BonusesType.LifeUp: playerLife++; break; case Bonus.BonusesType.LifeDown: playerLife--; break; case Bonus.BonusesType.NewBall: // Create ball and set the needed properties. Ball ball = new Ball(oneRacket.Area.X + (oneRacket.Area.Width / 2) - ballRadius, oneRacket.Area.Y - (ballRadius * 2), ballRadius * 2, ballRadius * 2, @"..\..\Resources\Media\Ball\normalball.jpg", 0, 0, Ball.BallsType.Normal); ball.VerticalMovement = ballVerticalMovement > 0 ? ballVerticalMovement : -ballVerticalMovement; ball.HorizontalMovement = ballHorizontalMovement < 0 ? ballHorizontalMovement : -ballHorizontalMovement; ball.BallInMove = false; ball.RelativePosition = ball.Area.X + ball.Area.Width / 2 - racketList[0].Area.X; ball.IsDeleted = false; canvas.Children.Add(ball.GetEllipse()); ballList.Add(ball); break; case Bonus.BonusesType.RacketLengthen: oneRacket.Lengthen(racketMaxSize, racketDifference); break; case Bonus.BonusesType.RacketShorten: oneRacket.Shorthen(racketMinSize, racketDifference); break; case Bonus.BonusesType.BallBigger: // There are at least one ball. if (ballList.Count > 0) { // Check each ball. foreach (var oneBall in ballList) { oneBall.ChangeBallToLarge(ballMaxRadius, ballRadius, canvasWidth, canvasHeight, racketHeight); } } break; case Bonus.BonusesType.BallSmaller: // There is at least one ball. if (ballList.Count > 0) { // Check each ball. foreach (var oneBall in ballList) { oneBall.ChangeBallToSmall(ballMinRadius); } } break; case Bonus.BonusesType.StickyRacket: // The racket is not sticky. if (!oneRacket.StickyRacket) { oneRacket.ChangeToSticky(); } break; case Bonus.BonusesType.HardBall: // There is at least one ball. if (ballList.Count > 0) { // Check each ball. foreach (var oneBall in ballList) { oneBall.ChangeToHard(); } } break; case Bonus.BonusesType.SteelBall: // There is at least one ball. if (ballList.Count > 0) { // Check each ball. foreach (var oneBall in ballList) { oneBall.ChangeToSteel(); } } break; } // The player's life points reached 0, game over. if (playerLife <= 0) { gameOverStatus = "fail"; gameIsOver = true; } #endregion AddBonusEffect // Set bonus to deleted. bonusList[i].IsDeleted = true; } } } } } } } } catch (Exception e) { errorLogViewModel.LogError(e); } }