我需要制作一个AI角色,当玩家在C#XNA中达到一定距离时,它会跟随玩家

我需要制作一个AI角色,当玩家在C#XNA中达到一定距离时,它会跟随玩家,c#,xna,artificial-intelligence,path-finding,C#,Xna,Artificial Intelligence,Path Finding,这是我到目前为止的游戏代码。我想创建一个AI角色(BlackBall),当玩家(WhiteBall)相距一定距离时,它将跟随玩家(WhiteBall)。我不知道从哪里开始,但这将是我比赛的主要部分,所以这是必不可少的 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using

这是我到目前为止的游戏代码。我想创建一个AI角色(BlackBall),当玩家(WhiteBall)相距一定距离时,它将跟随玩家(WhiteBall)。我不知道从哪里开始,但这将是我比赛的主要部分,所以这是必不可少的

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace PickUpTheCrew
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont TitleFont;


    private Vector2 playerPos = Vector2.Zero;
    Vector2 BlackBallPos;
    Vector2 position, velocity;
    Vector2 scorePos;
    Vector2 saved;
    private KeyboardState keyboardState;
    private KeyboardState prevKeyboardState;
    private bool canMove = true;

    int score;

    //Textures for background, player and sharks.
    Texture2D BlackBallTexture;
    Texture2D BlackBallTexture2;
    Texture2D BlueBallTexture;
    Texture2D GreenBallTexture;
    Texture2D OrangeBallTexture;
    Texture2D PinkBallTexture;
    Texture2D RedBallTexture;
    Texture2D WhiteBallTexture;
    Texture2D YellowBallTexture;

    Rectangle BlackBallRectangle;
    Rectangle BlackBallRectangle2;
    Rectangle BlueBallRectangle;
    Rectangle GreenBallRectangle;
    Rectangle OrangeBallRectangle;
    Rectangle PinkBallRectangle;
    Rectangle RedBallRectangle;
    Rectangle WhiteBallRectangle;
    Rectangle YellowBallRectangle;

    Sprite mainPlayer;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {

        score = 0;

        playerPos = new Vector2(this.GraphicsDevice.Viewport.Width / 2,
                       this.GraphicsDevice.Viewport.Height * 0.25f);
        BlackBallPos = new Vector2(this.GraphicsDevice.Viewport.Width / 2,
                       this.GraphicsDevice.Viewport.Height * 0.75f);

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>

    private Texture2D BackgroundTexture;
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(Gra`enter code here`phicsDevice);

        TitleFont = Content.Load<SpriteFont>("TitleFont");
        WhiteBallTexture = Content.Load<Texture2D>("WhiteBall");

        //mainPlayer = new Sprite(Content.Load<Texture2D>("WhiteBall"), new Rectangle((int)(playerPos.X - WhiteBallTexture.Width / 2),
        //(int)(playerPos.Y - WhiteBallTexture.Height / 2), WhiteBallTexture.Width, WhiteBallTexture.Height));

        BackgroundTexture = Content.Load<Texture2D>("Background");
        BlackBallTexture = Content.Load<Texture2D>("BlackBall");
        BlackBallTexture2 = Content.Load<Texture2D>("BlackBall");
        BlueBallTexture = Content.Load<Texture2D>("BlueBall");
        GreenBallTexture = Content.Load<Texture2D>("GreenBall");
        OrangeBallTexture = Content.Load<Texture2D>("OrangeBall");
        PinkBallTexture = Content.Load<Texture2D>("PinkBall");
        RedBallTexture = Content.Load<Texture2D>("RedBall");
        YellowBallTexture = Content.Load<Texture2D>("YellowBall");

        WhiteBallRectangle = new Rectangle(100, 100, 25,25);
        BlackBallRectangle = new Rectangle(150, 300, 25,25);
        BlackBallRectangle2 = new Rectangle(500, 400, 25, 25);
        BlueBallRectangle = new Rectangle(500, 150, 25, 25);
        GreenBallRectangle = new Rectangle(100, 500, 25, 25);
        OrangeBallRectangle = new Rectangle(180, 200, 25, 25);
        PinkBallRectangle = new Rectangle(260, 260, 25, 25);
        RedBallRectangle = new Rectangle(300, 450, 25, 25);
        YellowBallRectangle = new Rectangle(550, 300, 25, 25);

        scorePos.X = 575;
        scorePos.Y = 450;
        saved.X = 0;
        saved.Y = 50;
        /*
        WhiteBallRectangle = new Rectangle((int)(playerPos.X - WhiteBallTexture.Width / 2),
        (int)(playerPos.Y - WhiteBallTexture.Height / 2), WhiteBallTexture.Width, WhiteBallTexture.Height);
        BlackBallRectangle = new Rectangle((int)(BlackBallPos.X - BlackBallTexture.Width / 2),
        (int)(BlackBallPos.Y - BlackBallTexture.Height / 2), BlackBallTexture.Width, BlackBallTexture.Height);
        */
        /*
        WhiteBallRectangle; = new Rectangle((int)(playerPos.X - WhiteBallTexture.Width / 2),
        (int)(playerPos.Y - WhiteBallTexture.Height / 2), WhiteBallTexture.Width, WhiteBallTexture.Height);
        BlackBallRectangle; = new Rectangle ((int)(BlackBallPos.X - BlackBallTexture.Width / 2),
        (int)(BlackBallPos.Y - BlackBallTexture.Height / 2), BlackBallTexture.Width, BlackBallTexture.Height);
         */

    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        //mainPlayer.Update();

        prevKeyboardState = Keyboard.GetState();
        keyboardState = Keyboard.GetState();

        if (canMove)
        {
            if (keyboardState.IsKeyDown(Keys.Up) && prevKeyboardState.IsKeyDown(Keys.Up))
                WhiteBallRectangle.Y = WhiteBallRectangle.Y - 1;
           //playerPos -= new Vector2(0, 4);
            else if (keyboardState.IsKeyDown(Keys.Left) && prevKeyboardState.IsKeyDown(Keys.Left))
                WhiteBallRectangle.X = WhiteBallRectangle.X - 1;
            //playerPos -= new Vector2(4, 0);
            else if (keyboardState.IsKeyDown(Keys.Down) && prevKeyboardState.IsKeyDown(Keys.Down))
                WhiteBallRectangle.Y = WhiteBallRectangle.Y + 1;
            //playerPos += new Vector2(0, 4);
            else if (keyboardState.IsKeyDown(Keys.Right) && prevKeyboardState.IsKeyDown(Keys.Right))
                WhiteBallRectangle.X = WhiteBallRectangle.X + 1;
               //playerPos += new Vector2(4, 0);

                if (keyboardState.IsKeyDown(Keys.Up) && keyboardState.IsKeyDown(Keys.Left))
                {

                    WhiteBallRectangle.X = WhiteBallRectangle.X -  1;
                    WhiteBallRectangle.Y = WhiteBallRectangle.Y - 1;
                    //playerPos -= new Vector2(4, 4);
                }

                else if (keyboardState.IsKeyDown(Keys.Up) && keyboardState.IsKeyDown(Keys.Right))
                {
                    WhiteBallRectangle.Y = WhiteBallRectangle.Y - 1;
                    WhiteBallRectangle.X = WhiteBallRectangle.X + 1;
                    //playerPos -= new Vector2(0, 4);
                    //playerPos += new Vector2(4, 0);
                }
                else if (keyboardState.IsKeyDown(Keys.Down) && keyboardState.IsKeyDown(Keys.Left))
                {
                    WhiteBallRectangle.Y = WhiteBallRectangle.Y + 1;
                    WhiteBallRectangle.X = WhiteBallRectangle.X - 1;
                    //playerPos += new Vector2(0, 4);
                    //playerPos -= new Vector2(4, 0);
                }
                else if (keyboardState.IsKeyDown(Keys.Down) && keyboardState.IsKeyDown(Keys.Right))
                {
                    WhiteBallRectangle.Y = WhiteBallRectangle.Y + 1;
                    WhiteBallRectangle.X = WhiteBallRectangle.X + 1;
                }
                    //playerPos += new Vector2(4, 4);


        }


        CheckBounds();

        //Collision
       // Rectangle WhiteBallRectangle = new Rectangle((int)playerPos.X, (int)playerPos.Y, 10, 100);
        //Rectangle BlackBallRectangle = new Rectangle((int)playerPos.X, (int)playerPos.Y, 10, 100);
        if (WhiteBallRectangle.Intersects(BlueBallRectangle))
        {
            score = score + 20;
        }
        if (WhiteBallRectangle.Intersects(GreenBallRectangle))
        {

            score = score + 10;
        }
        if (WhiteBallRectangle.Intersects(OrangeBallRectangle))
        {

            score = score + 40;
        }
        if (WhiteBallRectangle.Intersects(PinkBallRectangle))
        {

            score = score + 25;
        }
        if (WhiteBallRectangle.Intersects(RedBallRectangle))
        {

            score = score + 10;
        }
        if (WhiteBallRectangle.Intersects(YellowBallRectangle))
        {

            score = score + 50;
        }
        if (WhiteBallRectangle.Intersects(BlackBallRectangle))
        {
            Exit();
        }
        if (WhiteBallRectangle.Intersects(BlackBallRectangle2))
        {
            Exit();
        }


        base.Update(gameTime);
    }

    private void CheckBounds()
    {
        if (WhiteBallRectangle.Y <= 0)
        {
            WhiteBallRectangle.Y = 1;
            canMove = false;
        }
        else if (WhiteBallRectangle.Y >= 452)
        {
            WhiteBallRectangle.Y = 451;
            canMove = false;
        }
        else
            canMove = true;

        if (WhiteBallRectangle.X <= 0)
        {
            WhiteBallRectangle.X = 1;
            canMove = false;
        }
        else if (playerPos.X >= 772)
        {
            WhiteBallRectangle.X = 771;
            canMove = false;
        }
        else
            canMove = true;
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        Vector2 text = new Vector2(10, 0);
        spriteBatch.Begin();


        spriteBatch.Draw(BackgroundTexture, position, Color.White);
        spriteBatch.DrawString(TitleFont, "Pick Up The Crew", text, Color.Black);
        spriteBatch.DrawString(TitleFont, "Score: " + score, scorePos, Color.Black);

       // spriteBatch.Draw(WhiteBallTexture, playerPos, null, Color.White, 0.0f, new Vector2(0, 0),
        //0.3f, SpriteEffects.None, 0.0f);
        //mainPlayer.Draw(spriteBatch);

        spriteBatch.Draw(WhiteBallTexture, WhiteBallRectangle, Color.White);
        spriteBatch.Draw(BlackBallTexture, BlackBallRectangle, Color.White);
        spriteBatch.Draw(BlackBallTexture2, BlackBallRectangle2, Color.White);
        spriteBatch.Draw(BlueBallTexture, BlueBallRectangle, Color.White);
        spriteBatch.Draw(GreenBallTexture, GreenBallRectangle, Color.White);
        spriteBatch.Draw(OrangeBallTexture, OrangeBallRectangle, Color.White);
        spriteBatch.Draw(PinkBallTexture, PinkBallRectangle, Color.White);
        spriteBatch.Draw(RedBallTexture, RedBallRectangle, Color.White);
        spriteBatch.Draw(YellowBallTexture, YellowBallRectangle, Color.White);

        /*spriteBatch.Draw(BlueBallTexture, new Vector2(500, (380 + (BlueBallTexture.Height / 2))), null, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
        spriteBatch.Draw(GreenBallTexture, new Vector2(230, (180 + (GreenBallTexture.Height / 2))), null, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
        spriteBatch.Draw(OrangeBallTexture, new Vector2(700, (200 + (OrangeBallTexture.Height / 2))), null, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
        spriteBatch.Draw(PinkBallTexture, new Vector2(600, (20 + (PinkBallTexture.Height / 2))), null, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
        spriteBatch.Draw(BlackBallTexture, new Vector2(100, (80 + (BlackBallTexture.Height / 2))), nul`enter code here`l, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
        spriteBatch.Draw(BlackBallTexture, new Vector2(300, (100 + (BlackBallTexture.Height / 2))), null, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
        spriteBatch.Draw(BlackBallTexture, new Vector2(400, (400 + (BlackBallTexture.Height / 2))), null, Color.White, 0.0f, new Vector2(0, 0),
        0.3f, SpriteEffects.None, 0.0f);
         */
        if (WhiteBallRectangle.Intersects(BlueBallRectangle))
        {
            spriteBatch.DrawString(TitleFont, "You Rescued Liuetenant Sky for 20 points!", saved, Color.Black);
        }
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
名称空间选取符
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
雪碧峰;
私有向量2 playerPos=Vector2.0;
向量2 BlackBallPos;
矢量2位置、速度;
向量2;
矢量2保存;
私有键盘状态键盘状态;
私有键盘状态prevKeyboardState;
private bool canMove=true;
智力得分;
//背景、玩家和鲨鱼的纹理。
纹理2d黑球纹理;
纹理2d黑球纹理2;
纹理2d蓝球纹理;
纹理2d绿球纹理;
纹理2d橙黄球纹理;
纹理2d粉球纹理;
纹理2D红球纹理;
纹理2D白球纹理;
纹理2D黄球纹理;
矩形黑球矩形;
矩形黑球矩形2;
矩形;
矩形、绿色、长方形;
长方形桔黄色小球长方形;
矩形针球矩形;
矩形红球矩形;
长方形;白球长方形;
矩形黄球矩形;
精灵主播放器;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
得分=0;
playerPos=new Vector2(this.GraphicsDevice.Viewport.Width/2,
this.GraphicsDevice.Viewport.Height*0.25f);
BlackBallPos=新矢量2(this.graphicsdevelope.Viewport.Width/2,
this.GraphicsDevice.Viewport.Height*0.75f);
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
私有纹理2D背景纹理;
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(Gra`enter code here`phicsDevice);
TitleFont=内容加载(“TitleFont”);
WhiteBallTexture=Content.Load(“白球”);
//mainPlayer=新精灵(Content.Load(“白球”)、新矩形((int)(playerPos.X-WhiteBallTexture.Width/2),
//(int)(playerPos.Y-WhiteBallTexture.Height/2)、WhiteBallTexture.Width、WhiteBallTexture.Height);
BackgroundTexture=Content.Load(“背景”);
BlackBallTexture=Content.Load(“BlackBall”);
BlackBallTexture2=Content.Load(“BlackBall”);
BlueBallTexture=Content.Load(“BlueBall”);
GreenBall纹理=Content.Load(“GreenBall”);
OrangeBallTexture=Content.Load(“OrangeBall”);
PinkBallTexture=Content.Load(“PinkBall”);
RedBallTexture=Content.Load(“RedBall”);
YellowBallTexture=Content.Load(“YellowBall”);
WhiteBallRectangle=新矩形(100,100,25,25);
BlackBallRectangle=新矩形(150,300,25,25);
BlackBallRectangle2=新矩形(500、400、25、25);
BlueBallRectangle=新矩形(500、150、25、25);
GreenBallRectangle=新矩形(100500,25,25);
OrangeBallRectangle=新矩形(180、200、25、25);
PinkBallRectangle=新矩形(260,260,25,25);
RedBallRectangle=新矩形(300、450、25、25);
YellowBallRectangle=新矩形(550,300,25,25);
得分位置X=575;
得分位置Y=450;
保存。X=0;
保存。Y=50;
/*
WhiteBallRectangle=新矩形((int)(playerPos.X-WhiteBallTexture.Width/2),
(int)(playerPos.Y-WhiteBallTexture.Height/2)、WhiteBallTexture.Width、WhiteBallTexture.Height);
BlackBallRectangle=新矩形((int)(BlackBallPos.X-BlackBallTexture.Width/2),
(int)(BlackBallPos.Y-BlackBallTexture.Height/2)、BlackBallTexture.Width、BlackBallTexture.Height);
*/
/*
WhiteBallRectangle;=新矩形((int)(playerPos.X-WhiteBallTexture.Width/2),
(int)(playerPos.Y-WhiteBallTexture.Height/2)、WhiteBallTexture.Width、WhiteBallTexture.Height);
BlackBallRectangle;=新矩形((int)(BlackBallPos.X-BlackBallTexture.Width/2),
(int)(BlackBallPos.Y-BlackBallTexture.Height/2)、BlackBallTexture.Width、BlackBallTexture.Height);
*/
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//mainPlayer.Update();
prevKeyboardState=Keyboard.GetState();
钥匙
var x = WhiteBallRectangle.X - BlackBallRectangle.X;
var y = WhiteBallRectangle.Y - BlackBallRectangle.Y;
var distance = (decimal)Math.Sqrt((Math.Pow(x, 2) + Math.Pow(y, 2));
if(distance < f)
{
  // do something
}
var x = WhiteBallRectangle.X - BlackBallRectangle.X;
var y = WhiteBallRectangle.Y - BlackBallRectangle.Y;
var direction = new Vector2(x, y);
direction.Normalize();
var x = WhiteBallRectangle.X - BlackBallRectangle.X;
var y = WhiteBallRectangle.Y - BlackBallRectangle.Y;
var distance = (decimal)Math.Sqrt((Math.Pow(x, 2) + Math.Pow(y, 2));
var direction = new Vector2(x, y); // from Black to White
direction.Normalize();
if(distance < f)
{
  // do something
}
if the distance is less then "far"
  go slowly
if the distance is less then "medium"
  go fast
if the distance is less then "near" OR greater then "far"
  stop
decimal far = 50.0m, medium = 30.0m, near = 10.0m; // tweak this
decimal slow = 10.0m, fast = 20.0m; // tweak this
var x = WhiteBallRectangle.X - BlackBallRectangle.X;
var y = WhiteBallRectangle.Y - BlackBallRectangle.Y;
var distance = (decimal)Math.Sqrt((x * x) + (y * y));
var direction = new Vector2(x, y); // from Black to White
direction.Normalize();
Vector2 move = new Vector2(0, 0);
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
if(distance < far)
{
   move = direction * slow * delta; // delta timing, explained at *
} else if (distance < medium && distance > near)
{
  move = direction * fast * delta; // delta timing, explained at *
}

BlackBallRectangle.X += move.X;
BlackBallRectangle.Y += move.Y;