Monogame for the Impatient Developer

This is a guide for those who want to learn Monogame but just want to know the bare essentials to get running. The target audience are those who can already code or know C#. Also if you are a game developer currently using something else and are simply wanting to get familiar with Monogame, this might be for you.

Whenever I look at a framework, engine or guide I like to see the results of what can be done using it. Below are some examples of projects I have made.
Skip examples.

Example Projects

Getting Started

This will focus on using Windows for development. I myself use Win 7 (I know, I know) and Visual Studio 2015 but Visual Studio 2017 should work fine. Head to this link and download Monogame 3.6

Run the installer and just yes through everything. You can install through NUGET, which I believe lets you choose specific template types. The installer simply installs all template types.

Project Setup

So once all that is installed, simply fire up VS and create a new project. I use the “Cross Platform Desktop Project” as the projects I'm developing require it. This guide will use that template, but everything should work with the “Windows project” template. (Disclaimer: I have not tested it.)

Choose the cross-platform project:

We now have our project. If you press run, you should see the following:

The project consists of two source files: Program.cs and Game1.cs. Program.cs contains the Main method and Game1.cs is where the magic happens. I won't run through the code as its very well documented and this is for impatient people so, lets learn by doing.

Loading and displaying Images

So we're gonna put this lil guy on screen and make him move about:

There's a few ways we can load resources (images/audio) but we'll use the ContentManager (Pipeline Tool). If you open up your Content Folder and double click “Content.mgcb”. It'll open up the manager. Right-click on “Content” and select “Add → Existing Item” and choose your image.

Then save (ctrl-s). You can build it if you like, but this is done automatically when you build the project.

Now, go to Game1.cs. Create a Texture2D member:

 
				 
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MyAwesomeGame
{

  public class Game1  : Game
  {

   GraphicsDeviceManager graphics;
  SpriteBatch spriteBatch;
   Texture2D boxman;

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

Find the LoadContent method and load the image like so:

 
				 
protected override void LoadContent()
{
  spriteBatch = new SpriteBatch (GraphicsDevice);
  boxman =  Content.Load<Texture2D>("boxman");
}
				
			

Note that you do not need to specify the file extension (i.e: "boxman", not "boxman.png").
It can be a good idea to keep track of images within Visual Studio by adding them to the project.

Finally, draw the image:

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

  spriteBatch.Begin();
  spriteBatch.Draw(boxman, new Vector2(200,200),Color.White);
  spriteBatch.End();

 base.Draw(gameTime);
}
				
			


Hit run and voila, we have our image on screen.

It should be noted that Texture2Ds need to call the Dispose() method when no longer needed as they are an unmanaged resource. This is usually done in the UnloadContent() method. Obviously, though, you should know when you no longer need a resource or object.

The complete code for Game1.cs

Controls (Keyboard)

We'll start off using keyboard input. I'll go through other means later on.

Create a Vector2 member which is boxman's position.

 
				 
namespace MyAwesomeGame
{
  public class Game1 :Game
  {

   GraphicsDeviceManager graphics;
  SpriteBatch spriteBatch;
   Texture2D boxman;
   Vector2 boxmanPos = new Vector2(200,200);
				
			

And make sure it is used in the draw call.

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

  spriteBatch.Begin();
  spriteBatch.Draw(boxman, boxmanPos, Color.White);
  spriteBatch.End();

 base.Draw(gameTime);
}
				
			

Add the following code to the Update method.

 
				 
protected override void Update(GameTime gameTime)
{
 float dt = ( float )gameTime.ElapsedGameTime.TotalSeconds;

  if (Keyboard.GetState().IsKeyDown(Keys.Up))
    boxmanPos.Y -= 100.0f * dt;
  if (Keyboard.GetState().IsKeyDownKeys.Down))
    boxmanPos.Y += 100.0f * dt;

 base.Update(gameTime);
}
				
			

All thats needed is to query the keyboard state and modify the position based on input. This is incredibly rough, but its just to get you started.
The Monogame functions are easy enough to wrap into On-press and On-release functions. I'll cover this in a later guide.

Full source available here.

Audio

Again, we'll be using the Pipeline Tool. I'll be using a sound file from my game, feel free to use any mp3 or ogg file. Most file formats are supported.
The process is basically the same as loading images.
Open the Pipeline Tool, right-click on "Content" in the project window > add existing item and select your file.

Ensure you have the processor correctly set to SoundEffect:

Save before you close (ctrl-s).

Add a SoundEffect and an SoundEffectInstance member, for this you'll need to import “Microsoft.Xna.Framework.Audio”.

 
				 

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

namespace MyAwesomeGame
{

  public class Game1  : Game
  {

   GraphicsDeviceManager graphics;
  SpriteBatch spriteBatch;
   Texture2D boxman;
   Vector2 boxmanPos = new Vector2(200,200);

   SoundEffect sfx;
   SoundEffectInstance sfxInstance;
				
			

Initialise the sfx member:

 
				 
protected override void LoadContent()
{
  spriteBatch = new SpriteBatch (GraphicsDevice);
  boxman =  Content.Load<Texture2D>("boxman");
  sfx =  Content.Load<SoundEffect>("sfx_voice_00");
  sfxInstance = sfx.CreateInstance();
}
				
			

The SoundEffectInstance is not required. You can solely use the SoundEffect but, an instance allows better control and has queryable state. They're kind of analogous to Texture and Sprite.
All thats left to do is hook up a key to play our sound:

 
				 
protected override void Update(GameTime gameTime)
{
 float dt = ( float )gameTime.ElapsedGameTime.TotalSeconds;

  if (Keyboard.GetState().IsKeyDown(Keys.Up))
    boxmanPos.Y -= 100.0f * dt;
  if (Keyboard.GetState().IsKeyDownKeys.Down))
    boxmanPos.Y += 100.0f * dt;

 if (Keyboard.GetState().IsKeyDown(Keys.Space))
  {
   if (sfxInstance.State != SoundState.Playing)
     sfxInstance.Play();
  }

 base.Update(gameTime);
}
				
			



Just like Texture2D, SoundEffects need to be disposed of.
Full source available here.

Email: magellanicDev@gmail.com
Last Updated: 16/02/2019
Copyright: Magellanic Games LTD

Generated with SpeedyHtml.