Windows Phone Resources
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.
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.
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.
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.
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.
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.
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.
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.