Game Development Reference
In-Depth Information
Chapter 30
Finishing the Game
30.1 Introduction
In this chapter, we will finish the Tick Tick game. First, we will add a timer such
that the player has a limited amount of time to complete each level. Then, we will
add a few mountains and clouds to the background to make the game visually more
interesting. Finally, we will discuss progressing through the levels by adding two
extra game states: the 'game over' state, and the 'level finished' state.
30.2 Adding a Timer
The first thing we will look at is adding a timer to the game. We do not want the timer
to take up too much screen space, so we opt for a text-version of the timer. Therefore,
the TimerGameObject class inherits from the TextGameObject class. We want to be able
to pause the timer (for example, when the level is finished), so we add a boolean
variable running that we can get and set through a property Running . Furthermore,
we store the time that is still left in a TimeSpan object. This class is not only useful
for representing a timespan, but it also includes convenient methods for converting
the time span into a string and more. We override the Reset method to initialize the
object. We want to give the player 30 seconds to finish each level. We use the static
method FromMinutes in the TimeSpan class to create a TimeSpan instance for us:
public override void Reset()
{
base .Reset();
this .timeLeft = TimeSpan.FromMinutes(0.5);
this .running = true ;
}
For convenience, we also add a property GameOver that indicates if the timer has
reached zero. We will use this property later on to handle the event that the player
does not finish the level in time:
 
Search WWH ::




Custom Search