- Add a new
Vector2
to the declarations area of Game1 to store the screen location where the score will be drawn:Vector2 scorePosition = new Vector2(605, 215);
- In the
Draw()
method, remove "this.Window.Title = playerScore.ToString();
" and replace the line with:spriteBatch.DrawString(pericles36Font, playerScore.ToString(), scorePosition, Color.Black);
Using named vectors to store things like text positions, allows you to easily move them around later if you decide to modify the layout of your game screen. It also makes code more readable, as we have the name scorePosition
instead of a hard-coded vector value in the spriteBatch.DrawString()
call. Since our window size is set to 800 by 600 pixels, the location we have defined above will place the score into the pre-defined score box on our background image texture.
The DrawString()
method accepts a font to draw with (pericles36Font
), a string to output (playerScore.ToString()
), a Vector2 specifying the upper left corner of the location to begin drawing (scorePosition
), and a color for the text to be drawn in (Color.Black
).
Simply drawing the player's score is not very exciting, so let's add another use for our SpriteFont. In some puzzle games, when the player scores, the number of points earned is displayed in the center of the screen, rapidly growing larger and expanding until it flies off of the screen toward the player.
We will implement this functionality with a class called ScoreZoom that will handle scaling the font.