Part 8 - Life & Score
At this point, the game should feel almost like a real game. The biggest thing missing is a way to track the player's life and score. Do this by creating life and score fields on the ArcadeFlyerGame class, and displaying the information in the game using a SpriteFont. All changes will take place in the ArcadeFlyerGame.cs class.
Adding Life
In almost every game, there is some concept of health. In this game, the player should lose a life whenever a projectiles with the sprite.
- Add a
private intfield namedlifeto theArcadeFlyerGameclass - After the declaration, set the
lifefield to3private int life = 3; - Find the
Updatemethod in theArcadeFlyerGameclass - Find the
ifstatement that checks whether the player has collided with an enemy projectileif (!playerProjectile && player.Overlaps(p)) - In the body of that
ifstatement, decrement the value oflifeby 1life--;
Adding Score
Another thing that exists in almost every game is a score. In this game, the player should gain a point whenever an enemy is destroyed.
- Under the
lifefield, add aprivate intfield namedscoreto theArcadeFlyerGameclass - After the declaration, set the
scorefield to0private int score = 0; - Find the
Updatemethod in theArcadeFlyerGameclass - Find the
ifstatement that checks whether an enemy has collided with a player projectileif (enemy.Overlaps(p)) - In the body of that
ifstatement, increment the value ofscoreby 1score++;
Showing the Life & Score
Now the life and score are tracked, but they don't actually show up anywhere yet! To do that, it will be necessary to create a SpriteFont like this.
Creating a SpriteFont Asset
Just like all the image assets in the game, fonts are created with the Content Pipeline tool.
- Open up the Content.mgcb file in the MonoGame Pipeline Tool
- Click the "New Item" button

- In the popup, select the "SpriteFont Description (.spritefont)" option
- Change the name to "Text"
- Click the "Create" button at the bottom

- When the popup closes, click the "Build" button

- Go back into VS Code, and make sure the Text.spritefont file appears in the Content directory

At this point, the Content.mgcb file should be updated, and there should be a Text.xnb file in the Content/bin/DesktopGL/ directory.
Using the SpriteFont to Write in the Game
Now that the asset has been created, it will be possible to use it in the game's code.
- Under the
spriteBatchfield in theArcadeFlyerGameclass, add a newprivate SpriteFontfield namedtextFontprivate SpriteFont textFont; - Find the
ArcadeFlyerGame()constructor - Somewhere in the method, initialize the
textFontfield usingContent.LoadtextFont = Content.Load<SpriteFont>("Text"); - Find the
Drawmethod - Toward the end of the method, call the
spriteBatch.DrawStringmethod- The first parameter should be a
SpriteFontobject - usetextFont - The second parameter should be a
stringvalue - create a string that has both thelifeandscorevalues - The third parameter should be a
Vector2object for position - use theVector2.Zerofor the upper left - The fourth parameter should be a
Colorobject - useColor.Black
- The first parameter should be a
- Add a semi-colon at the end, and it should be good to go
spriteBatch.DrawString(textFont, $"Life: {life}\nScore: {score}", Vector2.Zero, Color.Black);
At this point, try running the game. The "Life" and "Score" values should update based on what happens in the game!
Final Code
The final code for this walkthrough is available on GitHub.