随机敌人模式

本文关键字:模式 敌人 随机 | 更新日期: 2023-09-27 18:07:45

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

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

这里是处理移动的代码:

   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);
    }

,但这段代码只是对房间里的所有敌人做相同的模式。谢谢你的帮助。

随机敌人模式

我认为问题出在这里

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

检查是否多次调用时间过近。如果是这样的话,你会在

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

每次你调用它,你都会得到相同的数字,因此你会在每个敌人身上得到相同的图案。

相似的问题

我解决了这个问题在for阅读器上放了一个计数器像这样做种子(这是在for地图阅读器上):

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);}