创建 Game1 类

像所有 Microsoft XNA 项目一样,Game1 类派生自 Microsoft.Xna.Framework.Game 类,该类提供 XNA 游戏的基本图形设备初始化、游戏逻辑和呈现代码。 Game1 类相当简单,这是因为大多数工作已在 GamePiece 和 GamePieceCollection 类中完成。

创建代码

该类的私有成员包括用于保存游戏块的 GamePieceCollection 对象、GraphicsDeviceManager 对象,以及用于呈现游戏块的 SpriteBatch 对象。

#region PrivateMembers
// Number of game pieces.
private const int GamePieceCount = 6;
// The collection of game pieces.
private GamePieceCollection faces;
// Graphics device manager.
private GraphicsDeviceManager graphics;
// The sprite batch used for rendering game pieces.
private SpriteBatch spriteBatch;
#endregion

在游戏初始化期间,对这些对象实例化。

#region ConstructorInitialize
public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    // This is the default but assigning here explicitly
    // to show that resizing is not supported. The view port
    // boundaries used to bounce a game piece would not be
    // updated if the window was resized.
    Window.AllowUserResizing = false;
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// </summary>
protected override void Initialize()
{
    IsMouseVisible = true;
    faces = new GamePieceCollection();
    // base.Initialize calls the LoadContent method.
    base.Initialize();
}
#endregion

当调用 LoadContent 方法时,会创建游戏块,并将其分配给 GamePieceCollection 对象。 有两种类型的游戏块。 游戏块的缩放比例稍微进行更改,以便有一些小游戏块和大游戏块。

#region LoadContent
/// <summary>
/// LoadContent will be called once per game. Load all content here.
/// </summary>
protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);

    string filename = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    string path = System.IO.Path.GetDirectoryName(filename) + @"\Content";

    // Scale pieces from 100% to 160%
    float scale = 1.0f;
    float scaleFactor = 0.60f / ((GamePieceCount/2)-1);
    for (int k = 0; k < GamePieceCount / 2; k++)
    {
        GamePiece face1 = new GamePiece(spriteBatch, path + @"\Face1.png");
        GamePiece face2 = new GamePiece(spriteBatch, path + @"\Face2.png");

        face1.Scale = face2.Scale = scale;
        face1.PieceColor = Color.Green;
        face2.PieceColor = Color.LightSalmon;
        faces.Add(face1);
        faces.Add(face2);
        scale += scaleFactor;
    }
}
#endregion

当游戏运行时,XNA Framework 重复调用 Update 方法。 Update 方法对游戏块集合调用 ProcessInertiaUpdateFromMouse 方法。 创建 GamePieceCollection 类中对这些方法进行了介绍。

#region UpdateGame
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
    faces.ProcessInertia();
    faces.UpdateFromMouse();
    base.Update(gameTime);
}
#endregion

当游戏运行时,XNA Framework 还重复调用 Draw 方法。 Draw 方法通过调用 GamePieceCollection 对象的 Draw 方法,执行游戏块的呈现。 创建 GamePieceCollection 类中对这一方法进行了说明。

#region DrawGame
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    faces.Draw();
    spriteBatch.End();
    base.Draw(gameTime);
}
#endregion

请参见

概念

在 XNA 应用程序中使用操作和惯性

创建 GamePiece 类

创建 GamePieceCollection 类

完整代码清单

其他资源

操作和惯性