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 int
field namedlife
to theArcadeFlyerGame
class - After the declaration, set the
life
field to3
private int life = 3;
- Find the
Update
method in theArcadeFlyerGame
class - Find the
if
statement that checks whether the player has collided with an enemy projectileif (!playerProjectile && player.Overlaps(p))
- In the body of that
if
statement, decrement the value oflife
by 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
life
field, add aprivate int
field namedscore
to theArcadeFlyerGame
class - After the declaration, set the
score
field to0
private int score = 0;
- Find the
Update
method in theArcadeFlyerGame
class - Find the
if
statement that checks whether an enemy has collided with a player projectileif (enemy.Overlaps(p))
- In the body of that
if
statement, increment the value ofscore
by 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
spriteBatch
field in theArcadeFlyerGame
class, add a newprivate SpriteFont
field namedtextFont
private SpriteFont textFont;
- Find the
ArcadeFlyerGame()
constructor - Somewhere in the method, initialize the
textFont
field usingContent.Load
textFont = Content.Load<SpriteFont>("Text");
- Find the
Draw
method - Toward the end of the method, call the
spriteBatch.DrawString
method- The first parameter should be a
SpriteFont
object - usetextFont
- The second parameter should be a
string
value - create a string that has both thelife
andscore
values - The third parameter should be a
Vector2
object for position - use theVector2.Zero
for the upper left - The fourth parameter should be a
Color
object - 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.