Ejemplo n.º 1
0
        private void storyboard1_Completed(object sender, EventArgs e)
        {
            // when the explosion animation is finished, clear the canvas from the blast icon
            ClockGroup clockGroup = (ClockGroup)sender;

            DoubleAnimation completedAnimation = (DoubleAnimation)clockGroup.Children[0].Timeline;
            Blast           completedBlast     = (Blast)Storyboard.GetTarget(completedAnimation);

            playground.Children.Remove(completedBlast);
        }
Ejemplo n.º 2
0
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            // check if adjustments to balloons animation and timer are needed
            if ((DateTime.Now.Subtract(lastAdjustmentTime).TotalSeconds >
                 secondsBetweenAdjustments))
            {
                lastAdjustmentTime = DateTime.Now;

                secondsBetweenBalloons -= secondsBetweenBalloonsReduction;
                secondsToFloat         -= secondsToFloatReduction;

                balloonTimer.Interval = TimeSpan.FromSeconds(secondsBetweenBalloons);
            }

            // detect a popped balloon
            foreach (KeyValuePair <Balloon, Storyboard> item in storyboards)
            {
                Balloon balloon = item.Key;
                if (CheckCollision(needle, balloon))
                {
                    // play audio file with popping sound to make things realistic
                    player.Open(new Uri("../../files/pop_sound.mp3", UriKind.RelativeOrAbsolute));
                    player.Play();

                    // shut down running storyboard of the popped balloon
                    Storyboard storyboard = item.Value;
                    storyboard.Stop();


                    // discard a balloon from the canvas
                    playground.Children.Remove(balloon);


                    // check if the poppped balloon has a bomb inside
                    if (balloon.HaveBomb)
                    {
                        blastedBalloons++;
                        bombProgressBar.Value += 1;

                        // initiate blast
                        Blast blast = new Blast();


                        // put a blast icon in place where the balloon
                        // has been popped
                        double currentTop  = Canvas.GetTop(balloon);
                        double currentLeft = Canvas.GetLeft(balloon);
                        blast.SetValue(Canvas.TopProperty, currentTop);
                        blast.SetValue(Canvas.LeftProperty, currentLeft);
                        playground.Children.Add(blast);

                        // create explosion animation

                        DoubleAnimation widthAnimation = new DoubleAnimation();
                        widthAnimation.To       = 1.5;
                        widthAnimation.Duration = TimeSpan.FromSeconds(0.5);


                        Storyboard storyboard1 = new Storyboard();
                        Storyboard.SetTarget(widthAnimation, blast);
                        Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("RenderTransform.Children[0].ScaleX"));
                        storyboard1.Children.Add(widthAnimation);

                        DoubleAnimation heightAnimation = new DoubleAnimation();
                        heightAnimation.To       = 1.5;
                        heightAnimation.Duration = TimeSpan.FromSeconds(0.5);
                        Storyboard.SetTarget(heightAnimation, blast);
                        Storyboard.SetTargetProperty(heightAnimation, new PropertyPath("RenderTransform.Children[0].ScaleY"));
                        storyboard1.Children.Add(heightAnimation);

                        storyboard1.Completed += storyboard1_Completed;
                        storyboard1.Begin();
                    }
                    else
                    {
                        score++;
                        scoreProgressBar.Value += 1;
                    }
                }
            }

            // update a game board
            currentScore.Content = score.ToString();
            bombCount.Content    = blastedBalloons.ToString();

            // check if the game needs to finished as the number of popped
            // balloons with bombs surpassed the limit
            if (blastedBalloons >= maxBlastedBalloons)
            {
                StopGame();
                gameStatus.Text = "Game is over. You lost!";
            }

            // check if the game is won
            if (score >= PopBalloonsToWin)
            {
                StopGame();
                gameStatus.Text = "Game is over. You won!";
            }
        }