MonoGame Tutorial: Building a 2D Game Using C#

By | April 16, 2017

Part 2 – Basic structure of a MonoGame Program (The game loop)

In Part 1 of this tutorial, we installed Visual Studio and MonoGame, and created the first version of our Bricks game. In this part, we’ll look at our project, and examine what’s already been created for us. In the Visual Studio Solution Explorer (upper right of the window), you will see a summary of the project that has been created. I’ve highlighted the three files we’ll be looking at in this section:
MonoGame Template Files
The first file, “Program.cs”, we’ll just take a very quick look at, because we won’t need to modify it. I just want to mention it in passing, since it plays an important role. This file has the main entry point that Windows uses to launch our game. That’s all it does. It gets loaded when our program runs, and calls our game Run method:

static void Main()
{
    using (var game = new Game1())
    game.Run();
}

We’ll Look at the “Content.mgcb” file in the next section. It’s used to manage our Content Pipeline. We’ll go over that later, but for now, just remember that you will use the content pipeline to make images, sounds and other assets available to your game.

The “Game1.cs” file contains the Game1 class, which contains the main game loop for your program. Let’s look at the methods in this class to see what they do. Everything we add to the project will be called from the methods in this class.

Here’s the Game1 constructor:

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
}

This short method provides two critical things needed by our game. First, it creates a GraphicsDeviceManager instance that we’ll use to interact with DirectX.

Second, it tells our program where to look for content (images, sounds, etc.) that we’ll be using in our game. By default, this will be a folder called “Content” under the root folder of our project.

Next, there is an Initialize method that is called once at startup, that we can use to perform any initialization needed. This isn’t used for loading graphics and sounds, and we won’t need it for this game:

protected override void Initialize()
{
    // TODO: Add your initialization logic here
    base.Initialize();
}

We’ll look at LoadContent method in the next section, when we discuss the content pipeline. But in general, we’ll use LoadContent to cache all our image and sounds assets into memory, so that they can be used quickly while the game is running.

There is also an UnloadContent method. You can use this method to unload assets that you didn’t load using the ContentManager. Since we will be using ContentManager to load our assets, we won’t need to use UnloadContent in our program.

We’ll use the remaining two methods the most: Draw and Update. MonoGame will continuously loop and call these two methods for us. We use the Draw method whenever we want to draw something on the screen.

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    // TODO: Add your drawing code here
    base.Draw(gameTime);
}

The gameTime property provides the elapsed time since the last update, and also allows us to determine if the game is running at too slow a frame rate, so we can take action. We won’t need that for this game, but it is available for when you develop more complex games. The GraphicsDevice.Clear statement is what caused the game to display the “CornflowerBlue” colored background in the game window.

The Update method will allow us to get inputs from the mouse and keyboard (and a controller, if your game supports one)

protected override void Update(GameTime gameTime)
{
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
        Exit();
    // TODO: Add your update logic here
    base.Update(gameTime);
}

You can see that all that the update method does is check if the Back button on a game controller, or the “Escape” key was pressed on the keyboard. If it was, the program exits. That’s why the program terminated when we pressed escape earlier when we ran the program.

In summary, our game will simply have to load any images and sound the program will need, and then handle Draw and Update events provided by MonoGame. Anything we want to display on the screen we must include in the Draw method. And we’ll get inputs and take actions whenever the Update method is called.
Game Loop
Now that we have the basic framework under our belts, let’s look at how MonoGame handles sound and graphics assets in Part 3

12 thoughts on “MonoGame Tutorial: Building a 2D Game Using C#

  1. Kiel97

    I was looking for a quick tutorial to dive into MonoGame. Thank you! This one is perfect! It’s very easy to understand basic concepts of game making.

    Reply
  2. puppy

    Excellent tutorial . Although XNA is no longer supported by MicroSoft, it’s a good framework for 2D game development.

    Reply
  3. Josh_Kb

    Thankyou,
    This has really helped me to grasp the basics of Monogame!

    Reply
  4. Rodhlann

    Exactly what I was looking for! I heard that Axiom Verge was built with Monogame and instantly wanted to figure out what I could do with it as well. Thanks for setting me on the right track with this quick tutorial 🙂

    Reply
  5. stan10785

    What a great tutorial! Very too the point with great code examples! Thoughts on doing another one on another game type?

    Reply
    1. admin Post author

      Glad it helped! I hope to do some more once my schedule frees up.

      Reply
  6. stan10785

    This was really great! I would love to see another – tile maps are always fun

    Reply
  7. Wayne

    Great tutorial! This really helped me understand the basics of XNA/MonoGame; which in turn will help me get past a hurdle with something else I was attempting.

    Also, being the audio nerd that I am I couldn’t help but play around with the panning option. Using what I had already learned in the tutorial, is was simple enough to make the brick breaking sound pan more left or right as you get farther from the center. Fun stuff!

    Reply
  8. JustDoIt

    The best XNA/MonoGame tutorial so far!!
    I`m so hyped about the game, that I would love to see how to make more levels with more difficulties.

    Great Job!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *