持续随机飞行物体/矩形WPF C#

时间:2018-02-07 11:52:00

标签: c# wpf

我在C#WPF中创建简单游戏只是为了更多地了解语言。我的目标是创建类似于太空入侵者的游戏。我正在创造敌人,我想从一点到另一点随机飞行。然而,它飞到第一个randomed点,然后继续重复做同样的动作(包括传送到起点,这不是我的观点,你可以猜到)任何人都可以帮助我弄清楚我可以在这里更改/添加什么?这是创建和移动该对象的方法。我不认为这里需要更多的代码(我的意思是整个游戏)。

        public void CreateEnemy()
    {
        Random randomEnemy = new Random();
        Rectangle enemy = new Rectangle();
        var Urii = new Uri("pack://application:,,,/Modele/ufoRed.png");
        enemy.Width = 30;
        enemy.Height = 30;
        ImageBrush enemyImg = new ImageBrush();
        enemyImg.ImageSource = new BitmapImage(Urii);
        enemy.Fill = enemyImg;
        myCanvas.Children.Add(enemy);

        enemy.SetValue(Canvas.LeftProperty, 400d);
        enemy.SetValue(Canvas.TopProperty, 0d);

        Storyboard enemyAnimate = new Storyboard();
        DoubleAnimation enemyAnimation;

        int topRandom = randomEnemy.Next(50, 300);
        int leftRandom = randomEnemy.Next(0, 500);
        //up and down
        enemyAnimation = new DoubleAnimation(topRandom, new Duration(new TimeSpan(0, 0, 0, 5)));
        enemyAnimation.AutoReverse = false;
        Storyboard.SetTarget(enemyAnimation, enemy);
        Storyboard.SetTargetProperty(enemyAnimation, new PropertyPath("(Canvas.Top)"));
        enemyAnimate.Children.Add(enemyAnimation);
        // left and right
        enemyAnimation = new DoubleAnimation(leftRandom, new Duration(new TimeSpan(0, 0, 0, 5)));
        enemyAnimation.AutoReverse = false;
        Storyboard.SetTarget(enemyAnimation, enemy);
        Storyboard.SetTargetProperty(enemyAnimation, new PropertyPath("(Canvas.Left)"));
        enemyAnimate.Children.Add(enemyAnimation);
        enemyAnimation.RepeatBehavior = RepeatBehavior.Forever;
        enemyAnimate.Begin();
    }

问候, kim0z

0 个答案:

没有答案