Time for action – drawing the score
Add a new
Vector2
to the declarations area ofGame1
to store the screen location where the score will be drawn:Private scorePosition as Vector2 = new Vector2(605, 215)
In the
Draw()
method, removeMe.Window.Title = playerScore.ToString()
and replace the line with the following: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 hardcoded vector value in the spriteBatch.DrawString()
call. Since our window size is set to 800 x 600 pixels, the location we have defined previously 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...