XNA开发实用教程——添加文字
来源:互联网 发布:amd怎么优化显卡性能 编辑:程序博客网 时间:2024/06/08 10:58
三峡大学土木水电学院肖泽云
本教程的主要目的是让你看完后,真正体会一下什么是XNA?XNA中主要包括哪些部分?相信你自己,在看完整个教程后,你也能设计自己的三维场景!祝你成功! 三、添加文字 [TextWindowsGame]
添加文字前需要先新建或添加一个spritefont,是一个XML文件。在解决方案资源管理器中Content目录上点击右键选择“Add”—“新建项…”,新建一个spritefont,例如其名称为SpriteFont1.spritefont。如下图所示。
1、打开SpriteFont1.spritefont文件。
在<FontName>SpriteFont1</FontName>中间的SpriteFont1即为字体的名称,如更改为Arial。<Size>14</Size>中间的14位字体的大小,可以更改。
2、在Game1.cs文件中,首先全局定义:
SpriteFont myFont;
3、在LoadContent中添加:
myFont = Content.Load<SpriteFont>("SpriteFont1");
其中"SpriteFont1"即为SpriteFont1.spritefont文件的Asset name。
4、在Draw函数中添加:
spriteBatch.DrawString(myFont, "Hello!Dream Xiao", new Vector2(100, 100), Color.GreenYellow);
其结果如下图所示:
整个程序的代码如下:
#region Using Statements //引用
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace TextWindowsGame
{
public class Game1 : Microsoft.Xna.Framework.Game //继承Game类
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont myFont;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize() //初始化
{
base.Initialize();
}
protected override void LoadContent() //导入目录,每次游戏启动时都会启动
{
// 创建一个精灵,用于绘制图片
spriteBatch = new SpriteBatch(GraphicsDevice);
myFont = Content.Load<SpriteFont>("SpriteFont1");
}
protected override void UnloadContent() //卸载目录
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime) /// 更新。用于检测碰撞、输入等
{
// 设置游戏结束事件
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//添加更新的对象代码
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) //当绘制时被调用
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// 添加绘图代码
spriteBatch.Begin();
spriteBatch.DrawString(myFont, "Hello!Dream Xiao", new Vector2(100, 100), Color.GreenYellow);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
- XNA开发实用教程——添加文字
- XNA开发实用教程——图片
- XNA开发实用教程——键盘输入事件
- XNA开发实用教程——游戏最基本代码
- XNA开发实用教程——游戏窗体设置
- XNA开发实用教程——三维模型的导入及操作
- XNA游戏开发之(七)——XNA游戏开发框架解析
- XNA游戏开发之(五)——XNA实现组件复用
- XNA游戏开发之(二)——IsFixedTimeStep
- XNA游戏开发之(三)——使用IsRunningSlowly
- XNA游戏开发之(四)——改变Draw频率
- XNA游戏开发之(六)——保存游戏数据
- XNA 3D文字
- Word实用教程——五分钟教你如何在任意页开始添加页码
- Word实用教程——五分钟教你如何在任意页开始添加页码
- XNA Arcade Alpha1—XNA游戏发布软件
- XNA游戏——泡泡堂
- 《嵌入式Linux开发实用教程》
- Flex for ArcGIS开发
- Flex与WebService通信
- Flex开发实例下载——个人学习心得
- XNA开发实用教程——游戏最基本代码
- XNA开发实用教程——图片
- XNA开发实用教程——添加文字
- XNA开发实用教程——游戏窗体设置
- XNA开发实用教程——三维模型的导入及操作
- XNA开发实用教程——键盘输入事件
- XNA开发实用文档下载地址
- 3DSTATE for Visual basic.Net开发(一)
- 3DSTATE for Visual basic.Net开发(二)
- 3DSTATE for Visual basic.Net开发(三)
- 3DSTATE for Visual basic.Net开发(四)