次の方法で共有


Game1 クラスの作成

すべての Microsoft XNA プロジェクトと同様に、Game1 クラスは、基本的なグラフィック デバイスの初期化、ゲーム ロジック、XNA ゲームのレンダリング コードを提供する Microsoft.Xna.Framework.Game クラスから派生します。 ほとんどの処理は GamePiece クラスと GamePieceCollection クラスで実行されるため、Game1 クラスはきわめて単純です。

コードの作成

クラスのプライベート メンバーは、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 オブジェクトに割り当てられます。 2 種類のゲーム ピースがあります。 ピースのスケール ファクターは少し変化し、小さいピースや大きいピースが存在するようにします。

#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

ゲームの実行中、Update メソッドは XNA Framework によって繰り返し呼び出されます。 Update メソッドは、ゲーム ピース コレクションで ProcessInertia メソッドと UpdateFromMouse メソッドを呼び出します。 これらのメソッドについては、「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

ゲームの実行中、Draw メソッドも XNA Framework によって繰り返し呼び出されます。 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 クラスの作成

完全なコードの一覧