Add user interface elements to give information to the player

We’re almost to the end; this is the final step for Shooter, and the last coding exercise in this tutorial. When you think of games, the action of the game commonly comes to mind, but what may not be so obvious are the elements that help you track your progress and judge things like score, health, and bonuses. Together, these elements on the screen are known as a user interface and are an expected part of a good game.
We’ll create a very basic user interface with a score variable and a health variable, drawing them together at the top-left of the screen using a SpriteFont, a class that draws text on the screen just like a regular graphical element. This is simple enough that we can do all our work inside Game1.cs.
Switch to the Game1 class by double-clicking the Game1.cs file in the Solution Explorer in Visual Studio.
Inside our very familiar Game1 class, we’ll add the necessary variables to the top of the class, then move down through initialization, loading, updating, and drawing – the pattern we’ve used for nearly all our other objects.
Look for the first { mark under the start of the Game1 class, and go just below Song gameplayMusic; that you added in the previous step. Add a new line and then add the following:
We’ll be drawing both score and health, but we already have a health value inside Player – we’ll use that instead of a new health value here. Now that we have the font and score variable instantiated, let’s initialize them, starting with the score variable inside Initialize().
Find the Initialize() method. Inside that method, below the explosions = new List
Since the SpriteFont is content, we need to load it through the LoadContent() method. Find the LoadContent() method. Inside that method, above the call to PlayMusic(), add the following:
We’re all set to start updating and drawing score and health. Health is automatically being updated in the Player class thanks to work we did back in one of our earliest steps. We will have to add a call to update the score, however. We’ll want to do that whenever we destroy an enemy, either by shooting it or by ramming it with our ship. We will do that in UpdateEnemies(). Each time an enemy mine’s health reaches 0, we add a value to the player score equal to the score value we stored in the enemy.
Scroll down and find the UpdateEnemies() method. Look for the line marked explosionSound.Play(); just below it, add the following lines:
We are now updating both values. Finally, we need to draw these values, with some text in front of them, on screen. We’ll use the Draw() method. Find the Draw() method, and insert the following lines just below the } mark underneath the call to explosions[i].Draw:
And finally, since we haven’t yet done anything with the health value, let’s put a little bit of logic in that resets the player’s score if their health reduces down to zero or less. You’ll want to do this at the very bottom of UpdatePlayer():
And we’re done! It’s time to build and run this game to see all your code in action and working together.
Build your game by pressing CTRL+F5, and let’s see how it looks. You should see the score and health meters now at the top-left, updating as you destroy enemies and take damage. It should look like the screenshot below:

If it doesn’t look right, or won’t compile, don’t worry. Just below you can download the finished game and check your work.
checkpoint! get the complete gameThis is it; the end of the project and our completed game. Congratulations on building a comprehensive 2D shooter with XNA Game Studio.
If you’d like, you can download the completed version of this game below, to check how your code compares, or as a jump-off point to further customizing or adding features.
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!
![]() |
![]() |
![]() |
| Complete Game - for Windows | Complete Game - for Xbox 360 | Complete Game - for Phone |
Now that you’ve completed the game, head to the final step to learn a little bit about where you can go next now that you’re a game developer. Congratulations!
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/