O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Keeping Score

You have levels and end-game logic now, but what’s the fun when your score is always zero? In this section, you’ll flesh out scoring for the game. You’re already displaying the score at the end of the game, but you’ll want to let players see their scores as they play. To do that, add a class-level SpriteFont variable to your Game1 class, with which you’ll draw the score:

SpriteFont scoreFont;

And yes, that’s right: the next thing that you’ll need to do is add a new spritefont to your project. Right-click the 3D GameContent\Fonts folder in Solution Explorer and select AddNew Item…. Select the Sprite Font template on the right and name the file ScoreFont.spritefont.

To make your scoring font stand out a bit more, open the ScoreFont.spritefont file and find the <Style> element. This element lets you tweak properties such as setting the text in bold, italics, etc. Change the spritefont to use a bold font by changing the <Style> tag as follows:

<Style>Bold</Style>

Tip

The <Style> tag entries are case-sensitive, as it says in the actual spritefont file. Make sure you use the text Bold instead of bold or BOLD.

Next, in the LoadContent method of your Game1 class, add the following code to load the font:

scoreFont = Content.Load<SpriteFont>(@"Fonts\ScoreFont");

Now you’ll want to use that spritefont to draw the score on the screen. In addition to just drawing the score, though, it would be helpful for players to see how many more ships they can miss in each level.

To do that, you’ll ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required