随机的敌人模式

时间:2015-08-13 03:01:49

标签: c# random xna-4.0 monogame

我想要一个随机移动我的敌人的模式,因为当我有3或4个敌人时,他们看起来像是同一个敌人的运动。 这是我的随机代码:

   public  void move(GameTime time, WorldScreen screen)
    {
        timer += (float)time.ElapsedGameTime.TotalMilliseconds;
        if (timer > 2000)
        {
            switch (rand.Next(0, 3))
            {
                case 0:
                    base.direction = Enemy.Direction.down;
                    break;
                case 1:
                    base.direction = Enemy.Direction.right;
                    break;
                case 2:
                    base.direction = Enemy.Direction.left;
                    break;
                case 3:
                    base.direction = Enemy.Direction.up;
                    break;

            }
            timer = 0;
        }
        timer1 += (float)time.ElapsedGameTime.TotalMilliseconds;
        if (timer1 > 50)
        {
            timer1 = 0;
            if (direction == Enemy.Direction.down)
            {
                if (!MoveCollision(new Vector2(position.X, position.Y + speed), screen))
                {
                    base.position.Y += speed;
                    ActualAnimation = getAnimation("rundown");
                }
                else
                {
                    base.direction = Enemy.Direction.right;
                }
            }
            if (direction == Enemy.Direction.up)
            {
                if (!MoveCollision(new Vector2(position.X, position.Y - speed), screen))
                {
                    base.position.Y -= speed;
                    ActualAnimation = getAnimation("runup");
                }
                else
                {
                    base.direction = Enemy.Direction.down;
                }
            }
            if (direction == Enemy.Direction.right)
            {
                if (!MoveCollision(new Vector2(position.X + speed, position.Y), screen))
                {
                    base.position.X += speed;
                    ActualAnimation = getAnimation("runright");
                }
                else
                {
                    base.direction = Enemy.Direction.left;
                }
            }
            if (direction == Enemy.Direction.left)
            {
                if (!MoveCollision(new Vector2(position.X - speed, position.Y), screen))
                {
                    base.position.X -= speed;
                    ActualAnimation = getAnimation("runleft");
                }
                else
                {
                    base.direction = Enemy.Direction.up;
                }

            }
        }
        ActualAnimation.update(time, position);
    }

这里是处理移动的代码:

inout

但是这个代码对房间里的所有敌人都做了同样的模式。 请帮助我。

2 个答案:

答案 0 :(得分:0)

我相信问题就在于此

int seed = unchecked(System.DateTime.Now.Ticks.GetHashCode()); rand = new Random(seed);`

检查您是否多次呼叫它太近了。如果是这样,你就可以在

上获得相同的随机数
rand.Next(0, 3)

每当你打电话给你时,你都会获得相同的号码,因此你会在每个敌人身上获得相同的模式。

Similar question

答案 1 :(得分:0)

好的,我解决了这个问题,在读者面前放了一个计数器来做这样的种子(这是在地图阅读器上):

int id = map.Layers[1].Tiles[i].Gid;
            if (id == 172)
            {
                enemys.Add(new Hero.World.Entitys.Enemys.Octorock(new Vector2((int)position.X + map.Layers[1].Tiles[i].X * 16, (int)position.Y + map.Layers[1].Tiles[i].Y * 16),i));

            }

之后对敌人的构造者来说是这样的:

    public Octorock(Vector2 position,int i) :base(position)
    {
        int seed = unchecked(System.DateTime.Now.Millisecond);
         rand = new Random(seed*i);}