Monogame: getting started with 2D Game Dev

Game Development

What Is Monogame

Monogame is a component used by game developers to make their games. Monogame is an open source implementation of Microsoft XNA 4 Framework. It can target more platforms, including Android, iOS, Linux, Mac, and Windows 8. On the other hand, its development is a bit more fragmented and there are some bugs & features not available. Overall, it’s a pretty good project and alternative to XNA.

(Microsoft XNA is a set of tools with a managed runtime environment provided by Microsoft that facilitates game development and management. XNA is based on the .NET Framework. It can target several platforms like “Windows”,” Windows Phone “and the” Xbox”)

Game Life Cycle

A running game usually goes through the following steps:

1. Initialization/Load – Sets default and preliminary values to the game, queries and initializes user-based information, loads graphic & non-graphic contents etc.

2. Game Loop – Performs in-game repeating logic and layout calculations & render.

3. Unload – Saves current state, releases and unloads contents etc.

XNA provides a skeleton that is valid for any type of game. The skeleton is implemented within the Game class, which is a part of the Microsoft Xna Framework namespace. Once inherited, the Game class provides the complete game life cycle, enabling the programmer to override the required methods in order to add specific game logic.

Monogame Loop


Game Class Methods (Monogame Loop)

The Game class contains, among other things, virtual methods that cover the various aspects of the listed game steps. An inherited Game class may override some or all of these methods, and define a constructor as described below.

Class constructor – Used to instantiate and set default values to required elements. For example, instantiate the graphics device manager, define the game frame rate etc.
Initialize – Sets default and preliminary values to the game shell, queries and initializes user-based information etc.
LoadContent – Loads all graphics and other content required to run the game. For example, LoadContent to load game component as an object like background images, sound, and so on.
Update – Performs on-going game logic: collects input information from the various input devices. Calculates current positions, collisions and states; plays audio, and so on.
Draw – Draws the current view of the game: backgrounds, sprites, et cetera.
UnloadContent – Unloads all game content and content managers.
The Update and Draw methods are called repeatedly by the XNA Framework – not necessarily in sequence, but multiple times every second – according to the frame rate defined in the initialization phase

Each pass through the game loop is called a frame. XNA games on Windows and Xbox 360 target 60 frames per second (fps), and Windows Phone games run at 30 fps.

Preparing The System

Before moving to the setup of MonoGame, first we need to install the following

1. Install OS Windows 8 or 8.1 Release Preview

2. Install Visual Studio 2012 Express for Windows 8 or Visual Studio 2012 Express for Windows Phone

3. Download and install monogame installer from the flowing path

4. Launch Visual Studio 2012 for Windows 8 (or Visual Studio 2012 if you have Pro or above) and create a New Project.

5. Under Visual C#, you should now have MonoGame template(s). Pick the Game template and click OK.

6. Press F5 to compile and run the project. If you see a Cornflower Blue screen, you have successfully set up MonoGame.

2D Game Example


Initialize Game Class Constructor

Initializes a new instance of this class, which provides basic graphics device initialization, define the game frame rate, et cetera.

/// to handle the configuration and management of the graphics device.

GraphicsDeviceManager _graphics;

/// to handle Texture objects and write the position
SpriteBatch _spriteBatch;

// This is a texture we can render. Texture2D _sourcePlan;

// This is a background texture we can render.
Texture2D _background;

// To set the default position of the Plane
Vector2 sourcePlane = new Vector2(0, 0);

/// initializes a new instance of this class, to handle the configuration and management of the graphics device. ///

public Game1()
// Initialize the GraphicsDeviceManager
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = “Content”;
// to set the default position of the Plane
sourcePlane = new Vector2(0, 0);



The Initializemethod automatically enumerate through any game components that has been added to Game.Components and call their Initializemethods. We can initialize any game assets

protected override void Initialize()


This method is called by Initialize. Also, it is called any time the game content needs to be reloaded, such as when the DeviceReset event occurs. You should not access the GraphicsDevice until LoadContent is called.

protected override void LoadContent()
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
_background = Content.Load(“air_strike“);
_sourcePlane = Content.Load(“Plane3“);

Before starting your projects we need to convert game assets file like Images, effects and sprites into a .xnb file. Monogame doesn’t provide the option to convert the file into a .xnb. So we need to use XNAContentCompiler for compile the game assets to .Xnb file



XNA Framework calls Game.UnloadContent() is when your game is closing down. The only time it calls GameComponent.UnloadContent()is where any game assets can be released. You do not need to unload content managed by a ContentManager (such as Models and Effects)

Update ()


The Update loop is the best place to update your game logic: move objects around, take player input, and decide the outcome of collisions between objects

protectedvirtualvoid Update (GameTime gameTime)


Create new method and adds a little bit of logic that will move the sprite around each frame and cause the sprite to change direction if it hits the edges of the game window.


void UpdateSprite(GameTime gameTime)

KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Up))
if(sourcePlane.Y > 0)
sourcePlane.Y -= (float)(30 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds);
else if (keyboardState.IsKeyDown(Keys.Down))
if (sourcePlane.Y < Window.ClientBounds.Height) sourcePlane.Y += (float)(30 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds); } else if (keyboardState.IsKeyDown(Keys.Left)) { if (sourcePlane.X > 0)
sourcePlane.X -= (float)(30 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds);
else if (keyboardState.IsKeyDown(Keys.Right))
if (sourcePlane.X < Window.ClientBounds.Width)
sourcePlane.X += (float)(30 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds);


protectedoverridevoid Draw(GameTime gameTime)
_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
_spriteBatch.Draw(_background, new Rectangle(0, 0,
Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);
_spriteBatch.Draw(_sourcePlane, new Rectangle((int)sourcePlane.X, (int)sourcePlane.Y, 80, 80),
new Rectangle(0, 0, _sourcePlane.Width, _sourcePlane.Height), Color.White,
new Vector2(_sourcePlane.Width / 2, _sourcePlane.Width / 2), SpriteEffects.None, 0);


This code draws the sprite on the screen each frame.

Notice the parameter passed by the Begin method, BlendState.AlphaBlend. This parameter tells the Draw method to use the alpha channel of the source color to create a transparency effect so that the destination color appears through the source color.

Now Build and run your game. The sprite moves across the screen and changes direction using mouse or keyboard input.