Moving Items with Microsoft Visual Studio, XNA Game Studio and a GamePad

WEBINAR:On-Demand

In my previous article, I briefly covered installing the XNA Game Studio. The article proceeded to how you how to create a simple application that displayed an image. You also learned how to change the size and location of that image within your code.

Using an Xbox Game Pad with Your Applications

Now, you'll learn how to use an Xbox game pad with your applications. You'll see how to use some of the buttons on the Xbox controller including the X, Y, A, and B button as well as the LeftShoulder, RightShoulder, LeftStick, RightStick, and other controls. Specifically, you'll see how to move an image around on the page using some of these controls.

You should start with the final code listing from the previous article that helped you load an image. Alternatively, you can create a new XNA Windows Game project. From either listing, you'll find two lines of code that were automatically added to the project when it was created. These lines are worth pointing out now:

If you look at this code, you'll see that it is referencing a game pad. If you have a wired Xbox controller, you can plug it into a USB port on your PC and use it. The above line of code will then exit the game if the back button on the game pad is pressed. You could also test for other buttons on the controller. For example, the following adds checks to see if the A button is pressed:

In addition to the A button, you can also check the B, X, Y, and other buttons for a press in the same way. You can also check for button presses on the left and right sticks as well. If you do this in the Update method, then you will check the status of the button each time the game loops, which will be around 30 times a second. As such, each time Update is checked, if the A button is pressed, the logic above will execute. For example, if you wanted to move a rectangle in your program, you could use the A button to move it down on the page. You could do this by adjusting where the rectangle is drawn. A rectangle called myRect could have its Y value incremented each time the program loops and it finds the A button is pressed:

This code would adjust the Y coordinate where the rectangle called myRect is being drawn on the screen. It would do this when the Y coordinate is less than the size of the window minus the size of the image. This means that the image will move down the window, but not off the window. Of course, this code only adjusts the Y coordinate in the rectangle. It doesn't actually draw the rectangle on the page, nor does it set up the rectangle initially. Of course, you learned how to do that in the previous article.

Using what has just been covered, you could use the X, Y, A, and B buttons to move the image around the window. To do this, you follow similar logic to what was presented earlier:

Moving Items with Microsoft Visual Studio, XNA Game Studio and a GamePad

WEBINAR:On-Demand

Checking GamePad Button Presses

It is worth noting that the previous code listing checks to see if the button is pressed. As you hold the button down, your image will continue to move until it reaches the edge of the window. In many games, you want to test the pressing of a button. It might be that when you press a button down and release it that you only want a single action to occur. If that is the case, then if it takes you a half second to press and release a button, then you might actually call the Update method around 15 times. For the picture application that means a single push and release could cause you to move the image 15 pixels.

What if you wanted only one action to happen each time you pressed the button? You need a way to tell that the button was pressed and released. The easiest way to determine a full button press is to check for the button to be pushed down and then check for it to be released. When both actions have happened, then you know that you have a full button press.

To check for a button press, start by setting up a variable to hold the state of the controller the first time through. If the button was in a pressed state and then changes to released, you can then react. Otherwise, you don't have to count it as a press.

Tracking GamePad State

In the previous listing, the state of the game pad is called several times. In order to save a few processing cycles, you can actually create an object to hold the gamepad state and then check the various button states from that variable. More importantly, you can create a second object to hold the state of the gamepad the previous time you checked.

For the sample listing, two GamePadState objects are created called GamePad1 and origGamePad1. These objects are declared at the top of the Game1 class along with the other variables:

private GamePadState GamePad1;
private GamePadState origGamePad1;

With the variables declared, you now need to initialize them. You can initialize the origGamePad1 in the LoadContent method. You can initialize it to the state of the first gamepad:

origGamePad1 = GamePad.GetState(PlayerIndex.One);

This puts the game state from the first pad into origGamePad1. You'll now be able to use origGamePad1 to check the status of each of the buttons. More importantly, you'll be able to use it to check what the status was on the previous cycle of the gaming loop.

Of course, each time the game loops, you need to get the status again for the gamepad. You can do this using the GamePad1 type you declared:

GamePad1 = GamePad.GetState(PlayerIndex.One);

Once you get this new status you'll want to then compare each button's new state with the old state that was stored in origGamePad1. If the new state is released and the old state was pressed, then you know you have a completed button press and can therefore take action:

Once you've checked the states of the button and determined if button has been pressed, you can then take action. After all of your code is completed, you can then wrap up by shifting the current gamepad state to the previous state stored in the origGamePad1 object:

origGamePad1 = GamePad1;

With this, you've got all the pieces to check for button presses. Listing 2 at the end of this article contains a re-write of Listing 1 using. In Listing 2, you use the new logic for checking for completed button presses. Running the code in listing 2 requires that you keep pressing the button if you want to move an image around on the screen.

Wrapping Up

In this article you learned how to check for a complete button press rather than allowing the game to keep reacting over and over when a button is held down. In an upcoming article, you also learn how to do a few other actions based on what you can do with the controllers. This includes working with the cap controls on the gamepads.