How to Use SpriteFont in XNA

Written by faith chandler
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use SpriteFont in XNA
Textures are applied in movie animations, video and computer games. (Ryan McVay/Photodisc/Getty Images)

The basic game framework XNA developed by Microsoft is used to make games on all their current platforms, such as Windows and Xbox. XNA is designed to work with the programming language C# in Visual Studio. Users can use free express or paid professional versions of XNA. Both versions support all code and programming, including spritefonts. A spritefont is the framework's way of handling text drawn onto the screen or an image.

Skill level:

Other People Are Reading

Things you need

  • XNA software
  • C# software
  • Computer

Show MoreHide


  1. 1

    Open C# Express by clicking "File" and "New Project." Select the version of XNA to use such as XNA 4.0, and select "Windows Game" in the form. Name the project and choose the project directory then click "Create." This will create a game template with all the libraries and basic structure pre-made.

  2. 2

    Create your variables. Put the code in the "game1" class near the top of the input screen. This will allow you to find them easier and let all functions access them.

    SpriteFont MessageFont;

    String message = "Hello!";

  3. 3

    Load the font, but first you will need to add it to your project. Look for the "Project Directory Listing" on the edge of the window. Right-click on the "Content Directory" and select "Add Item." A new window will pop up. Choose "Sprite Font." The default name is "SpriteFont1.spritefont." Select "OK." This will bring up an XML document. You should see the tags "<FontName>Kootaney</FontName>" in the XML. The middle is the system font to be used. You can change it to "Arial," for example, by replacing "Kootaney" with "Arial."

  4. 4

    Go back to the "Game1.cs" tab and scroll down to the "LoadContent()" function. You will need to add the following code to load the spritefont:

    "MessageFont = Content.Load<SpriteFont>("Content/SpriteFont1");"

  5. 5

    Scroll down to the function "Draw()" and in between the two lines of code already there, add the following code:

            spriteBatch.DrawString(MessageFont, message, new Vector 2(20, 20), Color.Gold);
  6. 6

    Push "f6" to build the project. Press "f5" to test your file. A window will pop up displaying the word, "Hello!" You can change this message by replacing the "Hello!" in the variables code at the top of the screen with whatever text you want.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.