XNA游戏:各种输入测试 上

发布时间:2020-06-29 06:23:34 作者:linzheng
来源:网络 阅读:381

测试XNA游戏中键盘输入,触控输入,按钮输入

Game1.cs

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Microsoft.Xna.Framework;  
  5. using Microsoft.Xna.Framework.Audio;  
  6. using Microsoft.Xna.Framework.Content;  
  7. using Microsoft.Xna.Framework.GamerServices;  
  8. using Microsoft.Xna.Framework.Graphics;  
  9. using Microsoft.Xna.Framework.Input;  
  10. using Microsoft.Xna.Framework.Input.Touch;  
  11. using Microsoft.Xna.Framework.Media;  
  12.  
  13. using InputHandlerDemo.Inputs;  
  14.  
  15. namespace InputHandlerDemo  
  16. {  
  17.     public class Game1 : Microsoft.Xna.Framework.Game  
  18.     {  
  19.         GraphicsDeviceManager graphics;  
  20.         SpriteBatch spriteBatch;  
  21.  
  22.         SpriteFont font;  
  23.         Texture2D square;  
  24.         string action = "";  
  25.  
  26.         GameInput gameInput;//游戏输入管理类  
  27.         TouchIndicatorCollection touchIndicators;  
  28.  
  29.         Rectangle JumpRectangle = new Rectangle(0, 0, 480, 100);  
  30.         Rectangle UpRectangle = new Rectangle(0, 150, 480, 100);  
  31.         Rectangle PauseRectangle = new Rectangle(0, 500, 200, 100);  
  32.         //退出矩形  
  33.         Rectangle ExitRectangle = new Rectangle(220, 500, 200, 100);  
  34.  
  35.  
  36.         public Game1()  
  37.         {  
  38.             graphics = new GraphicsDeviceManager(this);  
  39.             Content.RootDirectory = "Content";  
  40.             TargetElapsedTime = TimeSpan.FromTicks(333333);  
  41.             //设置高度和宽度  
  42.             graphics.PreferredBackBufferWidth = 480;  
  43.             graphics.PreferredBackBufferHeight = 800;    
  44.  
  45.         }  
  46.  
  47.         protected override void Initialize()  
  48.         {  
  49.             //初始化游戏输入对象  
  50.             gameInput = new GameInput();  
  51.             touchIndicators = new TouchIndicatorCollection();  
  52.  
  53.             AddInputs();  
  54.  
  55.             base.Initialize();  
  56.         }  
  57.         /// <summary> 
  58.         /// 添加各种输入方式  
  59.         /// </summary> 
  60.         private void AddInputs()  
  61.         {  
  62.             //游戏按钮  
  63.  
  64.             // Add keyboard, gamepad and touch inputs for Jump  
  65.             gameInput.AddKeyboardInput(Actions.Jump, Keys.A, true);//A键为跳  
  66.             gameInput.AddKeyboardInput(Actions.Jump, Keys.Space, false);//Space键为跳  
  67.             gameInput.AddTouchTapInput(Actions.Jump, JumpRectangle, false);//跳矩形区域为跳  
  68.             gameInput.AddTouchSlideInput(Actions.Jump, Input.Direction.Right, 5.0f);//右滑动为跳  
  69.  
  70.             // Add keyboard, gamepad and touch inputs for Pause  
  71.             gameInput.AddGamePadInput(Actions.Pause, Buttons.Start, true);  
  72.             gameInput.AddKeyboardInput(Actions.Pause, Keys.P, true);  
  73.             gameInput.AddTouchTapInput(Actions.Pause, PauseRectangle, true);  
  74.             gameInput.AddAccelerometerInput(Actions.Pause,  
  75.                                             Input.Direction.Down,  
  76.                                             0.10f);  
  77.  
  78.             // Add keyboard, gamepad and touch inputs for Up  
  79.             gameInput.AddGamePadInput(Actions.Up, Buttons.RightThumbstickUp, false);  
  80.             gameInput.AddGamePadInput(Actions.Up, Buttons.LeftThumbstickUp, false);  
  81.             gameInput.AddGamePadInput(Actions.Up, Buttons.DPadUp, false);  
  82.             gameInput.AddKeyboardInput(Actions.Up, Keys.Up, false);  
  83.             gameInput.AddKeyboardInput(Actions.Up, Keys.W, true);  
  84.             gameInput.AddTouchTapInput(Actions.Up, UpRectangle, true);  
  85.             gameInput.AddTouchSlideInput(Actions.Up, Input.Direction.Up, 5.0f);  
  86.             gameInput.AddAccelerometerInput(Actions.Up,  
  87.                                             Input.Direction.Up,  
  88.                                             0.10f);  
  89.  
  90.             // Add keyboard, gamepad and touch inputs for Exit  
  91.             gameInput.AddGamePadInput(Actions.Exit, Buttons.Back, false);  
  92.             gameInput.AddKeyboardInput(Actions.Exit, Keys.Escape, false);  
  93.             gameInput.AddTouchTapInput(Actions.Exit, ExitRectangle, true);  
  94.  
  95.             // Add some Gestures too, just to show them off?  
  96.             gameInput.AddTouchGestureInput(Actions.Jump,  
  97.                                            GestureType.VerticalDrag,  
  98.                                            JumpRectangle);  
  99.             gameInput.AddTouchGestureInput(Actions.Pause,  
  100.                                            GestureType.Hold,  
  101.                                            PauseRectangle);  
  102.         }  
  103.  
  104.         protected override void LoadContent()  
  105.         {  
  106.             // Create a new SpriteBatch, which can be used to draw textures.  
  107.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  108.  
  109.             //加载内容  
  110.             font = Content.Load<SpriteFont>("Display");  
  111.             square = Content.Load<Texture2D>("Pixel");  
  112.  
  113.         }  
  114.  
  115.         protected override void UnloadContent()  
  116.         {  
  117.         }  
  118.  
  119.         protected override void Update(GameTime gameTime)  
  120.         {  
  121.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)  
  122.                 this.Exit();  
  123.  
  124.             //根据输入类型更新游戏界面  
  125.             gameInput.BeginUpdate();  
  126.  
  127.             //点击退出矩形,退出程序  
  128.             if (gameInput.IsPressed(Actions.Exit, PlayerIndex.One))  
  129.                 this.Exit();  
  130.  
  131.             if (gameInput.IsPressed(Actions.Jump, PlayerIndex.One))  
  132.                 action = Actions.Jump;  
  133.     
  134.             if (gameInput.IsPressed(Actions.Pause, PlayerIndex.One))  
  135.                 action = Actions.Pause;  
  136.  
  137.             if (gameInput.IsPressed(Actions.Up, PlayerIndex.One))  
  138.                 action = Actions.Up;  
  139.  
  140.             touchIndicators.Update(gameTime, Content);  
  141.  
  142.             gameInput.EndUpdate();  
  143.  
  144.             base.Update(gameTime);  
  145.         }  
  146.  
  147.         protected override void Draw(GameTime gameTime)  
  148.         {  
  149.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  150.  
  151.             //绘制游戏界面  
  152.             spriteBatch.Begin();  
  153.  
  154.             spriteBatch.Draw(square, UpRectangle, Color.Blue);  
  155.             spriteBatch.DrawString(font,  
  156.                                    "Up",  
  157.                                    new Vector2(UpRectangle.Left + 20, UpRectangle.Top + 20),  
  158.                                    Color.Black);  
  159.  
  160.             spriteBatch.Draw(square, JumpRectangle, Color.Yellow);  
  161.             spriteBatch.DrawString(font,  
  162.                                    "Jump",  
  163.                                    new Vector2(JumpRectangle.Left + 20, JumpRectangle.Top + 20),  
  164.                                    Color.Black);  
  165.  
  166.             spriteBatch.Draw(square, PauseRectangle, Color.Green);  
  167.             spriteBatch.DrawString(font,  
  168.                                    "Pause",  
  169.                                    new Vector2(PauseRectangle.Left + 20, PauseRectangle.Top + 20),  
  170.                                    Color.Black);  
  171.  
  172.             spriteBatch.Draw(square, ExitRectangle, Color.Red);  
  173.             spriteBatch.DrawString(font,  
  174.                                    "Exit",  
  175.                                    new Vector2(ExitRectangle.Left + 20, ExitRectangle.Top + 20),  
  176.                                    Color.Black);  
  177.  
  178.             spriteBatch.DrawString(font, action, new Vector2(100, 350), Color.White);  
  179.  
  180.             touchIndicators.Draw(spriteBatch);  
  181.  
  182.             spriteBatch.End();   
  183.  
  184.             base.Draw(gameTime);  
  185.         }  
  186.     }  

Action.cs 游戏操作的类型

 

  1. namespace InputHandlerDemo  
  2. {  
  3.     static class Actions  
  4.     {  
  5.         // 自定义的操作类型  
  6.         public const string Jump = "Jump";  
  7.         public const string Exit = "Exit";  
  8.         public const string Up = "Up";  
  9.         public const string Pause = "Pause";  
  10.     }  

下面是输入的操作的封装类

GameInput.cs

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.Xna.Framework;  
  4. using Microsoft.Xna.Framework.Input;  
  5. using Microsoft.Xna.Framework.Input.Touch;  
  6.  
  7.  
  8. namespace InputHandlerDemo.Inputs  
  9. {  
  10.     /// <summary> 
  11.     /// 游戏输入管理类  
  12.     /// </summary> 
  13.     class GameInput  
  14.     {  
  15.         //输入类型字典  
  16.         Dictionary<string, Input> Inputs = new Dictionary<string, Input>();  
  17.         //获取输入类型  
  18.         public Input GetInput(string theAction)  
  19.         {  
  20.             //如果没有改类型的输入操作则添加一个  
  21.             if (Inputs.ContainsKey(theAction) == false)  
  22.             {  
  23.                 Inputs.Add(theAction, new Input());  
  24.             }  
  25.  
  26.             return Inputs[theAction];  
  27.         }  
  28.  
  29.         public void BeginUpdate()  
  30.         {  
  31.             Input.BeginUpdate();  
  32.         }  
  33.  
  34.         public void EndUpdate()  
  35.         {  
  36.             Input.EndUpdate();  
  37.         }  
  38.  
  39.         public bool IsConnected(PlayerIndex thePlayer)  
  40.         {  
  41.             // If there never WAS a gamepad connected, just say the gamepad is STILL connected  
  42.             if (Input.GamepadConnectionState[thePlayer] == false)  
  43.                 return true;  
  44.  
  45.             return Input.IsConnected(thePlayer);  
  46.         }  
  47.  
  48.         public bool IsPressed(string theAction)  
  49.         {  
  50.             if (!Inputs.ContainsKey(theAction))  
  51.             {  
  52.                 return false;  
  53.             }  
  54.  
  55.             return Inputs[theAction].IsPressed(PlayerIndex.One);  
  56.         }  
  57.         /// <summary> 
  58.         /// 判断单击的状态  
  59.         /// </summary> 
  60.         /// <param name="theAction">动作</param> 
  61.         /// <param name="thePlayer">玩家</param> 
  62.         /// <returns></returns> 
  63.         public bool IsPressed(string theAction, PlayerIndex thePlayer)  
  64.         {  
  65.             if (Inputs.ContainsKey(theAction) == false)  
  66.             {  
  67.                 return false;  
  68.             }  
  69.  
  70.             return Inputs[theAction].IsPressed(thePlayer);  
  71.         }  
  72.  
  73.         public bool IsPressed(string theAction, PlayerIndex? thePlayer)  
  74.         {  
  75.             if (thePlayer == null)  
  76.             {  
  77.                 PlayerIndex theReturnedControllingPlayer;  
  78.                 return IsPressed(theAction, thePlayer, out theReturnedControllingPlayer);  
  79.             }  
  80.  
  81.             return IsPressed(theAction, (PlayerIndex)thePlayer);  
  82.         }  
  83.  
  84.         public bool IsPressed(string theAction, PlayerIndex? thePlayer, out PlayerIndex theControllingPlayer)  
  85.         {  
  86.             if (!Inputs.ContainsKey(theAction))  
  87.             {  
  88.                 theControllingPlayer = PlayerIndex.One;  
  89.                 return false;  
  90.             }  
  91.  
  92.             if (thePlayer == null)  
  93.             {  
  94.                 if (IsPressed(theAction, PlayerIndex.One))  
  95.                 {  
  96.                     theControllingPlayer = PlayerIndex.One;  
  97.                     return true;  
  98.                 }  
  99.  
  100.                 if (IsPressed(theAction, PlayerIndex.Two))  
  101.                 {  
  102.                     theControllingPlayer = PlayerIndex.Two;  
  103.                     return true;  
  104.                 }  
  105.  
  106.                 if (IsPressed(theAction, PlayerIndex.Three))  
  107.                 {  
  108.                     theControllingPlayer = PlayerIndex.Three;  
  109.                     return true;  
  110.                 }  
  111.  
  112.                 if (IsPressed(theAction, PlayerIndex.Four))  
  113.                 {  
  114.                     theControllingPlayer = PlayerIndex.Four;  
  115.                     return true;  
  116.                 }  
  117.  
  118.                 theControllingPlayer = PlayerIndex.One;  
  119.                 return false;  
  120.             }  
  121.  
  122.             theControllingPlayer = (PlayerIndex)thePlayer;  
  123.             return IsPressed(theAction, (PlayerIndex)thePlayer);  
  124.         }  
  125.  
  126.         public void AddGamePadInput(string theAction, Buttons theButton,  
  127.                             bool isReleasedPreviously)  
  128.         {  
  129.             GetInput(theAction).AddGamepadInput(theButton, isReleasedPreviously);  
  130.         }  
  131.  
  132.         public void AddTouchTapInput(string theAction, Rectangle theTouchArea,  
  133.                                      bool isReleasedPreviously)  
  134.         {  
  135.             GetInput(theAction).AddTouchTapInput(theTouchArea, isReleasedPreviously);  
  136.         }  
  137.  
  138.         public void AddTouchSlideInput(string theAction, Input.Direction theDirection,  
  139.                                        float slideDistance)  
  140.         {  
  141.             GetInput(theAction).AddTouchSlideInput(theDirection, slideDistance);  
  142.         }  
  143.  
  144.         public void AddKeyboardInput(string theAction, Keys theKey,  
  145.                                      bool isReleasedPreviously)  
  146.         {  
  147.             GetInput(theAction).AddKeyboardInput(theKey, isReleasedPreviously);  
  148.         }  
  149.  
  150.         public void AddTouchGestureInput(string theAction, GestureType theGesture,  
  151.                                          Rectangle theRectangle)  
  152.         {  
  153.             GetInput(theAction).AddTouchGesture(theGesture, theRectangle);  
  154.         }  
  155.  
  156.         public void AddAccelerometerInput(string theAction, Input.Direction theDirection,  
  157.                                           float tiltThreshold)  
  158.         {  
  159.             GetInput(theAction).AddAccelerometerInput(theDirection, tiltThreshold);  
  160.         }  
  161.  
  162.         public Vector2 CurrentGesturePosition(string theAction)  
  163.         {  
  164.             return GetInput(theAction).CurrentGesturePosition();  
  165.         }  
  166.  
  167.         public Vector2 CurrentGestureDelta(string theAction)  
  168.         {  
  169.             return GetInput(theAction).CurrentGestureDelta();  
  170.         }  
  171.  
  172.         public Vector2 CurrentGesturePosition2(string theAction)  
  173.         {  
  174.             return GetInput(theAction).CurrentGesturePosition2();  
  175.         }  
  176.  
  177.         public Vector2 CurrentGestureDelta2(string theAction)  
  178.         {  
  179.             return GetInput(theAction).CurrentGestureDelta2();  
  180.         }  
  181.  
  182.         public Point CurrentTouchPoint(string theAction)  
  183.         {  
  184.             Vector2? currentPosition = GetInput(theAction).CurrentTouchPosition();  
  185.             if (currentPosition == null)  
  186.             {  
  187.                 return new Point(-1, -1);  
  188.             }  
  189.  
  190.             return new Point((int)currentPosition.Value.X, (int)currentPosition.Value.Y);  
  191.         }  
  192.  
  193.         public Vector2 CurrentTouchPosition(string theAction)  
  194.         {  
  195.             Vector2? currentTouchPosition = GetInput(theAction).CurrentTouchPosition();  
  196.             if (currentTouchPosition == null)  
  197.             {  
  198.                 return new Vector2(-1, -1);  
  199.             }  
  200.  
  201.             return (Vector2)currentTouchPosition;  
  202.         }  
  203.  
  204.         public float CurrentGestureScaleChange(string theAction)  
  205.         {  
  206.             // Scaling is dependent on the Pinch gesture. If no input has been setup for   
  207.             // Pinch then just return 0 indicating no scale change has occurred.  
  208.             if (!GetInput(theAction).PinchGestureAvailable) return 0;  
  209.  
  210.  
  211.             // Get the current and previous locations of the two fingers  
  212.             Vector2 currentPositionFingerOne = CurrentGesturePosition(theAction);  
  213.             Vector2 previousPositionFingerOne 
  214.                 = CurrentGesturePosition(theAction) - CurrentGestureDelta(theAction);  
  215.             Vector2 currentPositionFingerTwo = CurrentGesturePosition2(theAction);  
  216.             Vector2 previousPositionFingerTwo 
  217.                 = CurrentGesturePosition2(theAction) - CurrentGestureDelta2(theAction);  
  218.  
  219.             // Figure out the distance between the current and previous locations  
  220.             float currentDistance = Vector2.Distance(currentPositionFingerOne, currentPositionFingerTwo);  
  221.             float previousDistance 
  222.                 = Vector2.Distance(previousPositionFingerOne, previousPositionFingerTwo);  
  223.  
  224.             // Calculate the difference between the two and use that to alter the scale  
  225.             float scaleChange = (currentDistance - previousDistance) * .01f;  
  226.             return scaleChange;  
  227.         }  
  228.  
  229.         public Vector3 CurrentAccelerometerReading(string theAction)  
  230.         {  
  231.             return GetInput(theAction).CurrentAccelerometerReading;  
  232.         }  
  233.  
  234.     }  

GestureDefinition.cs

 

  1. using System;  
  2. using Microsoft.Xna.Framework;  
  3. using Microsoft.Xna.Framework.Input.Touch;  
  4.  
  5. namespace InputHandlerDemo.Inputs  
  6. {  
  7.     /// <summary> 
  8.     /// 手势定义  
  9.     /// </summary> 
  10.     class GestureDefinition  
  11.     {  
  12.         public GestureType Type;  
  13.         public Rectangle CollisionArea;  
  14.         public GestureSample Gesture;  
  15.         public Vector2 Delta;  
  16.         public Vector2 Delta2;  
  17.         public Vector2 Position;  
  18.         public Vector2 Position2;  
  19.  
  20.         public GestureDefinition(GestureType theGestureType, Rectangle theGestureArea)  
  21.         {  
  22.             Gesture = new GestureSample(theGestureType, new TimeSpan(0),  
  23.                                         Vector2.Zero, Vector2.Zero,  
  24.                                         Vector2.Zero, Vector2.Zero);  
  25.             Type = theGestureType;  
  26.             CollisionArea = theGestureArea;  
  27.         }  
  28.  
  29.         public GestureDefinition(GestureSample theGestureSample)  
  30.         {  
  31.             Gesture = theGestureSample;  
  32.             Type = theGestureSample.GestureType;  
  33.             CollisionArea = new Rectangle((int)theGestureSample.Position.X,  
  34.                                           (int)theGestureSample.Position.Y, 5, 5);  
  35.  
  36.             Delta = theGestureSample.Delta;  
  37.             Delta2 = theGestureSample.Delta2;  
  38.             Position = theGestureSample.Position;  
  39.             Position2 = theGestureSample.Position2;  
  40.         }  
  41.  
  42.     }  

 

 

推荐阅读:
  1. Facebook Instant Game 测试、发布和分享小游戏
  2. 为什么游戏测试慢?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xna游戏 各种 输入测试

上一篇:*VMware虚拟机克隆后解决网络冲突问题

下一篇:oracle 11g exp默认空表不导出 原因及解决方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》