之前的教程只涉及鼠标点击的相关内容,今天我入手了一款游戏手柄——【有线版】北通蝙蝠2游戏手柄(BTP-BD2E),价格非常便宜65块人民币。该手柄支持XBox One,XBox360,PC,android,steam等支持四种模式。更重要的是能够用它来开发MonoGame游戏!

 

image

使用方法非常简单,买来后拆开包装,将USB线插入电脑。默认就是PC360模式。如果不是请长按 image 键,将模式切换到【PC360模式】手柄面板正下方指示灯显示为image 。更多模式如下图:

image

打开Visual Studio,【文件】【新建】【项目】

image

选择【MonoGame Windows Project】如果你想开发XBox One游戏可以选择【MonoGame Windows 10 Universal】项目,项目名称修改为【MyGamePad】点击【确定】

image

在解决方案管理器中双击【Game1.cs】

image

找到Update方法,在方法中输入

var gamePad = GamePad.GetState(PlayerIndex.One); //初始化手柄实例
if (gamePad.IsButtonDown(Buttons.A))//判断游戏手柄是否按下A键
{
       System.Diagnostics.Debug.WriteLine("A");//在调试界面显示A
}

以上代码是判断手柄的按钮是否按下了A键,Buttons是一个枚举类型,列出了手柄上的所有按钮。IsButtonDown也可以用gamePad.IsButtonUp代替即按下后释放按钮。

也可以通过判断Button的状态来判断是否按下了按钮,代码如下:

if(GamePad.GetState(PlayerIndex.One).Buttons.A==ButtonState.Pressed)
         {
             System.Diagnostics.Debug.WriteLine("A");
         }

还可以通过Button的状态是否为释放状态来判断是否按下按钮,代码如下:

if(GamePad.GetState(PlayerIndex.One).Buttons.A != ButtonState.Released)
           {
               System.Diagnostics.Debug.WriteLine("A");
           }

 

为了检测手柄对应的按钮名称,我写了 一个方法来测试。将以下代码替换之前的代码:

var gamePad=GamePad.GetState(PlayerIndex.One);

            var buttonsNames = System.Enum.GetNames(typeof(Buttons));//获取所有手柄按钮名称
            foreach(var btnName in buttonsNames)
            {
                if(gamePad.IsButtonDown((Buttons)System.Enum.Parse(typeof(Buttons), btnName)))
                {
                    System.Diagnostics.Debug.WriteLine(btnName);//输出按钮名称
                }
            }

点击【启动】

image

当按下手柄上的各个按钮,在输出窗口就会出现相应的按钮名称

image

手柄的详细按键名称如下图:

640

游戏手柄的用法在MonoGame中就是这么简单!

经过测试在Linux平台,该手柄同样支持:

image

完整代码如下:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MyGamePad
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            var gamePad = GamePad.GetState(PlayerIndex.One);

            var buttonsNames = System.Enum.GetNames(typeof(Buttons));
            foreach (var btnName in buttonsNames)
            {
                if (gamePad.IsButtonDown((Buttons)System.Enum.Parse(typeof(Buttons), btnName)))
                {
                    System.Diagnostics.Debug.WriteLine(btnName);
                }
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}



如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流

原创文章,转载请注明本文链接地址(违者必究):C#开发跨平台游戏——在MonoGame/XNA中使用游戏手柄GamePad

打赏作者
很喜欢这篇文章,打赏犒劳下作者,以此激励作者创作更多

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注