XNA-2D-drawing text and images

Source: Internet
Author: User

In xNa, 2D plotting is mainly completed through spritepatch, Sprite. drawstring () is used to draw text, and Sprite. Draw () is used to draw 2D images and materials (texture ).

1. Draw text in xNa

1. In the xNa windows game project, add a fonts folder for contentpipeline, and then add the font definition file: add...> new item... -> Select sprite font and name it "msyahei. spritefont ":

2. Open the new spritefont (actually an XML file) and modify the fontname node

 
1:<Fontname>Microsoft yahei</Fontname>

Microsoft yahei is the name of a font that has been installed in the system. It can be found in the Windows/fonts directory. This name is not the name of the font file, the font name displayed in the fonts directory.

3. Add a member variable to the game class:

1: spritefont fontmsyahei;

4. Load the font in the loadcontent () method:

 
1: fontmsyahei = This. content. Load<Spritefont>(@ "Fonts \ msyahei ");

5. Use the loaded font to draw text in the draw () method:

 
1: String text = "Hello, xNa! ";
 
2: spritebatch. Begin ();
3: spritebatch. drawstring (fontmsyahei, text, new vector2 (25f, 25f), color. Red );
 
4: spritebatch. End ();
2. Draw images in xNa

1. Add a folder images in contentpipeline and then add the image file:

In the two images, xna.png has a white background, while xna_trans.png is transparent.

2. Add two member variables to the game class to load the image:

 
1: texture2d image;
2: texture2d imagetrans;

3. Load the image in the loadcontent () method:

 
1: Image = content. Load<Texture2d>(@ "Images \ xNa ");
 
2: imagetrans = content. Load<Texture2d>(@ "Images \ xna_trans ");

4. Draw the loaded image in the draw () method:

1: spritebatch. Begin ();
 
2: spritebatch. Draw (image, new vector2 (25, 70), color. White );
 
3: spritebatch. Draw (imagetrans, new vector2 (25,250), color. White );
 
4: spritebatch. End ();
Iii. Running result:

4. tips:

XNa game does not display the mouse pointer by default. to display the pointer, you can set the attribute in the game constructor:

1: ismousevisible = true;

If you want to resizeProgramWindow to set properties:

 
1: window. allowuserresizing =True;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.