C#开发跨平台游戏——在MonoGame/XNA中使用游戏手柄GamePad
之前的教程只涉及鼠标点击的相关内容,今天我入手了一款游戏手柄——【有线版】北通蝙蝠2游戏手柄(BTP-BD2E),价格非常便宜65块人民币。该手柄支持XBox One,XBox360,PC,android,steam等支持四种模式。更重要的是能够用它来开发MonoGame游戏!
使用方法非常简单,买来后拆开包装,将USB线插入电脑。默认就是PC360模式。如果不是请长按 键,将模式切换到【PC360模式】手柄面板正下方指示灯显示为
。更多模式如下图:
打开Visual Studio,【文件】【新建】【项目】
选择【MonoGame Windows Project】如果你想开发XBox One游戏可以选择【MonoGame Windows 10 Universal】项目,项目名称修改为【MyGamePad】点击【确定】
在解决方案管理器中双击【Game1.cs】
找到Update方法,在方法中输入
1 2 3 4 5 |
var gamePad = GamePad.GetState(PlayerIndex.One); //初始化手柄实例 if (gamePad.IsButtonDown(Buttons.A))//判断游戏手柄是否按下A键 { System.Diagnostics.Debug.WriteLine("A");//在调试界面显示A } |
以上代码是判断手柄的按钮是否按下了A键,Buttons是一个枚举类型,列出了手柄上的所有按钮。IsButtonDown也可以用gamePad.IsButtonUp代替即按下后释放按钮。
也可以通过判断Button的状态来判断是否按下了按钮,代码如下:
1 2 3 4 |
if(GamePad.GetState(PlayerIndex.One).Buttons.A==ButtonState.Pressed) { System.Diagnostics.Debug.WriteLine("A"); } |
还可以通过Button的状态是否为释放状态来判断是否按下按钮,代码如下:
1 2 3 4 |
if(GamePad.GetState(PlayerIndex.One).Buttons.A != ButtonState.Released) { System.Diagnostics.Debug.WriteLine("A"); } |
为了检测手柄对应的按钮名称,我写了 一个方法来测试。将以下代码替换之前的代码:
1 2 3 4 5 6 7 8 9 10 |
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);//输出按钮名称 } } |
点击【启动】
当按下手柄上的各个按钮,在输出窗口就会出现相应的按钮名称
手柄的详细按键名称如下图:
游戏手柄的用法在MonoGame中就是这么简单!
经过测试在Linux平台,该手柄同样支持:
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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