You can now display text on
the screen in a variety of sizes and fonts. You could use this to write
a program that simply displays messages on the screen. Next, you need a
way to determine the correct time for the clock in your program to
display. The PC, the Zune, and the Xbox have internal clock hardware
that is used by some games to change the way they play so that, for
example, if it’s dark outside, it’s dark in the game as well. To
accomplish this, the XNA Framework must provide a way of finding the
date and time.
The date and time values are held in a special structure called DateTime. You already know that XNA provides types that are tailored to different needs. You’ve seen the Color type, the Texture2D type, and the SpriteFont type, to name a few. The DateTime
type holds all the information about the date and time of a particular
instance. The structure is not part of XNA as such; rather, it’s part of
the Microsoft .NET Framework, which provides resources to all C#
programs. Thus, when you want to manipulate dates and times in a C#
program running on a Windows operating system, you can do it in exactly
the same way.
For your clock, you need a DateTime structure that’s set to the current date and time. It turns out that DateTime provides a property that creates one for you. A property is a value or setting that an object in a C# program can expose for you to use. You’ve already seen these; when you used Color.CornflowerBlue, you were asking the Color structure to give you a color that represents that shade of blue. You use DateTime.Now
in the same way. Later, when you start using structures and classes to
design more complicated game programs, you’ll get more of an insight
into how all this works. For now, you simply get a DateTime value that holds the current time and use that to drive your clock, as follows:
DateTime nowDateTime = DateTime.Now;
The Now property of the DateTime
structure is always set to the current date and time. This works by
taking values from an internal hardware clock, which means that after a
while, the value will be out of date. In fact, you could use a DateTime variable to record the time at which the game was started.
Once you have your DateTime variable, you can ask it to do things for you. One thing it can do is give you a string that contains the time in text form:
DateTime nowDateTime = DateTime.Now;
string nowString = nowDateTime.ToLongTimeString();
These two statements create a variable of type DateTime,
which holds the current date and time, and then use this to create a
string. A string does exactly what you would expect, it holds a string
of text. The DateTime structure contains a method with the identifier ToLongTimeString.
You know that objects contain methods; this method has the job of
converting the date and time information inside the object into a string
that you can put on the screen in text form. In fact, DateTime provides several methods that you can use (see Table 1).
Table 1. Some DateTime String Methods
Method Call | Output |
---|
ToLongTimeString() | 20:23:55
|
ToShortTimeString() | 20:23
|
ToLongDateString() | 16 March 2009
|
ToShortDateString() | 16/03/2009
|
ToString() | 16/03/2009 20:23:55
|
We have previously
considered different types in C# as offices. You can think of these
methods as a number of different people sitting in the DateTime office,
all of whom have their own telephone and can be asked to deliver an
appropriately formatted string of text. You can call any of these
methods to get a string of text that describes the value being held by
the variable nowDateTime. You can use them to add the date and time to your clock if you wish.
Note:
The precise format of the date and time produced depends on the localization
of your system. Most software products are configured to display the
date and time in a manner in keeping with the country where they are
being used. The previously given samples are for a Windows PC used in
England. Yours might look slightly different.
Putting all this together, you can create a version of the Draw method that displays the current time on your screen:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
DateTime nowDateTime = DateTime.Now;
string nowString = nowDateTime.ToLongTimeString();
Vector2 nowVector = new Vector2(50, 400);
spriteBatch.Begin();
spriteBatch.DrawString(font, nowString, nowVector, Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
I’ve changed the name of the vector to nowVector to better describe what it is used for. I’ve also moved the draw position so that if you set the font size to 100, this Draw
method gives you a big clock on your Xbox that nicely fills the bottom
of the screen. If you want to display the clock on a Zune I’ve found
that with a font size of 25 you can use an X position of 50 and a Y
position of 280.
Because the Draw and Update methods are called automatically for you by the XNA environment, the clock is repeatedly redrawn with the up-to-date time.