XNA实践鼠标篇--根据点击坐标移动图片(四)下

 

 /// <summary>

    /// This is the main type for your game

    /// </summary>

    public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

        /// <summary>

        /// 图片的位置

        /// </summary>

        private Vector2 picPosition;

        /// <summary>

        /// 当前鼠标点击的位置

        /// </summary>

        private Vector2 CurrentPosition;



        /// <summary>

        /// 移动因子

        /// </summary>

        private Vector2 MovePix;

        /// <summary>

        /// 定义2D的纹理图片

        /// </summary>

        private Texture2D texture;

        public Game1()

        {

            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";

            IsMouseVisible = true;//显示鼠标

            //初始化鼠标点击游戏界面的坐标

            picPosition = new Vector2();

            MovePix = new Vector2(1.0f, 1.0f);//移动一个像素

        }



        /// <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()

        {

            // TODO: Add your initialization logic here



            base.Initialize();

        }



        /// <summary>

        /// LoadContent will be called once per game and is the place to load

        /// all of your content.

        /// </summary>

        protected override void LoadContent()

        {

            // Create a new SpriteBatch, which can be used to draw textures.

            spriteBatch = new SpriteBatch(GraphicsDevice);

            texture = Texture2D.FromFile(graphics.GraphicsDevice, "test.jpg");

            // TODO: use this.Content to load your game content here

        }



        /// <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)

        {

            // Allows the game to exit

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                this.Exit();



            MouseState mouseState = Mouse.GetState();

            if (mouseState.LeftButton == ButtonState.Pressed)

            {

                Viewport vp = graphics.GraphicsDevice.Viewport;//获取视窗

                if (mouseState.X < vp.X || mouseState.X > vp.X + vp.Width || mouseState.Y < vp.Y || mouseState.Y > vp.Y + vp.Height)

                {

                    //点击坐标不在窗体内就不管了

                }

                else

                {

                    if (CurrentPosition.X != mouseState.X || CurrentPosition.Y != mouseState.Y)

                    {

                        CurrentPosition.X = mouseState.X;

                        CurrentPosition.Y = mouseState.Y;

                    }

                }

            }

            Mover();



            // TODO: Add your update logic here



            base.Update(gameTime);

        }



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



            // TODO: Add your drawing code here

            spriteBatch.Begin();

            spriteBatch.Draw(texture, picPosition, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);

        }

        /// <summary>

        /// 开始移动

        /// </summary>

        void Mover()

        {

            Vector2 v = picPosition - CurrentPosition;//向量相减

            if (v.X > 0)

                picPosition.X -= MovePix.X;//向左移动

            else if (v.X + texture.Width  < 0)

                picPosition.X += MovePix.X;//向右移动

            if (v.Y > 0)

                picPosition.Y -= MovePix.Y;//向上移动

            else if (v.Y + texture.Height < 0)

                picPosition.Y += MovePix.Y;//向一移动

        }

    }

 

图片还是使用上次的2维图片,现在在非图片覆盖区按住鼠标的左键并移动鼠标看看,图片是不是跟着鼠标的方向慢慢移动了,看到这里,大家会想到什么了么?

对了,就是游戏中的角色对怪的位置跟踪,但在这张图上只体现了F(G+H),因为窗体没有任何的障碍物。

你可能感兴趣的:(图片)