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

本文介绍如何使用XNA实现键盘输入事件,包括监听键盘按键、响应按键操作来控制3D模型移动及摄像机位置变化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}
}
}
<!--v:3.2-->

<!--E 文章--><!--S 翻页-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值