在上篇博文里面 我学习了如何创建第一个WP7手机程序和silverlight的一些知识。今天这篇博文将提到怎么样创建一个XNA的应用程序,并输出Hello, Windows Phone 7!

--------内容开始---------

一个XNA的应用程序

创建一个XNA的程序与创建手机程序基本相同,选择Visual C#和XNA Game Studio 4.0,然后修改路径和程序名称。创建成功后你会发现VS为我们创建了两个项目。一个是XNA的项目一个是这个项目的内容。XNA程序通常包含的很多内容,主要是位图和三维模型,以及字体。下面我们将为XNA程序添加一个新的字体。首先右击这个项目的内容,这里我的为WindowsPhoneGame1Content,然后选择ADD(添加)-->new item 然后选择sprite font。然后点击ADD。完成之后你将会看到一个XML文件。你可以编辑它。

首先第一个标签<FontName>。你可以替换成下列字体:

Kootenay Lindsey Miramonte Pescadero  Miramonte Bold   Pescadero Bold  Pericles   Segoe UI Mono  Pericles Light  Segoe UI Mono Bold

其中Pericles 字体一般只试用于header。

<CharacterRegions>标签内表示Unicode字符十六进制编码。默认设置0x320x126包括所有的ASCII字符集的非控制字符。

在XNA项目中,我们可以发现两个.cs文件。比如我的是Program.cs 和 Game1.cs. Program.cs 和 Windows Phone 7 games没有关系。

大多数时间,我们都将修改 Game1.cs。

首先class Game1继承 Microsoft.Xna.Framework 命名空间下的Game类。具体类的成员 查看http://msdn.microsoft.com/zh-cn/library/microsoft.xna.framework.game_members.aspx

当类初始化后,会调用LoadContent()方法设置字体等其他内容。然后我们通过下面的代码来加载我们设置的字体,以及他的位置。

newFont = this.Content.Load<SpriteFont>("SpriteFont1");
Vector2 textSize = newFont.MeasureString(text); //作为向量返回字符串的长和宽
textPosition = new Vector2((viewport.Width - textSize.X) / 2,
                (viewport.Height - textSize.Y) /2);

之后在把它画出来.我们在Draw方法中添加如下的代码就可以了:

spriteBatch.Begin();
spriteBatch.DrawString(newFont, text, textPosition, Color.White);
spriteBatch.End();

它的完整代码如下:

namespace WindowsPhoneGame1
{

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
string text = "Hello, Windows Phone 7!";
SpriteFont newFont;
Vector2 textPosition;

public Game1()
{
graphics
= new GraphicsDeviceManager(this);
Content.RootDirectory
= "Content";

// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
}

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

newFont
= this.Content.Load<SpriteFont>("SpriteFont1");
Vector2 textSize
= newFont.MeasureString(text);
Viewport viewport
= this.GraphicsDevice.Viewport; //获取屏幕大小
textPosition = new Vector2((viewport.Width - textSize.X) / 2,
(viewport.Height
- textSize.Y) /2);

// TODO: use this.Content to load your game content here
}

protected override void UnloadContent()
{

// TODO: Unload any non ContentManager content here
}

protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.DrawString(newFont, text, textPosition, Color.White);
spriteBatch.End();
// TODO: Add your drawing code here

base.Draw(gameTime);
}
}
}

这样我们运行我们的程序,和WP7的操作步骤一样。是不是看到了Hello, Windows Phone 7!的输出。

(二)完。

posted on 2011-03-25 16:00  confusing  阅读(341)  评论(0编辑  收藏  举报