Game Development Reference
In-Depth Information
alternatively check if the bullet has left the screen. In both cases we want to destroy
the bullet; however, if the bullet hits an enemy we want to damage the enemy us-
ing the damage value specified by
Bullet::Damage
. The code inside
Enemy
will
handle destruction if it runs out of health. In this example each bullet instantly kills;
however, if you remember, when we created each bullet we set the
Damage
value.
Feel free to tweak the value as well as the health of the enemy to find the balance
you want.
Now you should be able to run the game and see the bullets emerge from the player
and hit the enemies, destroying them. We have gameplay in place, but these kinds
of games really rely on one other little feature to make them better: Scoring.
Now that we have everything in place, implementing scoring is really easy. We need
to track the score, and add/remove from it based on different events that happen in
the game, as well as display the score by adding some text rendering from
Chapter
First we need to add an integer score variable to
Game
; I named mine
_playerS-
core
. Ensure you set this to zero in the constructor so the player doesn't start with a
random score from some uninitialized memory—that would be confusing. There are
three points at which the player can gain or lose score, and they all happen after a
collision, so the best place to adjust the score is inside
ProcessCollisions
.
Here you can choose your own values but, as an example, if the enemy collides with
the player we levy a penalty of 1000 points. If the enemy exits the screen on the left
the penalty is 100 points, and if the player destroys an enemy we award him or her
500 points. We'll use a helper method called
AddScore
to allow us to manage this
by passing positive or negative amounts to be added to the score. This allows us to
separate out the score update so that we can be prepared for the next part.
Now we need to display this score to the player, to give them some feedback so they
know where they stand. First we need to add some text functionality to the renderer.
To do this, we're going to create a new class named
TextBlock
, which will store
the
Text
,
Position
, and
Color
that will be used along with the
SpriteFont
to
draw the string we want.
class TextBlock
{