programming4us
programming4us
MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Getting the Date and Time

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/14/2011 4:24:32 PM

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 CallOutput
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.
Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us