Time for action – drawing the score
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);
What just happened?
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()
)...