다음을 통해 공유


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 개체에 할당됩니다. 두 가지 유형의 게임 피스가 있습니다. 게임 피스의 배율 계수는 조금씩 변경되기 때문에 보다 작은 게임 피스와 보다 큰 게임 피스가 있습니다.

#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 메서드는 게임 피스 컬렉션의 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

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 클래스 만들기

전체 코드 목록

기타 리소스

조작 및 관성