Add the code needed to receive input and move the player around.
Now that we have a player graphic drawing - the golden airship you saw in the last step - we can add a critical component that every game needs: the ability to move the player around based on input. In this step, we’ll be adding input logic so that the ship can be moved around the screen. Let's start in our core game file: Game1.cs.
You might have noticed the Update() method inside the Game1.cs file; so far, we haven't done anything with it. However, it's a critical component to the game loop. Like Draw(), the Update() method is called automatically many times a second (a target rate is thirty times or more per second). By tracking the time taken between calls to update - the delta, we can make incremental changes to things like the player's position (remember the Player.Position variable?). Because Update() is called many times every second, those changes will look like smooth motion to the user.
We have a number of input devices the player might be using, depending on the platform we choose.
Shooter’s controls are all about moving around the screen – we’ll choose to take input from the keyboard, the Xbox 360 Gamepad, and the Phone touch screen. We can write the input for all three platforms, all in the same code, and it won’t be a problem. The unused inputs will simply be ignored depending on the platform. We do need to make one change before we start, however.
Look up at the very top of Game1.cs, where you see a number of using statements. At the end of the list, make a new line and add the following line:
This adds support for the touch input on Phone. Now, we’re ready to jump in and start writing input code. First, we need a few variables that can be used to track what’s being pressed on the keyboard and the Gamepad.
Look for the first { mark under the start of the Game1 class, and go just below the Player player; instantiation call you made in the last step. Add a new line and then add the following:
Notice, we snuck in a float value to represent the player’s move speed. A float is a number with a fractional component; useful when we need to get more specific than just whole numbers. The player movement speed isn’t strictly about taking input – it’s about how we respond to it – but we need it later in this step, so we’ll add it now.
Notice how the player move speed is instantiated, but it hasn’t been set equal to anything. We need to do that, as well as enable the Touch gestures on Phone. We’ll do this in the Initialize() method, same as we did for some of the variables we wrote up in the last step.
Scan down the code for the Initialize() method, and below the player = new Player (); code you wrote in the last step, add these lines:
Good – notice how the player move speed was set to 8.0f. The f means “float value”. It’s used whenever you directly want to assign a number with a fractional component to a variable. The f distinguishes the value from a double, which is a different type of fractional number we don’t generally use in XNA Game Studio.
The TouchPanel.EnabledGestures value is an enumeration (usually just called an enum), a variable that can take a value from a list of predetermined values. In this case, we’re telling the Touch input system through this enumeration to only care about a specific type of input.
Now, we’re ready to write the actual input update method itself. We could add our code directly to Update(), but there's a better way - we should encapsulate it into our own method called UpdatePlayer(), and do all our work there, then just call it from Update(). That will keep the Update() method from getting messy as we add more objects to the game later.
Let’s create a brand new method inside Game1.cs, called UpdatePlayer(), and add controls for moving up, down, left, and right on all three platforms.
To do this, you’ll want to find a nice empty spot inside the Game1 class, but not inside any other method. Try this trick – look for the Update() method, find the first { mark underneath the function name. Then, follow it down until you find a corresponding } mark that’s got the same indentation level. Then, go one line below that, make a new line, and you should be in safe territory.
Add the following lines to make your new UpdatePlayer method:
That's a big method, but it's fairly straightforward. It checks all the inputs we care about, and responds accordingly:
Now that we have our UpdatePlayer() method, we will call the method we wrote from the Game’s Update() method so that every time Update() is called by the game loop, it will call our UpdatePlayer() method. Anything we want to update every frame should be done the same way.
Look for the Update() method inside the Game1 class, and replace the line marked “// TODO: Add your update logic here” with the following:
You've written all the input code you'll need to get your ship moving. Let's try it out! To run your game, press CTRL+F5.
Remember: If pressing CTRL+F5 doesn't do anything, be sure to right-click on the Shooter project in your Solution Explorer and select Set as Startup Project, then press CTRL+F5 again.
If you've done it right, you'll see the game pop up on your chosen platform. If there are any errors, you'll see them pop up instead in a list at the bottom of the Visual Studio window.
Try giving the game some input. If you're on Windows, try the arrow keys on your keyboard. On Xbox 360, either the thumbsticks or the directional pad. On phone, touch your finger down and drag around.
If it's not working, don't worry. Look below for a downloadable version of this completed step that you can open to keep going error-free.
checkpoint! check your work and get codeAnother step done, another step closer to a full game.
We recommend you take a second to download a fresh copy of the project with the source code synchronized to the end of this step, especially if you're having trouble getting your code to run.
Just close your Visual Studio environment, click on the link below for the platform you're developing on, then unzip the file and open the enclosed .SLN with Visual Studio. You'll be all caught up!
![]() |
![]() |
![]() |
| Taking Input - Windows | Taking Input - for Xbox 360 | Taking Input - for Phone |
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/