Game Development Reference
In-Depth Information
Chapter 9
A Limited Number of Lives
9.1 Introduction
In this chapter, we are going to make the game more interesting by giving the player
a limited number of lives. If the player misses too many paint cans, she/he dies. In
this chapter, we will discuss how to deal with that, and how to display the current
number of lives to the player. In order to do the latter, we introduce a few program-
ming constructs for repeating a group of instructions several times.
9.2 The Number of Lives
9.2.1 Maintaining the Number of Lives
In order to introduce some danger in the game, we would like to limit the number
of paint cans in the wrong color that the player can allow to fall through the bottom
of the screen. In the
Painter8
example, you can see this effect worked out. We set
this limit to 5. In order to store this limit, we add an extra member variable to the
GameWorld
class, as well as a property for modifying it:
int
lives;
public int
Lives
{
get
{
return
lives; }
set
{ lives =
value
;}
}
We initially set this value to 5 in the constructor of the
GameWorld
class. Now we
can update this value whenever a paint can falls outside of the screen. We perform
this check in the
Update
method of the
PaintCan
class. Therefore, we have to add a