private void timer1_Tick(object sender, EventArgs e)
        {


            time++;
            background.Margin = new Thickness(background.Margin.Left - 1.5, background.Margin.Top, 0, 0);

          
            if ((distance <= 43 && distance > 40) || (distance <= 55 && distance > 49) || (distance <= 67 && distance > 61) ||(distance <= 95 && distance > 90) || (distance <= 105 && distance > 102) )
                Canvas.SetTop(canvas.Children[bossPosition], Canvas.GetTop(canvas.Children[bossPosition]) - 1.5);
            else if ((distance <= 49 && distance > 43) || (distance <= 61 && distance > 55) || (distance <= 70 && distance > 67)|| (distance <= 102 && distance > 95) ||(distance <= 110 && distance > 105))
                Canvas.SetTop(canvas.Children[bossPosition], Canvas.GetTop(canvas.Children[bossPosition]) + 1.5);


            if (distance < 67 && distance > 40)
            {
                Random r = new Random(Guid.NewGuid().GetHashCode());
                if (r.Next() % 1000 > 950)
                {
                    Image myImage3 = new Image();
                    BitmapImage bi3 = new BitmapImage();
                    bi3.BeginInit();
                    bi3.UriSource = new Uri("角色/金幣.png", UriKind.Relative);
                    myImage3.Width = 30;
                    myImage3.Height = 30;
                    bi3.EndInit();
                    myImage3.Stretch = Stretch.Fill;
                    myImage3.Source = bi3;

                    Canvas.SetLeft(myImage3, 700);

                    Canvas.SetTop(myImage3, Canvas.GetTop(canvas.Children[bossPosition]) + 100);
                    player.Pause();
                    canvas.Children.Add(myImage3);



                    player.Play();
                    total++;
                }


            }
            if (distance < 107 && distance > 90)
            {
                Random r = new Random(Guid.NewGuid().GetHashCode());
                if (r.Next() % 1000 > 950)
                {
                    Image myImage3 = new Image();
                    BitmapImage bi3 = new BitmapImage();
                    bi3.BeginInit();
                    bi3.UriSource = new Uri("角色/金幣.png", UriKind.Relative);
                    myImage3.Width = 30;
                    myImage3.Height = 30;
                    bi3.EndInit();
                    myImage3.Stretch = Stretch.Fill;
                    myImage3.Source = bi3;

                    Canvas.SetLeft(myImage3, 700);

                    Canvas.SetTop(myImage3, r.Next() % 600);
                    player.Pause();
                    canvas.Children.Add(myImage3);



                    player.Play();
                    total++;
                }


            }


            if (distance <= 70 && distance > 40)
            {
                for (int i = bossPosition + 1; i < total + 2; i++)
                {
                    Canvas.SetLeft(canvas.Children[i], Canvas.GetLeft(canvas.Children[i]) - 10);
                }
            }
            else if (distance <= 110 && distance > 93) { 
                for (int i = bossPosition + 1; i < total + 2; i++)
                {
                    Canvas.SetLeft(canvas.Children[i], Canvas.GetLeft(canvas.Children[i]) - 50);
                }
        }

            for (int i = 2; i < total + 2; i++)
            {

                if (distance < 40 )
                {
                    Canvas.SetLeft(canvas.Children[i], Canvas.GetLeft(canvas.Children[i]) - 6 - difficulty / 7);

                    if (Canvas.GetLeft(image) + 70 > Canvas.GetLeft(canvas.Children[i]) && Canvas.GetLeft(image) < Canvas.GetLeft(canvas.Children[i]))
                        if ((Canvas.GetTop(image) + image.Height >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) + image.Height <= Canvas.GetTop(canvas.Children[i]) + 155) || (Canvas.GetTop(image) >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) <= Canvas.GetTop(canvas.Children[i]) + 155))
                        {
                            player.Stop();
                            money.Stop();
                            timer.Stop();
                            MessageBox.Show("飛行時間:" + distance + "秒");

                            MainWindow mw = new MainWindow();
                            mw.Show();
                            countWindow cw = new countWindow();
                            this.Close();
                            cw.ShowDialog();
                            //mw.Show();


                        }
                }
                else
                {
                    if (Canvas.GetLeft(image) + 70 > Canvas.GetLeft(canvas.Children[i]) && Canvas.GetLeft(image) < Canvas.GetLeft(canvas.Children[i]))
                        if ((Canvas.GetTop(image) + image.Height >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) + image.Height <= Canvas.GetTop(canvas.Children[i]) + 25.5) || (Canvas.GetTop(image) >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) <= Canvas.GetTop(canvas.Children[i]) + 25.5))
                        {
                            player.Stop();
                            money.Stop();
                            timer.Stop();
                            MessageBox.Show("飛行時間:" + distance + "秒", "遊戲結束");

                            MainWindow mw = new MainWindow();
                            mw.Show();
                            countWindow cw = new countWindow();
                            this.Close();

                            cw.ShowDialog();
                            //mw.Show();

                        }
                }
            }
            //}
                 for (int i = 2; i < total + 2; i++)
                 {

                if ((distance < 90 && distance >70) || distance >= 110)
                {
                    Canvas.SetLeft(canvas.Children[i], Canvas.GetLeft(canvas.Children[i]) -6 - difficulty /5.5);

                    if (Canvas.GetLeft(image) + 70 > Canvas.GetLeft(canvas.Children[i]) && Canvas.GetLeft(image) < Canvas.GetLeft(canvas.Children[i]))
                        if ((Canvas.GetTop(image) + image.Height >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) + image.Height <= Canvas.GetTop(canvas.Children[i]) + 155) || (Canvas.GetTop(image) >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) <= Canvas.GetTop(canvas.Children[i]) + 155))
                        {
                            player.Stop();
                            money.Stop();
                            timer.Stop();
                            MessageBox.Show("飛行時間:" + distance + "秒");

                            MainWindow mw = new MainWindow();
                            mw.Show();
                            countWindow cw = new countWindow();
                            this.Close();
                            cw.ShowDialog();
                            //mw.Show();
                         

                        }
                }
                else
                {
                    if (Canvas.GetLeft(image) + 70 > Canvas.GetLeft(canvas.Children[i]) && Canvas.GetLeft(image) < Canvas.GetLeft(canvas.Children[i]))
                        if ((Canvas.GetTop(image) + image.Height >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) + image.Height <= Canvas.GetTop(canvas.Children[i]) + 25.5) || (Canvas.GetTop(image) >= Canvas.GetTop(canvas.Children[i]) && Canvas.GetTop(image) <= Canvas.GetTop(canvas.Children[i]) + 25.5))
                        {
                            player.Stop();
                            money.Stop();
                            timer.Stop();
                            MessageBox.Show("飛行時間:" + distance + "秒", "遊戲結束");

                            MainWindow mw = new MainWindow();
                            mw.Show();
                            countWindow cw = new countWindow();
                            this.Close();
                            
                            cw.ShowDialog();
                            //mw.Show();
                            
                        }
                }
            }




        }
        private void count(object sender, RoutedEventArgs e)
        {
              
            countWindow cw = new countWindow();
                cw.Owner = this;
                cw.ShowDialog();

        }