Click here to Skip to main content
15,882,315 members
Articles / Programming Languages / C#

XNA Basics (Windows 10 and MonoGame)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Aug 2015CPOL4 min read 21.1K   8   4
Learn the basics of game development in C# and MonoGame


In this article, you will learn how to do a simple MonoGame using C# as the programming language. A PC version for this tutorial.



A game, to work, requires 4 basic methods.


Initializes all objects, variables and managers required at start up.


Pre-load images, sounds, fonts, etc. and assign variables.


Updates the game LOGIC.


Draw the images on screen.


Game runs indefinitely and specified by a TIMER.

Timer measures how many times or how fast the Update() and Draw() execute in a cycle.

We then TIME it how much it processes and LIMIT that to reach the maximum potential of our processors thus making the game run efficiently.

We measure a process by nano time and limit it by frames per second (FPS). 

Game.TargetElapsedTime = (60 / 1000); // Sample 60 FPS

Game state

Every program has a status of WHAT is currently happening. 

  • Intro
  • Game Menu
  • Loading
  • Playing
  • Game Over

The program must know what is going on the screen (current state).  

if(gameState == "Main Menu")

if(gameState == "In Game")

if(gameState == "Settings")

Like the “Main Menu”, it waits for user input as to what the user wanted to do in the menu. If the user wanted to play a game, it now go to the state “In Game” then Update()s and Draw()s the specific resources required to play the game.  

That includes game logic, drawing of images, playing of sounds and what ever the game requires it to.


MonoGame is an Open Source implementation of the Microsoft XNA 4 Framework. Our goal is to allow XNA developers on Xbox 360, Windows & Windows Phone to port their games to the iOS, Android, Mac OS X, Linux and Windows 8 Metro. PlayStation Mobile, Raspberry PI, and PlayStation 4 platforms are currently in progress.

Creating a Project

When you create a Monogame project. (Windows Game)

Monogame now manages Content added to the game. The files are processed during build so we can use them when we run our game.

Let us name that project as MyFirstXNA


Step 1 : MyFirstXNA

I will show you how to FIRST display an image on the screen. With MonoGame, we will use the MonoGame Pipeline, a program capable of processing content into XNA’s format.

  1. Run Monogame Pipeline (Start > All Apps > MonoGame > MonoGame Pipeline)
  2. Open the Content file in your project
  3. Right click the Content then Add>Existing Item
  4. Select omnom.png or any image you want

It should appear in Content’s tree then Build the project


The omnom.png image I used 


Step 2: Coding

In Game1.cs

Declare globally  (must be declared outside any methods)

Texture2D omnom;

Vector2 position;


omnom = Content.Load<Texture2D>("omnom");

position = new Vector2(100f, 100f); 

Put this inside  

protected override void LoadContent()
  • Assign omnom image to Texture2D “omnom”
  • Assign any Vector 2D position to “position” 

A programmer's perspective


Programmers see the coordinate plane like this.
The point of origin is at the upper left most corner of the screen.

And as described, we use the 4th quadrant of the coordinate plane mostly.
We define points inside the screen as Vector2 for 2D position which is the XY coordinate plane

In XNA we call this the Viewport, it contains

  • BufferHeight
  • BufferWidth

Step 2 continuation

We draw the image using XNA’s SpriteBatch, manages the graphics drawn on screen.


spriteBatch.Draw(omnom, position, Color.White);


Put this inside

protected override void Draw(GameTime gameTime)

It first initialize the sprite batch to begin drawing, draws the processed image then flushes the sprite batch to restore the Game’s state.


Step 3: Run the game

After all coding try running the game. You’ll see the image drawn on screen. 

Step 4: Movement

Now let's make Omnom move!

Declare globally  (declare outside all other methods)

KeyboardState currentState;


We now update the logic of simple movement using the cursor keys of our keyboard.

We then assign the currentState from the Keyboard’s current state

currentState = Keyboard.GetState();

Put this code inside 

protected override void Update(GameTime gameTime)

Make sure currentState is on top of our game logic to ensure that user input is first captured before doing anything. 


We now modify the position of omnom as we tap the cursor keys of our keyboard.


protected override void Update(GameTime gameTime) 

We put

{ position.Y -= 5; }

{ position.Y += 5; }

{ position.X -= 5; }

{ position.X += 5; }

To move up we subtract a number to Y to move it UP;

Add to Y to move DOWN; 

Subtract a number to X to move LEFT;

And add to X to move RIGHT.

Try running the game and see how Omnom moves.  


What do you think is better? The old XNA or MonoGame? Let me know in the comments and also feel free to ask.


XNA version

This is the Windows 7, 8 and 8.1 version of the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionDoes not work in Windows 10 Pin
jockey4her10-Oct-15 6:10
jockey4her10-Oct-15 6:10 
Your page lacks the appropriate installation information. Please add this.

After installing Monogame from their web site, and running their Windows 10 Universal template for visual studio I get this error:

Visual Studio update required
One or more projects require a platform SDK (UAP, Version=10.0.10069.0 that is either not installed or is included as part of a future update to Visual Studio.

What's the solution. I have the latest SDK for windows 10.
AnswerRe: Does not work in Windows 10 Pin
Patchrick29-Oct-15 4:54
professionalPatchrick29-Oct-15 4:54 
QuestionImages missing. Pin
clawton4-Aug-15 15:55
clawton4-Aug-15 15:55 
AnswerRe: Images missing. Pin
Patchrick4-Aug-15 17:51
professionalPatchrick4-Aug-15 17:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.