Home > Net >  how do i make my enemies chase my player?
how do i make my enemies chase my player?

Time:05-31

i try to make the 10 enemies go wherver i move my player try do some while statmrnts but coudlnt figure it out.

public sealed partial class MainPage : Page { int[] arr_Enemy = new 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;

        }
        


    }
   

} }

CodePudding user response:

there is two little changes needed on your code

1:

Rectangle Enemy;

to

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

2:

Enemy.Fill = new ImageBrush . . . .

to

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

CodePudding user response:

how do i make my enemies chase my player?

The better way is make timer to move enemy to player step by step. We could get enemy and player postion. and measure the distance between them. Then call set left-top method base on the distance. And stop moving when enemy and player cross.

for example

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
    }
    
}
  • Related