Game Development Reference
In-Depth Information
public bool GameOver
{
get { return (timeLeft.Ticks <= 0); }
}
Now, the only thing we still need to do is implement the Update method to program
the timer behavior. As a first step, we only update the timer if it is running. So if the
timer is not running, we return from the method:
if (!running)
return ;
Then, as usual we subtract the elapsed game time from the current remaining time:
= gameTime.ElapsedGameTime;
timeLeft
Now, we create the text that we want to print on the screen. For that, we can use the
formatting methods of the DateTime class. We also set the color of the text to yellow,
so that it better fits the design of the game:
DateTime timeleft = new DateTime(timeLeft.Ticks);
this .Text = timeleft.ToString("mm:ss");
this .color = Color.Yellow;
As you can see, the DateTime class has a special To S t r i n g method, which takes a
formatting string as a parameter. In this case, we allow two digits for the minutes
and two digits for the seconds, separated by a colon.
Finally, we want to warn the player if he/she does not have a lot of time left to
finish the level. We do this by alternating between a red and a yellow color when
printing the text on the screen. This we can do using an if -instruction and smartly
using the modulus operator.
if (timeLeft.TotalSeconds <= 10 && ( int )timeLeft.TotalSeconds % 2 == 0)
this .color = Color.Red;
30.2.1 Making the Timer Go Faster or Slower
Depending on the kind of tile the player is walking on, the time should go faster or
slower. Walking on a hot tile will increase the speed at which time passes, walking
on an ice tile will decrease it. In order to allow for a timer running at different speeds,
we introduce a multiplier valueinthe TimerGameObject class. We gain access to this
multiplier using a property:
Search WWH ::




Custom Search