/// <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>
        /// Adds a bonus.
        /// </summary>
        /// <param name="oneBrick">The brick.</param>
        private void AddBonus(Brick oneBrick)
        {
            try
            {
                string bonusImage = "";
                Bonus.BonusesType type = Bonus.BonusesType.BallBigger;

                // Random generate an effect for the bonus.
                switch (rnd.Next(1, 11))
                {
                    case 1:
                        type = Bonus.BonusesType.BallBigger;
                        bonusImage = @"..\..\Resources\Media\Bonus\ballbigger.jpg";
                        break;
                    case 2:
                        type = Bonus.BonusesType.BallSmaller;
                        bonusImage = @"..\..\Resources\Media\Bonus\ballsmaller.jpg";
                        break;
                    case 3:
                        type = Bonus.BonusesType.HardBall;
                        bonusImage = @"..\..\Resources\Media\Bonus\hardball.jpg";
                        break;
                    case 4:
                        type = Bonus.BonusesType.LifeDown;
                        bonusImage = @"..\..\Resources\Media\Bonus\lifedown.jpg";
                        break;
                    case 5:
                        type = Bonus.BonusesType.LifeUp;
                        bonusImage = @"..\..\Resources\Media\Bonus\lifeup.jpg";
                        break;
                    case 6:
                        type = Bonus.BonusesType.NewBall;
                        bonusImage = @"..\..\Resources\Media\Bonus\newball.jpg";
                        break;
                    case 7:
                        type = Bonus.BonusesType.RacketLengthen;
                        bonusImage = @"..\..\Resources\Media\Bonus\racketlengthen.jpg";
                        break;
                    case 8:
                        type = Bonus.BonusesType.RacketShorten;
                        bonusImage = @"..\..\Resources\Media\Bonus\racketshorten.jpg";
                        break;
                    case 9:
                        type = Bonus.BonusesType.SteelBall;
                        bonusImage = @"..\..\Resources\Media\Bonus\steelball.jpg";
                        break;
                    case 10:
                        type = Bonus.BonusesType.StickyRacket;
                        bonusImage = @"..\..\Resources\Media\Bonus\stickyracket.jpg";
                        break;
                }

                // Create the bonus and set the needed properties.
                Bonus bonus = new Bonus(oneBrick.Area.X + (oneBrick.Area.Width / 2) - (bonusWidth / 2), oneBrick.Area.Y + oneBrick.Area.Height,
                    bonusHeight, bonusWidth, bonusImage, type);
                bonus.ScorePoint = 50;
                bonus.IsDeleted = false;
                canvas.Children.Add(bonus.GetRectangle());
                bonusList.Add(bonus);

                // Move the bonus down.
                if (bonus.Descend(bonusSpeed, canvasWidth, canvasHeight))
                {
                    // Delete the bonus if the top of it reached the bottom of the canvas.
                    bonus.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);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Loads the bricks from the map.
        /// </summary>
        /// <param name="pathString">The path to the txt file.</param>
        /// <param name="brickWidth">The width of the bricks.</param>
        /// <param name="brickHeight">The height of the bricks.</param>
        /// <returns>The brick list or null.</returns>
        public ObservableCollection<Brick> LoadMap(string pathString, double brickWidth, double brickHeight)
        {
            try
            {
                if (FileExists(pathString))
                {
                    double X = 0;
                    double Y = 0;
                    ObservableCollection<Brick> returnValue = new ObservableCollection<Brick>();

                    // Open a connection to the txt file.
                    FileStream fileStream = new FileStream(pathString, FileMode.Open, FileAccess.Read, FileShare.None);

                    // Open a reader to read the txt file.
                    BinaryReader binaryReader = new BinaryReader(fileStream);

                    while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
                    {
                        // The string row needs to be checked by characters.
                        char[] oneLine = binaryReader.ReadChars(24);

                        for (int i = 0; i < oneLine.Length; i++)
                        {
                            switch (oneLine[i])
                            {
                                case '.':
                                    break;
                                case '1':
                                    Brick brick1 = new Brick(X, Y, brickWidth, brickHeight, @"..\..\Resources\Media\Brick\easybrick.jpg", Brick.BricksType.Easy);
                                    brick1.ScorePoint = 10;
                                    brick1.BreakNumber = 1;
                                    brick1.IsDeleted = false;
                                    returnValue.Add(brick1);
                                    break;
                                case '2':
                                    Brick brick2 = new Brick(X, Y, brickWidth, brickHeight, @"..\..\Resources\Media\Brick\mediumbrick.jpg", Brick.BricksType.Medium);
                                    brick2.ScorePoint = 20;
                                    brick2.BreakNumber = 2;
                                    brick2.IsDeleted = false;
                                    returnValue.Add(brick2);
                                    break;
                                case '3':
                                    Brick brick3 = new Brick(X, Y, brickWidth, brickHeight, @"..\..\Resources\Media\Brick\hardbrick.jpg", Brick.BricksType.Hard);
                                    brick3.ScorePoint = 30;
                                    brick3.BreakNumber = 5;
                                    brick3.IsDeleted = false;
                                    returnValue.Add(brick3);
                                    break;
                                case '4':
                                    Brick brick4 = new Brick(X, Y, brickWidth, brickHeight, @"..\..\Resources\Media\Brick\steelbrick.jpg", Brick.BricksType.Steel);
                                    brick4.ScorePoint = 40;
                                    brick4.BreakNumber = 1;
                                    brick4.IsDeleted = false;
                                    returnValue.Add(brick4);
                                    break;
                            }

                            // Next column.
                            X += brickWidth;
                        }

                        // Ready the variables for the next row.
                        X = 0;
                        Y += brickHeight;
                    }

                    // Close the BinaryReader and the FileStream.
                    binaryReader.Close();
                    fileStream.Close();

                    return returnValue;
                }

                return null;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// Adds a bonus.
        /// </summary>
        /// <param name="oneBrick">The brick.</param>
        private void AddBonus(Brick oneBrick)
        {
            try
            {
                string bonusImage = "";
                Bonus.BonusesType type = Bonus.BonusesType.BallBigger;

                switch (rnd.Next(1, 11))
                {
                    case 1:
                        type = Bonus.BonusesType.BallBigger;
                        bonusImage = @"..\..\Resources\Media\Bonus\ballbigger.jpg";
                        break;
                    case 2:
                        type = Bonus.BonusesType.BallSmaller;
                        bonusImage = @"..\..\Resources\Media\Bonus\ballsmaller.jpg";
                        break;
                    case 3:
                        type = Bonus.BonusesType.HardBall;
                        bonusImage = @"..\..\Resources\Media\Bonus\hardball.jpg";
                        break;
                    case 4:
                        type = Bonus.BonusesType.LifeDown;
                        bonusImage = @"..\..\Resources\Media\Bonus\lifedown.jpg";
                        break;
                    case 5:
                        type = Bonus.BonusesType.LifeUp;
                        bonusImage = @"..\..\Resources\Media\Bonus\lifeup.jpg";
                        break;
                    case 6:
                        type = Bonus.BonusesType.NewBall;
                        bonusImage = @"..\..\Resources\Media\Bonus\newball.jpg";
                        break;
                    case 7:
                        type = Bonus.BonusesType.RacketLengthen;
                        bonusImage = @"..\..\Resources\Media\Bonus\racketlengthen.jpg";
                        break;
                    case 8:
                        type = Bonus.BonusesType.RacketShorten;
                        bonusImage = @"..\..\Resources\Media\Bonus\racketshorten.jpg";
                        break;
                    case 9:
                        type = Bonus.BonusesType.SteelBall;
                        bonusImage = @"..\..\Resources\Media\Bonus\steelball.jpg";
                        break;
                    case 10:
                        type = Bonus.BonusesType.StickyRacket;
                        bonusImage = @"..\..\Resources\Media\Bonus\stickyracket.jpg";
                        break;
                }

                Bonus bonus = new Bonus(oneBrick.Area.X + (oneBrick.Area.Width / 2) - (bonusWidth / 2), oneBrick.Area.Y + oneBrick.Area.Height, bonusHeight, bonusWidth, bonusImage, type);
                bonus.ScorePoint = 5;
                bonusList.Add(bonus);
                gameObjectList.Add(bonus);

                if (bonus.Descend(bonusSpeed, canvasWidth, canvasHeight))
                {
                    bonusList.Remove(bonus);
                    gameObjectList.Remove(bonus);
                }
            }
            catch (Exception e)
            {
                errorLogViewModel.LogError(e);
            }
        }