private void Play_Click(object sender, RoutedEventArgs e)
 {
     if (running == false)
     {
         Play.Content    = "";
         Play.Visibility = Visibility.Collapsed;
         if (GameObjects.Count > 85)
         {
             Restart();
         }
         else
         {
             BuildNextLevel();
         }
         winCountDown   = false;
         running        = true;
         lastUpdateTime = DateTime.Now;
         MainStoryboard.Begin();
     }
 }
        private void MainStoryboard_Completed(object sender, EventArgs e)
        {
            textBlockCount.Text = string.Format("No. of blocks - {0}", GameObjects.Count - 1);

            // the game loop
            elapsedTime    = DateTime.Now - lastUpdateTime;
            lastUpdateTime = DateTime.Now;
            double secs = (elapsedTime.TotalMilliseconds / 1000.0) + leftoverUpdateTime;

            while (secs > .01)
            {
                physicsSimulator.Update(.01f);

                List <IGameObject> delete = new List <IGameObject>();
                foreach (IGameObject iGameObject in GameObjects)
                {
                    if (iGameObject.Body.Position.Y != Canvas.GetTop(iGameObject.UIElement))
                    {
                        Canvas.SetTop(iGameObject.UIElement, iGameObject.Body.Position.Y);
                    }

                    if (iGameObject.Body.Position.X != (double)Canvas.GetLeft(iGameObject.UIElement))
                    {
                        Canvas.SetLeft(iGameObject.UIElement, iGameObject.Body.Position.X);
                    }

                    double theAngle = (iGameObject.Body.Rotation * 360) / (2 * Math.PI);
                    if (theAngle != iGameObject.RotateTransform.Angle)
                    {
                        iGameObject.RotateTransform.Angle = theAngle;
                    }

                    if (iGameObject.Body.Position.X > 600 ||
                        iGameObject.Body.Position.X < -10 ||
                        iGameObject.Body.Position.Y > 600 ||
                        iGameObject.Body.Position.Y < -10)
                    {
                        delete.Add(iGameObject);
                    }

                    double y = (double)winLine.GetValue(Canvas.TopProperty);
                    if (winCountDown == false &&
                        iGameObject.Y < y)
                    {
                        winCountDown = true;
                        winnerBrick  = iGameObject;
                        winnerClock  = DateTime.Now;
                    }

                    if (winCountDown == true)
                    {
                        Int32 seconds = (DateTime.Now - winnerClock).Seconds;
                        textBlockCount.Text = string.Format("Win in {0} seconds.", 3 - seconds);
                    }
                }

                foreach (IGameObject iGameObject in delete)
                {
                    iGameObject.UIElement.Visibility = Visibility.Collapsed;
                    GameObjects.Remove(iGameObject);
                    TheCanvas.Children.Remove(iGameObject.UIElement);
                    iGameObject.Delete();
                }

                secs -= .01;
            }
            leftoverUpdateTime = secs;

            double winLineY = (double)winLine.GetValue(Canvas.TopProperty);

            if (winCountDown == true &&
                winnerBrick != null &&
                winnerBrick.Y < winLineY)
            {
                if (winnerClock.AddSeconds(3) <= DateTime.Now)
                {
                    running             = false;
                    textBlockCount.Text = "You Win!";
                    if ((double)winLine.GetValue(Canvas.TopProperty) < 1.0)
                    {
                        Play.Content    = "You are a Grand Champion!";
                        Play.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        Play.Content    = "Next Level";
                        Play.Visibility = Visibility.Visible;
                    }
                }
            }
            else
            {
                winCountDown = false;
            }
            if (winCountDown == false &&
                running == true &&
                GameObjects.Count > 85)
            {
                Play.Content        = "Try Again";
                Play.Visibility     = Visibility.Visible;
                textBlockCount.Text = "Too many bricks!";
                running             = false;
            }

            if (running == true)
            {
                MainStoryboard.Begin();
            }
        }