// Drop a bomb. private void bombTimer_Tick(object sender, EventArgs e) { // Perform an "adjustment" when needed. if ((DateTime.Now.Subtract(lastAdjustmentTime).TotalSeconds > secondsBetweenAdjustments)) { lastAdjustmentTime = DateTime.Now; secondsBetweenBombs -= secondsBetweenBombsReduction; secondsToFall -= secondsToFallReduction; // (Technically, you should check for 0 or negative values. // However, in practice these won't occur because the game will // always end first.) // Set the timer to drop the next bomb at the appropriate time. bombTimer.Interval = TimeSpan.FromSeconds(secondsBetweenBombs); // Update the status message. lblRate.Text = String.Format("A bomb is released every {0} seconds.", secondsBetweenBombs); lblSpeed.Text = String.Format("Each bomb takes {0} seconds to fall.", secondsToFall); } // Create the bomb. Bomb bomb = new Bomb(); bomb.IsFalling = true; // Position the bomb. Random random = new Random(); bomb.SetValue(Canvas.LeftProperty, (double)(random.Next(0, (int)(canvasBackground.ActualWidth - 50)))); bomb.SetValue(Canvas.TopProperty, -100.0); // Attach mouse click event (for defusing the bomb). bomb.MouseLeftButtonDown += bomb_MouseLeftButtonDown; // Create the animation for the falling bomb. Storyboard storyboard = new Storyboard(); DoubleAnimation fallAnimation = new DoubleAnimation(); fallAnimation.To = canvasBackground.ActualHeight; fallAnimation.Duration = TimeSpan.FromSeconds(secondsToFall); Storyboard.SetTarget(fallAnimation, bomb); Storyboard.SetTargetProperty(fallAnimation, new PropertyPath("(Canvas.Top)")); storyboard.Children.Add(fallAnimation); // Create the animation for the bomb "wiggle." DoubleAnimation wiggleAnimation = new DoubleAnimation(); wiggleAnimation.To = 30; wiggleAnimation.Duration = TimeSpan.FromSeconds(0.2); wiggleAnimation.RepeatBehavior = RepeatBehavior.Forever; wiggleAnimation.AutoReverse = true; Storyboard.SetTarget(wiggleAnimation, ((TransformGroup)bomb.RenderTransform).Children[0]); Storyboard.SetTargetProperty(wiggleAnimation, new PropertyPath("Angle")); storyboard.Children.Add(wiggleAnimation); // Add the bomb to the Canvas. canvasBackground.Children.Add(bomb); // Add the storyboard to the tracking collection. storyboards.Add(bomb, storyboard); // Configure and start the storyboard. storyboard.Duration = fallAnimation.Duration; storyboard.Completed += storyboard_Completed; storyboard.Begin(); }