Game Development Reference
In-Depth Information
few instructions in that method to deal with this. The only thing we need to do is
check if the color of the paint can is the same as its target color when it falls through
the bottom of the screen. If that is the case, we decrement the lives counter in the
GameWorld class through the Lives property:
if (Painter.GameWorld.IsOutsideWorld(position))
{
if (color != targetcolor)
Painter.GameWorld.Lives
−− ;
Reset();
}
As you can see, we use the −− operator here to decrease the number of lives by 1,
but we also could have written a longer version as follows (more about that later):
1;
Painter.GameWorld.Lives = Painter.GameWorld.Lives
9.2.2 Indicating the Number of Lives to the Player
Obviously, the player would like to know how he/she is doing. So we have to indi-
cate somehow on the screen how many lives the player still has. In the Painter game,
we do that by displaying a number of balloons in the left top corner of the screen.
Using the knowledge that we have, we could use an if -instruction for that:
if (lives == 5)
{
Drawtheballoonsprite5timesinarow
}
else if (lives == 4)
{
Drawtheballoonsprite4timesinarow
}
else if (lives == 3)
Andsoon...
This is not a very nice solution. It leads to a lot of code, and we would have to copy
the same instruction a lot of times. Fortunately, there is a better solution for that:
iteration .
9.3 The while -Instruction: Executing Instructions Multiple
Times
Iteration in C# is a way to repeat instructions a number of times. Have a look at the
following code fragment:
Search WWH ::




Custom Search