我如何让我的敌人追我的玩家?

how do i make my enemies chase my player?

我试着让 10 个敌人走到我移动我的玩家的任何地方,尝试做一些统计但无法弄清楚。

public 密封部分 class MainPage : Page { int[] arr_Enemy = 新的 int[10];

    Rectangle Enemy;

    Rectangle Player;

    Random rnd = new Random();  
    
    public MainPage()        
        
    {
        
    this.InitializeComponent();

    Window.Current.CoreWindow.KeyDown += PlayerMovement;

    MyCanvas.Background = new ImageBrush

       {

ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Background.jpg"))

        };

       Player = new Rectangle();

       Player.Width = 50;

       Player.Height = 50;

       Player.Fill = new ImageBrush

        {

ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/PlAYER2.GIF"))

        };

       MyCanvas.Children.Add(Player);

       Canvas.SetTop(Player, 700);

       Canvas.SetLeft(Player, 900);

        for (int i = 0; i < arr_Enemy.Length; i++)

        {
          
            Enemy.Fill = new ImageBrush

            {

ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/enemy.GIF"))

            };
         Enemy.Width = 120;

         Enemy.Height = 115;

         MyCanvas.Children.Add(Enemy);

         Canvas.SetTop(Enemy, rnd.Next(750));

         Canvas.SetLeft(Enemy, rnd.Next(750));

        }


    }
    private void PlayerMovement(CoreWindow sender, KeyEventArgs args)

    {


    VirtualKey movement = args.VirtualKey;

    switch (movement)

       {

         case VirtualKey.Up:
             Canvas.SetTop(Player, Canvas.GetTop(Player) - 10);
         break;

         case VirtualKey.Down:
             Canvas.SetTop(Player, Canvas.GetTop(Player) + 10);
         break;

         case VirtualKey.Left:
             Canvas.SetLeft(Player, Canvas.GetLeft(Player) - 10);
         break;

         case VirtualKey.Right:
             Canvas.SetLeft(Player, Canvas.GetLeft(Player) + 10);
         break;

        }
        


    }
   

} }

您的代码需要做两处小改动

1:

Rectangle Enemy;

System.Collections.Generic.List<Rectangle> Enemies = new System.Collections.Generic.List<Rectangle>();

2:

Enemy.Fill = new ImageBrush . . . .

var Enemy = new Rectangle();
Enemies.Add(Enemy);
Enemy.Fill = new ImageBrush . . . .

how do i make my enemies chase my player?

更好的方法是让计时器一步步将敌人移动到玩家身边。我们可以获得敌人和玩家的位置。并测量它们之间的距离。然后根据距离调用 set left-top 方法。当敌人和玩家交叉时停止移动。

例如

private void Chase(FrameworkElement enemy)
{
    var enemyLeft = Canvas.GetLeft(enemy);
    var enemyTop = Canvas.GetTop(enemy);
 
    var playerLeft = Canvas.GetLeft(Player);
    var playerTop = Canvas.GetTop(Player);
  
    var distance = new Point(playerLeft - enemyLeft, playerTop - enemyTop);

    if (distance.X == 0 && distance.Y == 0 || !DetectCollisions(Player,enemy).IsEmpty) return;

    if (distance.X > 0 && distance.Y > 0)
    {
        //down-right
        Canvas.SetTop(enemy, Canvas.GetTop(enemy) + 10);
        Canvas.SetLeft(enemy, Canvas.GetLeft(enemy) + 10);

    }
    else if (distance.X < 0 && distance.Y < 0)
    {
        //up-left
        Canvas.SetTop(enemy, Canvas.GetTop(enemy) - 10);
        Canvas.SetLeft(enemy, Canvas.GetLeft(enemy) - 10);
    }
    else if (distance.X > 0 && distance.Y < 0)
    {
        Canvas.SetTop(enemy, Canvas.GetTop(enemy) - 10);
        Canvas.SetLeft(enemy, Canvas.GetLeft(enemy) + 10);
        //up-right

    }
    else if (distance.X < 0 && distance.Y > 0)
    {
        Canvas.SetTop(enemy, Canvas.GetTop(enemy) + 10);
        Canvas.SetLeft(enemy, Canvas.GetLeft(enemy) - 10);
        //down-left
    }
    
}