MonoGame Development for Windows 8 Apps (Metro Apps)

Monogame Development

Monogame DevelopmentMonoGame Development for Windows 8 Apps

What is it?

“MonoGame” is the component which is used by developers to develop their favorite 2D games. It is an open-source implementation of MS XNA 4 frame work. Supported platforms are windows 8, iOS, Android, Linux and Mac. On the other side, it has some bugs and lack of features. But it is a good component and better alternative to XNA.

The Game Life Cycle

There are three main steps involving in running a game.

1. Initialization/Load
2. Game loop
3. Unload

The MS XNA will provide a skeleton for any type of game. So the developers easily implement the skeleton within the game class, which is the part of XNA’s framework namespace.

Game Class Methods (The MonoGame Loop)

Game Class has virtual methods that cover other aspects of the following game steps.

  • Class constructor
  • Initialize
  • LoadContent
  • Update
  • Draw
  • UnloadContent

The “update” and “draw” methods are often called by the XNA frame work – not exactly in sequence, but several time each second – as per the frame rate setup in the initialization phase.

Preparing the System

You need the following things in your machine in order to proceed with the setup process.

1. Windows 8 or 8.1 RP
2. MS Visual Studio 2012 Express for windows 8 or Windows phone (or if you have VS 2012 pro version or above)
3. The MonoGame installer (Download it from

Start Your New Project

1. Create new project in VS 2012
2. Under VC#, you can see the MonoGame templates. Pick a template and click OK.
3. Now press F5 to compile the project.

A Cornflower Blue screen output is a successful setup.

Code for initialize Game Class Constructor

The following code can Initializes one new instance of this class that provide essential graphics device initialization, label the game frame rate, etc.

/// 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);
/// <summary>
/// initializes a new instance of this class,
 to handle the configuration and management of the graphics device.
/// <summary>
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);


This method will automatically enumerate through any Game components which have added to Game.Components and call “Initializemethods”. You can initialize any game assets by including the following code.

protected override void Initialize()


The LoadContent() method is called by “Initialize”. It is called any time the game content needs to be reloaded too, whenever the DeviceReset event occurs. You shouldn’t access the “GraphicsDevice” until “LoadContent” is called. Check the following example.

protectedoverridevoid LoadContent()
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
_background = Content.Load<Texture2D>("air_strike");
_sourcePlane = Content.Load<Texture2D>("Plane3");

Before you begin the project, you need to convert game property file like Images, effects and sprites into .xnb file extension. MonoGame does not offer the option to convert the file into .xnb extension. So you need to use the XNAContentCompiler to convert game asset files to .Xnb file.


XNA Frame work calls Game.UnloadContent() function when the game is closing.

Update ()

The Update() function creates a loop and it is the place to update the game logic. Move objects, take the player input and define the output of collisions between game objects etc.

protectedvirtualvoid Update (GameTime gameTime)

Create some new methods and add little logic, which will move the sprite around every frame set and cause the sprite to change the path suppose it hits the corners 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);
new Rectangle((int)sourcePlane.X, (int)sourcePlane.Y, 80, 80), 
new Rectangle(0, 0, _sourcePlane.Width, _sourcePlane.Height), 
Color.White, 0f, new Vector2(_sourcePlane.Width / 2, 
_sourcePlane.Width / 2), SpriteEffects.None, 0); 
_spriteBatch.End(); base.Draw(gameTime);

The above code draws the sprite on screen each frame set.

Note that the parameter sent by the Begin method, BlendState.AlphaBlend tells the Draw method to use alpha channel of the source color in order to create a transparency effect. So that the targeted color is appear through the source color.

This is the time to Build and run your game. Run the game and see the sprite moves around the screen and try to change the path using your mouse or keyboard.

Here is the complete source code of the game project FlightShooter

We hope you find this article useful.


About Author:
A  Microsoft gold certified partner providing offshore software development on Microsoft technologies (.NET, SQL), custom application development, SharePoint, Microsoft Business Intelligence, Dynamics ERP & CRM and Mobile app development on iOS, Windows and Android platforms. Please do contact us if you have any questions pertaining to this article or any other queries that you might have relating to our services.

Leave a Reply