Apr 8, 2017 11:27 PM
(This post was last modified: Apr 9, 2017 03:49 AM by DatFroschlaich.)
I will post some interesting code snippets here from time to time which you can use in your Monogame or XNA game projects. These pieces of code helped myself a lot in the past.
Eventually I will start making a tutorial here about coding a game with C# and XNA.
Let's start with centering text:
Eventually I will start making a tutorial here about coding a game with C# and XNA.
Let's start with centering text:
Code
//At first you have to load a sprite font, in my project it's called "Font"
SpriteFont SF = Content.Load<SpriteFont>("Font");
//you can create fonts via the MG Pipeline
//The string that will be drawn
string Text = "some nice text";
//The position of the drawn string
//This will be centered exactly at the middle of the game window
float x = GraphicsDevice.Viewport.Width / 2 - SF.MeasureString(Text).X / 2;
float y = GraphicsDevice.Viewport.Height / 2 - SF.MeasureString(Text).Y / 2;
Vector2 Position = new Vector2(x, y);
//put the whole code above in the LoadContent method or, if you have one in your class, the constructor.
//Then we can finally draw our string
//I will use the standard SpriteBatch parameter of the Draw method
spriteBatch.Draw(SF, Position, Text, Color.White);
//this code has to be put into the Draw method

![[Image: Syr3DlV.png]](https://i.imgur.com/Syr3DlV.png)