When you start with a new project in XNA, you get some starter code templated out to begin writing your game. In this article, I’m going to walk through what you get. I will be using Visual Studio 2010 Express for Windows Phone, and creating a Windows Phone Game project type. This article is the first in a series that will lead to the creation of the TriangleShooter game I mentioned before.

What’s in the solution?


As you can see, the default solution contains two projects. The first project contains the code for your game, and is named whatever you select when you first create the project. In this case, I’ve named the project TriangleShooter, to go along with my game idea. The other project is named as the first project appended with Content, so in my case, TriangleShooterContent, and is a location to place all of your content including graphics, models, audio and video.

In the primary project, you’ll notice three graphics files. The Background.png file is the image that will be shown on the main page of the phone when you have pinned your application. The GameThumbnail.png will be displayed on the list of all applications installed on the phone to the right of the main page. The Game.ico is an icon used only for Games for Windows, and is not used for Windows Phone or Xbox.

Program.cs contains the entry point for your project. It’s actually not used in Windows Phone projects, just Windows and Xbox.

Game1.cs is where we will spend the rest of our time in this article. It gives you everything you need to get started with building a game. Let’s walk through the functions that come standard in a new project.

  1. public Game1()
  2. {
  3.     graphics = new GraphicsDeviceManager(this);
  4.     Content.RootDirectory = "Content";
  6.     // Frame rate is 30 fps by default for Windows Phone.
  7.     TargetElapsedTime = TimeSpan.FromTicks(333333);
  8. }

The constructor sets up your graphics object, which you will be using to access information about your viewport, orientation, set your game to full screen, and so on.

  1. protected override void Initialize()
  2. {
  3.     // TODO: Add your initialization logic here
  5.     base.Initialize();
  6. }

The initialize method is used for initializing any variables you have created for your game. My recommendation is to create a function that you will call that resets everything back to their initial values for a new game, which will allow you to just call that function on game reset, saving you from having to write code twice.

  1. protected override void LoadContent()
  2. {
  3.     // Create a new SpriteBatch, which can be used to draw textures.
  4.     spriteBatch = new SpriteBatch(GraphicsDevice);
  6.     // TODO: use this.Content to load your game content here
  7. }

LoadContent allows you to bring anything over from the Content project. Typical examples include graphics files like PNGs that will come over as Texture2D, WAVs that come over as SoundEffect, and  MP3s that will come over as Song (though you can use them for SoundEffect as well). If you are doing 3D development, you’ll also see files that represent models like X. You can also create custom pipeline projects. I’ll go through one of those in a future article I am working on around sprite sheet animations.

The spriteBatch that it is initializing is used when drawing 2D graphics and text onto the screen. We’ll be using this a lot.

  1. protected override void UnloadContent()
  2. {
  3.     // TODO: Unload any non ContentManager content here
  4. }

The UnloadContent method is used for any special loading you did outside of the scope of the LoadContent method. I have actually never put anything into this method.

  1. protected override void Update(GameTime gameTime)
  2. {
  3.     // Allows the game to exit
  4.     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  5.         this.Exit();
  7.     // TODO: Add your update logic here
  9.     base.Update(gameTime);
  10. }

Update is the first of two methods, the other being Draw, that will be called over and over in your loop, alternating between the two. Update is where you will handle all of your user input, AI routines, anything that changes stuff around. When processing is taking longer than the 1/30th of a second that it should, Update will be called over Draw, which makes sense because if nothing gets updated, the Draw routine shouldn’t be any different than the last time it was drawn.

The code inside the block by default checks to see if the user is pressing the back button, and exiting the game if it is. The fact that it is written as if you are pulling state from the first player’s gamepad is actually a really cool thing about using XNA for multiple platforms. That one block of code works for Xbox, Windows, and Phone.

  1. protected override void Draw(GameTime gameTime)
  2. {
  3.     GraphicsDevice.Clear(Color.CornflowerBlue);
  5.     // TODO: Add your drawing code here
  7.     base.Draw(gameTime);
  8. }

Draw is where you want to put everything that gets displayed on the screen. For a 2D game, you’re going to make a lot of calls to methods in the spriteBatch we initialized earlier.

The default code simply clears the screen with a beautiful Cornflower Blue. You can actually run the game now, and you’ll get a windows that looks like this:


I call it the Blue Screen of Life, because you know that everything is working.

Next up, I’ll walk through everything we need to get some basic gameplay.