The MonoGame Game1 Class

MonoGame

MonoGame is a framework that is based on the XNA framework by Microsoft. It is, free, open source and cross platform. MonoGame targets Windows, Mac, Linux Android and other platforms. On the MonoGame Tutorials page of my blog I have a number of tutorials. I also of series of tutorials on creating specific game types on other pages of the blog.

MonoGame’s Game1 Class

The heart of a MonoGame game is the Game1 class. It comes with several methods that are standard to all games. They include Initialize, LoadContent, Update, Draw and UnloadContent as seen in this basic Game class.

    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

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

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

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
        }
    }
}

MonoGame’s Initialize Method

The Initialize method is where you initialize fields and properties, as the name suggests. This is where you can set up display properties, load non-content related data like game settings. MonoGame calls the method once at the start of the game once the constructor has finished.

MonoGame’s LoadContent Method

The LoadContent method is where you load your game assets. It uses the Content Manager to load content that is built using the Content Pipeline. The Content Pipeline takes your game assets and packages them into a format that is read in at run time. It simplifies the process of loading game related assets.

MonoGame’s Update Method

Update handles all game related logic. By default MonoGame tries to call the Update 60 times per second, or 60 frames per second. Here you would handle game input and update all entities in your game. You should not perform any rendering in the Update method.

MonoGame’s Draw Method

Draw renders the game world. Like Update, MonoGame tires to call Draw 60 times per second. You should not perform any update logic in the Draw method and reserve it solely for rendering your world.

MonoGame’s UnloadContent Method

The UnloadContent method is called when the game exits. Here is where you unload any non-Content Manager content and clean up after yourself.

GameComponents and DrawableGameComponents in MonoGame

These are not part of Game1 class but are used in the Game1 class. A GameComponent is similar to the Game1 class. It is an Initialize and Update method. A DrawableGameComponent also has LoadContent, UnloadContent and Draw methods. When these are added to the list of GameComponents, a property of the Game1 class. Their methods are called whenever the base method is called. For example, calling base.Update calls the Update method of all GameComponents in the list of components.