© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
L. Salin, R. MorrarGame Development with MonoGamehttps://doi.org/10.1007/978-1-4842-7771-3_7

7. Game State and Gamepad Input

Louis Salin1   and Rami Morrar2
(1)
Cedar Park, TX, USA
(2)
San Leandro, CA, USA
 

You have done what is necessary for the base of your game so now you need to build around it. You can’t just have the player start off in the middle of the game, and you want your player to play with more than just a keyboard. Let’s make a main menu and code in a gamepad controller. You’ll start with the gamepad input detection.

Gamepad Detection

MonoGame natively supports Xbox 360 controllers in the framework, so you’ll be using one to map out the input for your game. Since your game is primarily only two buttons and three directions, you really only need to program the analog stick, D-pad, and four face buttons (A, B, X, and Y). Let’s write some code to get this going. Add this line of code right under the InputManager class reference in the GameRoot.cs file where your global variables are located:
GamePadCapabilities capabilities = GamePad.GetCapabilities(PlayerIndex.One);
And in the update method, put the following code logic into your game. You update each of the buttons along with the D-pad and Left Analog to have your player character move left or right, as well as jump and punch:
            #region Controller Input
            if (capabilities.IsConnected)
            {
                AnimStates();
                GamePadState state = GamePad.GetState(PlayerIndex.One);
                if (state.IsButtonDown(Buttons.B))
                {
                    AnimState = 3;
                }
                if (state.IsButtonDown(Buttons.Y))
                {
                    AnimState = 3;
                }
                if (state.IsButtonDown(Buttons.A) && !player.hasjumped)
                {
                    player.position.Y -= 14;
                    player.gravity = -7.5f;
                    player.hasjumped = true;
                }
                if (state.IsButtonDown(Buttons.X) && !player.hasjumped)
                {
                    player.position.Y -= 14;
                    player.gravity = -7.5f;
                    player.hasjumped = true;
                }
                if (state.IsButtonDown(Buttons.DPadLeft) && !player.isCollidingside)
                {
                    flip = true;
                    AnimState = 1;
                    player.position.X -= player.speed;
                    CameraPos.X -= player.speed;
                    hearts.Positions[0].X -= player.speed;
                    hearts.Positions[1].X -= player.speed;
                    hearts.Positions[2].X -= player.speed;
                }
                if (state.IsButtonDown(Buttons.DPadRight) && !player.isCollidingside)
                {
                    flip = false;
                    AnimState = 1;
                    player.position.X += player.speed;
                    CameraPos.X += player.speed;
                    hearts.Positions[0].X += player.speed;
                    hearts.Positions[1].X += player.speed;
                    hearts.Positions[2].X += player.speed;
                }
               if (capabilities.HasLeftXThumbStick)
                    {
                    //Moves player with the Thumbstick
  if (state.ThumbSticks.Left.X < -0.5f && !player.isCollidingside)
                    {
                        flip = true;
                        AnimState = 1;
                        player.position.X -= player.speed;
                        CameraPos.X -= player.speed;
                        hearts.Positions[0].X -= player.speed;
                        hearts.Positions[1].X -= player.speed;
                        hearts.Positions[2].X -= player.speed;
                    }
  if (state.ThumbSticks.Left.X > .5f && !player.isCollidingside)
                    {
                        flip = false;
                        AnimState = 1;
                        player.position.X += player.speed;
                        CameraPos.X += player.speed;
                        hearts.Positions[0].X += player.speed;
                        hearts.Positions[1].X += player.speed;
                        hearts.Positions[2].X += player.speed;
                    }
                }
            }
            #endregion

If you run the game with this code in it, you will notice that your player is now able to move with an Xbox controller plugged in!

What MonoGame is doing is detecting your Xbox 360 controller’s driver (a computer program that operates or controls a particular type of device that is attached to a computer) and is initiating your program to take the controller’s input.

We tested other devices and interestingly even controllers for the PlayStation 4 also work. Do note that not all controllers are created equal and you might have to do some tuning to make your controller’s input detection work correctly with each controller’s .dll files.

Let’s move on to changing screens.

Game States

Currently in your code, you only have the default game state rendering out your main level. Since your game is simple, you only really need a couple game states to render: a main menu, a screen when your player dies, and a screen for when your player wins.

The main menu is very simple. You wrap your entire GameRoot class in a Boolean. You set that Boolean to false, and when you start the game by pressing Enter, the game will be drawn out like so:
public class GameRoot : Game
{
bool gameStarted = false;
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
gameStarted = true;
}
}
The gameStarted bool is seen throughout the GameRoot class. When this bool is set to false, you have just your background with some text telling the player to press Enter to play the game.
protected override void Update (GameTime gameTime){
if (!gameStarted)
{
string title = "Mini Man";
string startText = "Press Enter To Start";
_spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
_spriteBatch.DrawString(font, title, new Vector2(150, 200), Color.Black);
_spriteBatch.DrawString(font, startText, new Vector2(115, 220), Color.Black);
}
}
Next, you update the player and keys for when your player dies using the playerisDead Boolean.
if(!playerisDead)
{
player.Update(gameTime);
}
You can use a different bool, playerWon, to stop the player from moving and bring up some text for when the player beats the samurai boss in your GameRoot’s Draw() method. You also need to update the GameRoot’s Update() method in order to stop the player from moving when playerWon is set to true:
Update(GameTime  gameTime)
{
...
if (!playerisDead || !playerWon)
{
player.Update(gameTime);
}
}
Draw(GameTime gameTime)
{
...
if (playerWon)
{
string Won = "Congrats, you won!";
_spriteBatch.DrawString(font, Won, CameraPos, Color.Black);
}
}

Conclusion

This chapter was brief but important on how to do simple game states. In the next chapter, you’ll figure out how to add particles and sound effects to your game to give it some polish and finally ship out your game!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.119