/// <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> /// Brick is at contact with ball. /// </summary> /// <param name="oneBall">One ball.</param> /// <param name="oneBrick">One brick.</param> private void BrickContact(Ball oneBall, Brick oneBrick) { try { // The ball is not a steel ball. if (oneBall.BallType != Ball.BallsType.Steel) { // The brick is not a steel brick. if (oneBrick.BrickType != Brick.BricksType.Steel) { // The ball is not a hard ball. if (oneBall.BallType != Ball.BallsType.Hard) { // The brick is at breaking point. if (oneBrick.BreakNumber == 1) { // If the brick is lucky, then add bonus. if (oneBrick.CalculateBonusChance()) { AddBonus(oneBrick); } // Add the brick's points to the player's points and make it deleted. playerScorePoint += oneBrick.ScorePoint; oneBrick.IsDeleted = true; } // The brick is not at breaking point. else { // Decrement the breaking number. oneBrick.DecrementBreakNumber(); } } // The ball is a hard ball. else { // The brick is lucky, then add bonus. if (oneBrick.CalculateBonusChance()) { AddBonus(oneBrick); } // Add the brick's points to the player's points and make it deleted. playerScorePoint += oneBrick.ScorePoint; oneBrick.IsDeleted = true; } } } // The ball is a steel ball. else { // If the brick is lucky, then add bonus. if (oneBrick.CalculateBonusChance()) { AddBonus(oneBrick); } // Add the brick's points to the player's points and make it deleted. playerScorePoint += oneBrick.ScorePoint; oneBrick.IsDeleted = true; } PlaySound(); } catch (Exception e) { errorLogViewModel.LogError(e); } }
/// <summary> /// Handles the event when the ball falls down. /// </summary> private void DisposeOfBall() { try { // Dispose balls, rackets, bonuses. racketList.Clear(); ballList.Clear(); bonusList.Clear(); gameObjectList.Clear(); if (brickList.Count > 0) { for (int i = 0; i < brickList.Count; i++) { gameObjectList.Add(brickList[i]); } } // Add new racket. Racket racket = new Racket((canvasWidth / 2) - (racketWidth / 2), canvasHeight - racketHeight, racketHeight, racketWidth, @"..\..\Resources\Media\Racket\normalracket.jpg"); racket.Direction = Racket.Directions.Stay; racket.StickyRacket = false; racketList.Add(racket); gameObjectList.Add(racket); // Add 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; ballList.Add(ball); gameObjectList.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); } }
/// <summary> /// Brick is at contact with ball. /// </summary> /// <param name="oneBall">One ball.</param> /// <param name="oneBrick">One brick.</param> private void BrickContact(Ball oneBall, Brick oneBrick) { try { // The ball is not a steel ball. if (oneBall.BallType != Ball.BallsType.Steel) { // The brick is not a steel brick. if (oneBrick.BrickType != Brick.BricksType.Steel) { // The ball is not a hard ball. if (oneBall.BallType != Ball.BallsType.Hard) { // The brick is at breaking point. if (oneBrick.BreakNumber == 1) { //// Add points to the score. //scoreValue += oneBrick.ScorePoint; //// Show the scorepints. //ScoreLabel.Content = "Score: " + scoreValue; // If the brick is lucky, then add bonus. if (oneBrick.CalculateBonusChance()) { AddBonus(oneBrick); } brickList.Remove(oneBrick); gameObjectList.Remove(oneBrick); } // The brick is not at breaking point. else { // Decrement the breaking number. oneBrick.DecrementBreakNumber(); } } // The ball is a hard ball. else { //// Add points to the score. //scoreValue += oneBrick.ScorePoint; //// Show the scorepints. //ScoreLabel.Content = "Score: " + scoreValue; // The brick is lucky, then add bonus. if (oneBrick.CalculateBonusChance()) { AddBonus(oneBrick); } brickList.Remove(oneBrick); gameObjectList.Remove(oneBrick); } } } // The ball is a steel ball. else { //// Add points to the score. //scoreValue += oneBrick.ScorePoint; //// Show the scorepints. //ScoreLabel.Content = "Score: " + scoreValue; // If the brick is lucky, then add bonus. if (oneBrick.CalculateBonusChance()) { AddBonus(oneBrick); } brickList.Remove(oneBrick); gameObjectList.Remove(oneBrick); } // The sound is a on. if (GetOption().IsSoundEnabled) { // Play the sound. mediaPlayer.Position = new TimeSpan(0); mediaPlayer.Play(); } } catch (Exception e) { errorLogViewModel.LogError(e); } }