Game Development Reference
In-Depth Information
We update the score in the PaintCan class, where we can check if the can falls
outside of the screen. If so, we check if it has the right color and update the score
and the number of player lives accordingly. Finally, we reset the paint can object so
that it can fall down again:
if (Painter.GameWorld.IsOutsideWorld(position))
{
if (color == targetcolor)
{
Painter.GameWorld.Score += 10;
collectPoints.Play();
}
else
Painter.GameWorld.Lives
−−
;
Reset();
}
minVelocity += 0.001f;
Whenever a can of the right color falls off the screen, we play a sound. This sound
is again a SoundEffect instance that is passed along as a parameter in the constructor
and stored as a member variable in the PaintCan class.
11.5 Drawing Text on the Screen
Next to drawing the sprites on the screen, we also want to draw the current score on
the screen (otherwise it would not make much sense to maintain it). For this, we can
use the DrawString method. Instead of drawing a sprite on the screen, this method can
draw text on the screen. Just like the Draw method from the SpriteBatch class requires
a sprite, the DrawString method requires a font . This font is loaded just like a sprite,
in the GameWorld constructor, and it is stored in a variable of type SpriteFont :
gameFont = Content.Load<SpriteFont>("GameFont");
The file describing the font is located in the same place as the sprites: in the con-
tent project. Open the Painter content project and double click on the file 'Game-
Font.spritefont'. You will see a text file containing a lot of information describing
the font. In this file, you can change the size and type of the font, among other things.
Try, for example, to change the size to 30 and run the game again to see what hap-
pens. Or try to change the font name to 'Courier New' or 'Times New Roman'. You
can add your own fonts to the content project by right clicking on the project name
in the solution explorer and choosing Add New Item . Then, choose the template
called 'SpriteFont' and type an appropriate name. When you click 'Add', the font
file is created for you and it is added to the content project. After that, you can
modify the font file as desired and use it in your game application.
Drawing text on the screen is quite easy using the DrawString method. For exam-
ple, this draws some green text in the top left of the screen:
 
Search WWH ::




Custom Search