Slateboard
Slateboard

Reputation: 1031

How do I display Numbers and Text in C# with XNA?

I am working on a pong clone, and i want to display the player scores onscreen. I don't know how to display it.

Upvotes: 2

Views: 11841

Answers (3)

Sekhat
Sekhat

Reputation: 4479

The SpriteBatch object has a DrawString method that takes:

  • a SpriteFont which can be created in your content project and loaded via content.Load
  • the string you wish to write to the screen
  • a Vector2 of the position that you want to draw the text at
  • the Color you wish the text to be.

So for example your draw method might look like this:

public void Draw()
{
    spriteBatch.Begin();

    DrawPaddles(spriteBatch);
    DrawBall(spriteBatch);

    // this being the line that answers your question
    spriteBatch.DrawString(scoreFont, playerScore.ToString(), new Vector2(10, 10), Color.White);

    spriteBatch.End();
}

See http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.drawstring.aspx

Upvotes: 12

Andrew Russell
Andrew Russell

Reputation: 27215

MSDN has you covered: How To: Draw Text

Upvotes: 1

Tergiver
Tergiver

Reputation: 14517

You should start your XNA journey at the XNA Creators Club. Even the most basic tutorials output text.

The XNA Forums are a better resource for XNA-related questions.

Upvotes: 4

Related Questions