Share via


Creating the Game1 Class

As with all Microsoft XNA projects, the Game1 class derives from the Microsoft.Xna.Framework.Game class, which provides basic graphics device initialization, game logic, and rendering code for XNA games. The Game1 class is fairly simple because most of the work in done in the GamePiece and GamePieceCollection classes.

Creating the Code

The private members for the class consist of a GamePieceCollection object to hold the game pieces, a GraphicsDeviceManager object, and a SpriteBatch object used to render game pieces.

#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

During game initialization, these objects are instantiated.

#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

When the LoadContent method is called, the game pieces are created and assigned to the GamePieceCollection object. There are two types of game pieces. The scale factor for the pieces is changed slightly so that there are some smaller and some larger pieces.

#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

The Update method is called repeatedly by the XNA Framework while the game is running. The Update method calls the ProcessInertia and the UpdateFromMouse methods on the game piece collection. These methods are described in Creating the GamePieceCollection Class.

#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

The Draw method is also called repeatedly by the XNA Framework while the game is running. The Draw method performs the rendering of game pieces by calling the Draw method of the GamePieceCollection object. This method is described inCreating the GamePieceCollection Class.

#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

See Also

Concepts

Using Manipulations and Inertia in an XNA Application

Creating the GamePiece Class

Creating the GamePieceCollection Class

Full Code Listings

Other Resources

Manipulations and Inertia