梦想成真 XNA (5) - 通过键盘、鼠标、手柄控制精灵
梦想成真 XNA (5) - 通过键盘、鼠标、手柄控制精灵
作者:webabcd
介绍
XNA: 控制精灵的移动
- 键盘控制
- 鼠标控制
- 手柄控制
示例
1、通过键盘控制精灵移动的 Demo(按键盘 H 键加载此 Demo)
Component/Control/KeyboardControl.cs
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 XNA.Component.Control
{
public class KeyboardControl : Microsoft.Xna.Framework.DrawableGameComponent
{
// 精灵绘制器
SpriteBatch _spriteBatch;
// 精灵对象
Texture2D _sprite;
// 精灵对象的位置
Vector2 _spritePosition;
// 精灵对象的速度(每帧移动的像素数)
int _spriteSpeed = 6;
public KeyboardControl(Game game)
: base(game)
{
}
public override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(Game.GraphicsDevice);
_sprite = Game.Content.Load<Texture2D>("Image/Son");
}
public override void Update(GameTime gameTime)
{
/*
* KeyboardState - 键盘状态
* KeyboardState Keyboard.GetState() - 返回当前的 KeyboardState 对象
* Keys[] KeyboardState.GetPressedKeys() - 返回当前被按下的键的数组
* bool KeyboardState.IsKeyDown(Keys key) - 指定的键当前是否处于按下的状态
* bool KeyboardState.IsKeyUp(Keys key) - 指定的键当前是否处于释放的状态
*/
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Left) && _spritePosition.X >= 0)
_spritePosition.X -= _spriteSpeed;
if (keyboardState.IsKeyDown(Keys.Right) && _spritePosition.X <= Game.Window.ClientBounds.Width - _sprite.Width)
_spritePosition.X += _spriteSpeed;
if (keyboardState.IsKeyDown(Keys.Up) && _spritePosition.Y >= 0)
_spritePosition.Y -= _spriteSpeed;
if (keyboardState.IsKeyDown(Keys.Down) && _spritePosition.Y <= Game.Window.ClientBounds.Height - _sprite.Height)
_spritePosition.Y += _spriteSpeed;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Game.GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
// 在指定的位置上绘制精灵
_spriteBatch.Draw(_sprite, _spritePosition, Color.White);
_spriteBatch.End();
base.Update(gameTime);
}
}
}
2、通过鼠标控制精灵移动的 Demo(按键盘 I 键加载此 Demo)
Component/Control/MouseControl.cs
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 XNA.Component.Control
{
public class MouseControl : Microsoft.Xna.Framework.DrawableGameComponent
{
// 精灵绘制器
SpriteBatch _spriteBatch;
// 精灵对象
Texture2D _sprite;
// 精灵对象的位置
Vector2 _spritePosition;
// 文字精灵
SpriteFont _spriteFont;
// 文字精灵所显示的文字内容
string _message;
public MouseControl(Game game)
: base(game)
{
}
public override void Initialize()
{
// 显示鼠标指针(默认值为 false)
Game.IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(Game.GraphicsDevice);
_sprite = Game.Content.Load<Texture2D>("Image/Son");
_spriteFont = Game.Content.Load<SpriteFont>("SpriteFont/Message");
}
public override void Update(GameTime gameTime)
{
/*
* MouseState - 鼠标状态
* MouseState Mouse.GetState() - 返回当前的 MouseState 对象
* int MouseState.X - 以游戏窗口左上角为原点,鼠标光标相对于原点 X 轴方向的位置
* int MouseState.Y - 以游戏窗口左上角为原点,鼠标光标相对于原点 Y 轴方向的位置
* int MouseState.ScrollWheelValue - 游戏开始后,鼠标滑轮滚动的累加值
* ButtonState MouseState.LeftButton - 鼠标的左键状态
* ButtonState MouseState.RightButton - 鼠标的右键状态
* ButtonState MouseState.MiddleButton - 鼠标的中键状态
* ButtonState MouseState.XButton1 - 鼠标的附加键 1 的状态(我的一款无线鼠标上有这个键)
* ButtonState MouseState.XButton2 - 鼠标的附加键 2 的状态(我的一款无线鼠标上有这个键)
*
* ButtonState - 按键状态(枚举值)
* ButtonState.Pressed - 按下状态
* ButtonState.Released - 释放状态
*/
MouseState mouseState = Mouse.GetState();
_spritePosition = new Vector2(mouseState.X, mouseState.Y);
_message = string.Format("X:{0}, Y:{1}, ScrollWheelValue:{2}, LeftButton:{3}, RightButton:{4}, MiddleButton:{5}",
mouseState.X, mouseState.Y, mouseState.ScrollWheelValue, mouseState.LeftButton, mouseState.RightButton, mouseState.MiddleButton);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Game.GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
// 在指定的位置上绘制精灵
_spriteBatch.Draw(_sprite, _spritePosition, Color.White);
// 显示鼠标状态信息
_spriteBatch.DrawString(_spriteFont, _message, Vector2.Zero, Color.White);
_spriteBatch.End();
base.Update(gameTime);
}
}
}
3、通过手柄控制精灵移动的 Demo(按键盘 J 键加载此 Demo)
Component/Control/GamePadControl.cs
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 XNA.Component.Control
{
public class GamePadControl : Microsoft.Xna.Framework.DrawableGameComponent
{
// 精灵绘制器
SpriteBatch _spriteBatch;
// 精灵对象
Texture2D _sprite;
// 精灵对象的位置
Vector2 _spritePosition = Vector2.Zero;
public GamePadControl(Game game)
: base(game)
{
}
public override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(Game.GraphicsDevice);
_sprite = Game.Content.Load<Texture2D>("Image/Son");
}
public override void Update(GameTime gameTime)
{
/*
* GamePadState - 游戏手柄状态
* GamePadState GamePad.GetState(PlayerIndex playerIndex) - 返回当前的 GamePadState 对象,需要指定是哪个手柄(目前一共支持 4 个手柄)
*
* 没设备,就不研究了,等以后有机会吧
*/
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Game.GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
// 在指定的位置上绘制精灵
_spriteBatch.Draw(_sprite, _spritePosition, Color.White);
_spriteBatch.End();
base.Update(gameTime);
}
}
}
OK
[源码下载]
浙公网安备 33010602011771号