中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

XNA游戲:各種輸入測試 上

發布時間:2020-06-29 06:23:34 來源:網絡 閱讀:376 作者:linzheng 欄目:開發技術

測試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.     }  

 

 

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

古浪县| 即墨市| 盐池县| 廉江市| 手游| 海安县| 甘谷县| 大港区| 四平市| 灵璧县| 阳西县| 仙游县| 青铜峡市| 威海市| 涟源市| 大新县| 阿荣旗| 夹江县| 景德镇市| 高要市| 禄劝| 阳新县| 额敏县| 临泽县| 洪洞县| 浙江省| 宜州市| 岐山县| 安新县| 芜湖市| 汉源县| 舟曲县| 五峰| 柯坪县| 南涧| 大化| 榆社县| 邳州市| 封开县| 松潘县| 陆河县|