Welcome

首页 / 软件开发 / .NET编程技术 / XNA基础(01) 游戏循环

XNA基础(01) 游戏循环2011-09-26 博客园 zhuweisky当安装好了VS 2008和XNA GameStudio 3.0后,我们就可以开始学习XNA了。

首先,在VS 2008中新建一个XNA GameStudio 3.0项目(选择Windows Game类型),会生成一个最简单 的、可运行的游戏模板。

接下来我们将注意力转移到我们要剖析的重点 —— 从Microsoft.Xna.Framework.Game继承的Game1 类,其代码如下:

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

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

protected override void Initialize()
{
base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

protected override void UnloadContent()
{
}

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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

base.Draw(gameTime);
}
}

我们简单解释一下该类中所用到的重要类型。

GraphicsDeviceManager 图形设备管理器,用于访问图形设备的通道。

GraphicsDevice 图形设备。

Sprite 精灵,绘制在屏幕上的的2D或3D图像,比如游戏场景中的一个怪兽就是一个Sprite。

SpriteBatch 它使用同样的方法来渲染一组Sprite对象。