XNA开发实用教程——键盘输入事件

来源:互联网 发布:amd怎么优化显卡性能 编辑:程序博客网 时间:2024/06/07 03:46

 XNA开发实用教程——键盘输入事件三峡大学土木水电学院肖泽云本教程的主要目的是让你看完后,真正体会一下什么是XNA?XNA中主要包括哪些部分?相信你自己,在看完整个教程后,你也能设计自己的三维场景!祝你成功!
六、键盘输入事件 [ModelWindowsGame]
1、首先在全局变量中定义:
KeyboardState currentKeyboardState = new KeyboardState();  //定义键盘状态
2、定义函数HandleUpdata(),用于监听键盘事件,代码如下:
void HandleUpdata()
{
currentKeyboardState = Keyboard.GetState();
if (currentKeyboardState.IsKeyDown(Keys.Escape)) this.Exit(); //如果按ESC键,则退出程序
}
3、在Draw()函数或Update()函数中调用这个监听函数HandleUpdata()即可。在程序运行时,如果按了ESC键,将退出程序。
4、下面通过键盘事件来移动模型,继承前面例子的代码。将监听函数HandleUpdata()更加丰富,如下:
void HandleUpdata()
            {
            currentKeyboardState = Keyboard.GetState();
            if (currentKeyboardState.IsKeyDown(Keys.Escape)) this.Exit(); //如果按ESC键,则退出程序
            //设置模型的位置
            if (currentKeyboardState.IsKeyDown(Keys.W)) modelpositon.Y += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.S)) modelpositon.Y -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.A)) modelpositon.X += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.D)) modelpositon.X -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Q)) modelpositon.Q += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Z)) modelpositon.Z -= 2.0f;
            //设置摄像机位置
            if (currentKeyboardState.IsKeyDown(Keys.Up)) camerapositon.Y += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Down)) camerapositon.Y -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Left)) camerapositon.X += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Right)) camerapositon.X -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.E)) camerapositon.Z += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.C)) camerapositon.Z -= 2.0f;

            }
其显示结果如图所示:

该程序所以得代码如下:
#region Using Statements  //引用
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace ModelWindowsGame
    {
    public class Game1 : Microsoft.Xna.Framework.Game //继承Game类
        {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Model myModel;     //定义模型
        Vector3 modelpositon = Vector3.Zero;            //定义模型的位置
        Vector3 camerapositon = new Vector3(0, 30, 100);   //定义摄像机的位置
        Vector3 cameraLookAt = Vector3.Zero;         //定义摄像机目标的位置
        Matrix cameraprojectionMatrix;             //定义摄像机投影矩阵
        Matrix cameraviewMatrix;                   //定义摄像机视图矩阵
        KeyboardState currentKeyboardState = new KeyboardState();  //定义键盘状态

        public Game1()
            {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            }
        protected override void Initialize()  //初始化
            {
            base.Initialize();
            }
        protected override void LoadContent()  //导入目录,每次游戏启动时都会启动
            {
            // 创建一个精灵,用于绘制图片
            spriteBatch = new SpriteBatch(GraphicsDevice);
            myModel = Content.Load<Model>("MyModel01");
            }
        protected override void UnloadContent()  //卸载目录
            {
            // TODO: Unload any non ContentManager content here
            }
        protected override void Update(GameTime gameTime)  /// 更新。用于检测碰撞、输入等
            {
            // 设置游戏结束事件
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            //添加更新的对象代码
            HandleUpdata();
            base.Update(gameTime);
            }
        protected override void Draw(GameTime gameTime)  //当绘制时被调用
            {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            // 添加绘图代码
            CamaraUpdata();
            DrawModel(myModel, modelpositon);
            base.Draw(gameTime);
            }

        void CamaraUpdata()
            {
            cameraviewMatrix = Matrix.CreateLookAt(camerapositon, cameraLookAt, Vector3.Up);       //设置摄像机视图矩阵为从摄像机到摄像机目标,向上
            cameraprojectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(60.0f),
                graphics.GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f);   //设置摄像机投影矩阵,用于控制摄像机的视角角度、视距等,如60.0f即为视角角度,1.0f和1000.0f分别为可见的最短距离和最远距离
            }

        void DrawModel(Model model, Vector3 modelPositon)
            {
            //modelpositon.X = modelpositon.X+ 1; 设置模型的X方向位置随着时间增加
            foreach (ModelMesh mesh in model.Meshes)  //每个模型model中有很多网格mesh。
                {
                foreach (BasicEffect effect in mesh.Effects)
                    {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    if (mesh.Name == "Box01")
                        effect.World = Matrix.CreateTranslation(modelpositon); //如果mesh的名称叫Box01,则设置它的世界坐标为前面定义的modelpositon矩阵,由于我们前面建模型的时候绘制的就是一个box,而且该box的名称就是Box01。                    
                    effect.Projection = cameraprojectionMatrix;   //设置它的投影为摄像机投影矩阵
                    effect.View = cameraviewMatrix;    //设置它的视图为摄像机视图
                    /*此外,还可以设置是否有雾等等,如下:
                    effect.FogEnabled = true;
                    effect.FogStart = 200;
                    effect.FogEnd = 500;
                    effect.FogColor = Color.CornflowerBlue.ToVector3();
                    */
                    }
                mesh.Draw();    //绘制模型,即显示模型        
                }
            }
        void HandleUpdata()
            {
            currentKeyboardState = Keyboard.GetState();
            if (currentKeyboardState.IsKeyDown(Keys.Escape)) this.Exit(); //如果按ESC键,则退出程序
            //设置模型的位置
            if (currentKeyboardState.IsKeyDown(Keys.W)) modelpositon.Y += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.S)) modelpositon.Y -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.A)) modelpositon.X += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.D)) modelpositon.X -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Q)) modelpositon.Z+= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Z)) modelpositon.Z -= 2.0f;
            //设置摄像机位置
            if (currentKeyboardState.IsKeyDown(Keys.Up)) camerapositon.Y += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Down)) camerapositon.Y -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Left)) camerapositon.X += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Right)) camerapositon.X -= 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.E)) camerapositon.Z += 2.0f;
            if (currentKeyboardState.IsKeyDown(Keys.C)) camerapositon.Z -= 2.0f;

            }
        }
    }

原创粉丝点击