MonoGame Tutorial: Building a 2D Game Using C#

Part 4 – Loading Images and Sounds

Before we can use images and sounds in our game, we have to load them. An advantage of using the content pipeline and content loader, is that all of our images and sounds will be cached in memory, and can be used quickly when we need them.

In Part 3, we converted the images into a format that MonoGame can load. In this section, we’ll load them into our game.

Let’s create a new class we’ll call GameContent. This class will do all of the content loading for our game. To do this, right click on the Bricks project in the Solution Explorer, then click Add and Class from the drop-down menus:
Add new Class
In the Add New Item Dialog, enter “GameContent.cs” for the file name and click the Add Button:
Add GameContent.cs
Now that we’ve created the class, Visual Studio should have opened it in an edit window, and we are ready to add the code to it. Here’s the code that needs to be added to the GameContent class:

[code language=”csharp” highlight=”6,7,8,9,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44″]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Bricks
{
class GameContent
{
public Texture2D imgBrick { get; set; }
public Texture2D imgPaddle { get; set; }
public Texture2D imgBall { get; set; }
public Texture2D imgPixel { get; set; }
public SoundEffect startSound { get; set; }
public SoundEffect brickSound { get; set; }
public SoundEffect paddleBounceSound { get; set; }
public SoundEffect wallBounceSound { get; set; }
public SoundEffect missSound { get; set; }
public SpriteFont labelFont { get; set; }

public GameContent(ContentManager Content)
{
//load images
imgBall = Content.Load<Texture2D>("Ball");
imgPixel = Content.Load<Texture2D>("Pixel");
imgPaddle = Content.Load<Texture2D>("Paddle");
imgBrick = Content.Load<Texture2D>("Brick");

//load sounds
startSound = Content.Load<SoundEffect>("StartSound");
brickSound = Content.Load<SoundEffect>("BrickSound");
paddleBounceSound = Content.Load<SoundEffect>("PaddleBounceSound");
wallBounceSound = Content.Load<SoundEffect>("WallBounceSound");
missSound = Content.Load<SoundEffect>("MissSound");

//load fonts
labelFont = Content.Load<SpriteFont>("Arial20");

}
}
}
[/code]

Since we are going to use the MonoGame Framework, these “using” statements that we added tell Visual Studio how to resolve the names we’ll be using:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

But, wait a minute! I thought we were using MonoGame, not XNA? MonoGame is fully compatible with XNA 4. To make sure that existing code would work with MonoGame, the MonoGame creators decided to use the Namespace names that XNA used. This also means that as you work with MonoGame and come across XNA examples, you should be able to use them just fine in MonoGame.

Since we put the Content.Load statements in our GameContent class constructor, the assets will all be loaded when a GameContent class is created.

We need to pass a reference to the ContentManager, because all of the assets we are loading will be managed by that class. The rest of the method just uses the ContentManager Load method to load all of our asset files. Note, we have to tell MonoGame the type of file we are loading:

  • Images: Texture2D
  • SoundEffects: SoundEffect
  • Font: SpriteFont

The name we pass as the argument matches the file names we added in the pipeline tool (minus the file extension). In our Game1 class ContentLoad method, we’ll create a new instance of the GameContent class, which will load all of the assets we need to the ContentManager.

So, in the Game1 class, add a new variable called gameContent:
[code language=”csharp” highlight=”5″]
public class Game1: Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameContent gameContent;
//rest of file not shown for space reasons
[/code]
And we’ll need to create an instance of GameContent in the Game1 LoadContent Method:
[code language=”csharp” highlight=”7″]
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
gameContent = new GameContent(Content);
}
[/code]

With these changes, our program has now loaded all of the assets we’ll need into the ContentManager. You may want to run your program now, just to make sure it still brings the game up with the blue-background window. If you get an error, you probably forgot to add a file to the pipeline tool, or you forgot to do a Build using the pipeline tool. If you get an error, I’d go back and make sure you’ve done all of the steps from Part 3.

If your game still runs properly, we’re ready to actually start doing some drawing. We’ll tackle that in Part 5.

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

  1. 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.

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

  3. 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 🙂

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

  5. 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!

  6. 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!

Leave a Reply

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