/// <summary> /// 封装用户输入 /// </summary> public class Input { private Input() { } /// <summary> /// 用户输入实例 /// </summary> private static Input _instance; /// <summary> /// 获取封装用户输入的唯一实例 /// </summary> public static Input Instance { get { if (_instance == null) { _instance = new Input(); } return _instance; } } /// <summary> /// 绑定到窗体 /// </summary> /// <param name="window">相应用户输入的窗体</param> public void BindingToForm(Form window) { KeyBoard = new KeyBoard(window); } /// <summary> /// 获取或设置键盘输入状态 /// </summary> public KeyBoard KeyBoard { get; private set; } /// <summary> /// 更新输入状态 /// </summary> public void Update() { KeyBoard.Process(); } }
/// <summary> /// 封装用户输入 /// </summary> public class Input { /// <summary> /// 绑定到窗体 /// </summary> /// <param name="window">相应用户输入的窗体</param> public void BindingToForm(Form window) { KeyBoard = new KeyBoard(window); } /// <summary> /// 获取或设置键盘输入状态 /// </summary> public KeyBoard KeyBoard { get; private set; } /// <summary> /// 更新输入状态 /// </summary> public void Update() { KeyBoard.Process(); } }
class SpaceShip { Input _input; public SpaceShip(Input input) { this._input = input; } public Vector MoveDirection { get; set; } private Vector _posation; public void Move() { _posation = _posation + MoveDirection; } public void Update() { Vector amount = Vector.Zero; if (_input.KeyBoard.IsKeyHeld(Keys.Left)) { amount.X = -1; } if (_input.KeyBoard.IsKeyHeld(Keys.Right)) { amount.X = 1; } if (_input.KeyBoard.IsKeyHeld(Keys.Up)) { amount.Y = -1; } if (_input.KeyBoard.IsKeyHeld(Keys.Down)) { amount.Y = 1; } MoveDirection = amount; } }
原文地址:http://blog.csdn.net/u011453312/article/details/30040961