Anatomy of a Windows Phone Game Project

This topic contains the following sections.

After the project is created, you enter the Visual Studio development area. By default, an XNA game solution contains two projects. The first is the actual Windows Phone game project, called MarsInvasion. The second is the MarsInvasionContent project. This is where XNA expects you to deposit all game content such as sound files, artwork, and so on. The primary MarsInvasion project is where the actual game logic and code are hosted.

Let’s take a quick look at what is provided when the project is created. Open the Game1.cs file, and quickly scan the code. This is your primary game code. This file is where you add code to load game content, draw and move objects, update internal game variables, and detect user input. If you have looked at video game code in the past, you have typically seen the main game loop that is responsible for updating game objects and drawing them on the screen. The game loop was a never-ending while loop that could be broken only when the user quit the game. However, in this XNA project, notice that there is no visible game loop code. Instead of writing your own implementation of this, the XNA framework creates a full game loop behind the scenes and gives you a few method implementations where you can put your game logic and game drawing code without having to worry about those methods being called continuously. In fact, the XNA framework provides the entire game engine for you right down to difficult and complex interactions with video card hardware. When creating games for the Windows Phone 7 platform with XNA, you don’t have to worry about any of this “plumbing” code and can focus solely on the game logic itself.

In the code in the Game1.cs file, the first method you see is called the constructor. The following code shows the constructor performing some basic functionality that is common to all Windows Phone 7 games:

public Game1()
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";

     // Frame rate is 30 fps by default for Windows Phone.
     TargetElapsedTime = TimeSpan.FromTicks(333333);

As you can see from the code, a new GraphicsDeviceManager object is created. It takes care of necessary interactions with the video hardware. Also, the default frame rate for the game is set to 30 fps, which is the standard frame rate for all Windows Phone 7 games. For the most part, you do not need to add any additional code in this method because several other methods are provided that you can take advantage of to initialize objects and data for your game.

The next method is the Initialize method, which is shown in the following code:

protected override void Initialize()

The Initialize method is called when your game starts, and you should plan on placing any game initialization code in here. For example, in Mars Invasion, you have a player ship and enemy ships, and these objects can be created and initialized in this method.

Next is the LoadContent method. This method is responsible for loading content into your game, such as 2-D art, 3-D models, sound files, and sprite fonts. Because they are stored in the Content project, you need to load these files and store them in your game objects. The LoadContent method gives you a place to do this. The following code shows the default implementation of this method:

protected override void LoadContent()
     // Create a new SpriteBatch, which can be used to draw textures.

     spriteBatch = new SpriteBatch(GraphicsDevice);
     // TODO: use this.Content to load your game content here.

You see more of the SpriteBatch class later, but for now understand that the SpriteBatch object is the main XNA object you use to draw game artwork.

The next method is the UnloadContent method, which is shown in the following code:

protected override void UnloadContent()
     // TODO: Unload any non ContentManager content here

The UnloadContent method gives you a place to unload textures that you might have previously loaded into memory. This frees up resources.

Next is the Update method. The Update method is where the bulk of your work is performed. The default implementation is shown in the following code:

protected override void Update(GameTime gameTime)
     // Allows the game to exit.
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

     // TODO: Add your update logic here.


The Update method gets called continuously as part of the main game loop provided by XNA. This method is where you do things like calculate the current position of player and enemy ships, do collision detection for missiles, and update the screen to reflect the current score.

Finally, you have the Draw method. This method is where you need to draw your game objects to the screen. The drawing process is made much easier by using the provided GraphicsDevice object. In the following code, you can see that the only thing being done on the screen is a clearing operation that uses the CornflowerBlue color value.

protected override void Draw(GameTime gameTime)

     // TODO: Add your drawing code here.


Believe it or not, at this point, you actually have a fully functioning program. You can even build and run it using the Windows Phone 7 emulator software. To do this, press the F5 key or find the Play icon on the toolbar, as shown in Figure 2. This starts the debugging process and deploys your new game to the emulator.

Figure 2. Start the emulator by clicking the Play icon from the build toolbar.

Referenced Image

When the game is running, it won’t look like much of a game, but you will see that the screen has been filled with the cornflower blue color, as shown in Figure 3.

Figure 3. Default game project running in the emulator

Referenced Image

The goal of the Mars Invasion game is to destroy alien ships as they appear on the screen and collect the highest score possible. This game mimics many of the Space Invader clone games you might have seen in the past. To get started, let’s look at the design and determine what objects might be necessary for this type of game.

Game Objects

To build your new game, you need to think about creating several game objects to represent the important players. First you need a player ship, and then you need enemy ships. Because both the player ship and all the enemy ships presumably fire at each other, you should probably plan on having a missile object. Next, you want to keep score and perhaps give the player a certain number of chances before considering the game over. You can create a heads-up display or HUD type of object that is responsible for displaying the score and the number of player ships that remain at a given time. Finally, to organize all of the potential enemy ships on the screen and keep track of them, you should plan on creating an enemy fleet object that is responsible for creating new enemy ships and managing any existing ones. To summarize, the following should be considered as separate C# class objects in your game:

  • PlayerShip

  • EnemyShip

  • Missile

  • EnemyFleet

GameComponent and DrawableGameComponent

When looking at the Game1.cs file you see several methods, such as Initialize, LoadContent, Update, UnloadContent, and Draw. The XNA framework calls the Initialize method when the game starts and then calls the LoadContent method to allow you to load any images, sounds, and so on.

At this point, you enter the game loop and the Update method is called, giving you a place to perform movement logic, missile firing, collision detection, input checks, and other game logic. The XNA framework repeatedly calls Update for you, so you can be assured that for each pass of the game loop, your logic code will be hit. Next, the framework calls the Draw method and gives you a place to draw each of your game objects, such as the player, enemy ships, and any missiles fired.

At first, it seems as if you can potentially end up with quite a bit of code in any one of these methods, and in fact, you can actually put all of your game logic in the Update method and all drawing logic in the Draw method. However, this would get messy quickly, even for a simple game like this one.

To facilitate cleaner game code, the XNA framework provides two classes called GameComponent and DrawableGameComponent. These base classes give you a way to keep all Initialize, LoadContent, Update, and Draw logic right in your game object classes. For example, if you have a PlayerShip class that loads player ship–specific artwork and sounds, and then it performs movement logic, you can inherit from the DrawableGameComponent class and you can override all of the standard methods, such as Initialize, LoadContent, UnloadContent, Update, and Draw. You can place player ship–specific logic right in the PlayerShip class itself, and the XNA runtime makes sure that your custom methods (Update, Draw, and so on) are called for every pass during the game loop.

Your choice of inheriting from the GameComponent class as opposed to the DrawableGameComponent class depends on whether or not the game object is drawn on the screen. If you have a game object that needs to process custom logic in the Update method but doesn’t actually need to be drawn, you can inherit from the GameComponent class. If you need drawing capabilities, you should inherit from the DrawableGameComponent class instead.

Now that you have some background information about the XNA platform, the tools, and some of the base classes, it’s time to start creating this game. Because every game needs resources such as artwork and sounds to run, you should look at how to add resources to the game project. In the Mars Invasion source code that accompanies this article, you will see the MarsInvasionContent project. This is the project where all game assets are stored. XNA makes it simple to add existing content to this project. All you need to do is right-click the project node, select Add Existing Item, and then navigate to the game asset you want to include. For Mars Invasion, several different types of game assets are required. First up is the artwork.

Artwork and 2-D Textures

For this game, the following artwork is included in the content project:

  • player_ship.png—A 2-D texture representing the player ship (shown in Figure 4).

    Figure 4. Player ship artwork

    Referenced Image
  • player_explosion_1.png ... player_explosion_5.png—2-D textures that provide an explosion animation when the player ship is hit (shown in Figure 5).

    Figure 5. Player explosion sequence

    Referenced Image
  • player_missile.png—A 2-D texture representing a player missile (shown in Figure 6).

    Figure 6. Player missile artwork

    Referenced Image
  • enemy_ship.png—A 2-D texture representing an enemy ship (shown in Figure 7).

    Figure 7. Enemy ship artwork

    Referenced Image
  • enemy_explosion_1...enemy_explosion_3—2-D textures that provide an explosion animation when an enemy ship is hit by a player missile (shown in Figure 8).

    Figure 8. Enemy explosion sequence

    Referenced Image
  • enemy_missile.png—A 2-D texture representing an enemy missile (shown in Figure 9).

    Figure 9. Enemy missile artwork

    Referenced Image
  • background.png—A 2-D texture that serves as the space background for the game; all other images are overlaid on top of this one (shown in Figure 10).

    Figure 10. Background artwork

    Referenced Image

Sound and Music

Using sound effects and music in an XNA game is just as easy as using artwork. In the content project of the game, you see an Audio folder that holds all of the required sound files for the game. Before the release of XNA Game Studio, it was common to have to convert music and sound effect audio formats to a format that was specific for use in games. XNA Game Studio 4.0 makes it much easier to use many of the popular sound formats, such as .wav, .mp3, .wma, and so on.

Like the artwork, you can simply right-click the content project node in Solution Explorer, select Add Existing Item, navigate to your sound files, and add them to the project. One thing to note about audio files is that XNA supports two types of audio for your game, and you need to distinguish these types after adding the files to the project. In an XNA game, you can set an audio file to be used as either a sound effect or as a song. You distinguish between the two by looking at the Properties section of the selected audio file. For example, the Audio folder contains a background.mp3 file that is used to play background music while the user is playing the game. This is not a sound effect because it should loop continuously while the game runs. To mark this file as a song, you simply set Content Processor to Song - XNA Framework, as shown in Figure 11.

Figure 11. Setting Content Processor to Song – XNA Framework

Referenced Image

If you take a look at some of the other options in the drop-down control, you can see that the XNA content pipeline supports many different types of game content, including sound effects, music, video, and more. Figure 12 shows all the choices available.

Figure 12. XNA Content Processors

Referenced Image

In addition to the background.mp3 file, several other audio files are required for the game. The following table lists files in the Audio directory and their assigned Content Processors.


Game Usage



Background music for game



Sound effect for when player ship fires missile

Sound Effect


Sound effect for when player ship is hit by enemy missile

Sound Effect


Sound effect for when enemy ship fires missile

Sound Effect


Sound effect for when enemy ship is hit by player missile

Sound Effect

Previous Article: Beginning 2D Game Development

Continue on to the Next Article: Creating Ships