RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Introducing XNA Game Studio Express : Page 3

Microsoft's XNA Game Studio Express gives you powerful game tools, and the ability to create games for both Windows and Xbox 360.

Add the Car Image
Now you'll add the car image (see Figure 4) to the project in exactly the same way you added the road image, and then add a new Texture2D object to the class to store the car image.

Figure 4: Adding the car image to the game project.
Add the following code to the beginning of the Game1.cs class:

   private Texture2D mCar;
   private Rectangle mCarPosition;
You saw the Texture2D object before when you added the road image, and once again you'll use it to store an image. The Rectangle object is an XNA Framework object used to store a rectangular shape with positioning and size information. In this game the mCarPosition rectangle keeps track of where the car is and how big the car should be.

You need to add code to initialize these new objects. Add the following line of code to the StartGame method to initialize the position object.

   mCarPosition = new Rectangle(280, 440, 
     (int)(mCar.Width * 0.2f), 
     (int)(mCar.Height * 0.2f));
Next, initialize the Texture2D object by loading the car image from disk, and storing it in the object using the following code. Add the code line to the LoadGraphicsContent method, where you added code earlier to load the road image:

   mCar = content.Load<Texture2D>("Car");
With the car positioned and the image loaded, you can now draw the image to make sure you have it all set up properly. When drawing 2D images with SpriteBatch, it is important to draw your images back to front. For example, if you draw the car first, and then draw the road, the road image will be drawn over the car image and the player will never see it.

Add the following line of code to the Draw method, making sure the road image gets drawn before the car image:

   mSpriteBatch.Draw(mCar, mCarPosition, Color.White);
Figure 5: Drawing the car image to the screen.
Build and run your game project to make sure you added the drawing code for the car correctly. You should see a little red car zooming along the road as shown in Figure 5.

Now you can start capturing user input to move the car.

Getting User Input: Keyboard
To gather user input from the keyboard you must add some new class-level objects to the game. Add the following code to the beginning of the Game1.cs class:

   private KeyboardState mPreviousKeyboardState;
   private int mMoveCarX;
The KeyboardState object is part of the XNA Framework; you use it to store the state of the keyboard. You can query which keys are pressed—or not being pressed—at any given point in time. The KeyboardState object stores the previous keyboard state, so (for example) you can check to see if a key that the user is currently pressing was pressed last time you checked the state. This allows you to do things such as enforce a rule that users must release a key before repeating some action. In this case you want the car to change lanes every time the user presses the spacebar, but you don't want to continually switch lanes while the user holds the spacebar down. That is where the mPreviousKeyboardSate KeyboardState object comes into play.

The second object, the mMoveCarX integer defined above, stores how far to move the car in the X direction. You'll use this value to move the car back and forth between the lanes. You start by initializing mMoveCarX in the StartGame method using the following line:

   mMoveCarX = 160;
Now you'll add code to the Update method to check user input and move the car accordingly. You want the car to switch lanes only when users press the spacebar and you want to make sure they have released the spacebar before moving the car again. Add the following lines of code to the Update method:

   KeyboardState aCurrentKeyboardState =
   if (aCurrentKeyboardState.IsKeyDown(
      Keys.Escape) == true)
   if (aCurrentKeyboardState.IsKeyDown(
      Keys.Space) == true &&
      Keys.Space) == false)
      mCarPosition.X += mMoveCarX;
      mMoveCarX *= -1;
   mPreviousKeyboardState = aCurrentKeyboardState;
This code starts off by collecting information about the current keyboard state. Next, it checks to see whether the user has pressed the Escape key, which indicates that the user wants to exit the game. If not, the code then checks to see whether the spacebar has been pressed, and if so, that it was not pressed continuously since the last check (this prevents the user from holding it down to repeatedly switch lanes). If that's true, the code moves the car between the lanes by changing its current X position.

The last line stores the current keyboard state in the mPreviousKeyboardState object for use the next time the Update method fires.

Build the project again and run it. Now, when you press the spacebar, the car should move between the lanes as it zooms along the scrolling road.

Getting the User Input: Xbox 360 Controller
If you intend to deliver your games to the Xbox 360, you need to support the Xbox gamepad instead of the keyboard. Fortunately, they're similar. You can easily add gamepad support to your game for any wired Xbox 360 (or wireless with the appropriate dongle adapter) controller. Both will plug into a USB port on your computer. The XNA Framework provides built-in support for the Xbox 360 gamepad.

To gather gamepad input, you need to add some more class-level objects to the game. Add the following code to the beginning of the Game1.cs class:

   private GamePadState mPreviousGamePadState;
The GamePadState object is built into the XNA Framework. It lets you store the gamepad state. You use it similarly to the KeyboardState object. You just need to check whether the user has pressed and released a button on the gamepad. Again, you do that in the Update method. Because you may not know whether a user is running the game on a computer or on the Xbox, enhance the code that checks to see whether the Escape key has been pressed so it also checks to see if a user pressed the Back button on the gamepad. In either case, exit the game.

   KeyboardState aCurrentKeyboardState =
   GamePadState aCurrentGamePadState =
   if (aCurrentKeyboardState.IsKeyDown(
      Keys.Escape) == true || 
      aCurrentGamePadState.Buttons.Back == 
The code next checks to see if the user has pressed the "X" button on the controller; if so, it moves the car. Note that the check process is very similar to the way you checked the spacebar previously.

       == true &&
       == false) || (aCurrentGamePadState.Buttons.X 
       == ButtonState.Pressed && 
       mPreviousGamePadState.Buttons.X == 
       mCarPosition.X += mMoveCarX;
       mMoveCarX *= -1;
   mPreviousKeyboardState = aCurrentKeyboardState;
   mPreviousGamePadState = aCurrentGamePadState;
Finally, the code stores the current gamepad state in the mPreviousGamePadState object for use the next time Update gets called.

Go ahead and build the project. You should be able to use your Xbox 360 controller or the computer keyboard to move the car between the lanes and exit the game.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date