Learn MONOGAME with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
MonoGame Simple Counter Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class CounterGame : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteFont _font;
private int count = 0;
public CounterGame() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); _font = Content.Load<SpriteFont>("Arial"); }
protected override void Update(GameTime gameTime)
{
var kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.Up)) count++;
if (kstate.IsKeyDown(Keys.Down)) count--;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_spriteBatch.DrawString(_font, $"Count: {count}", new Vector2(100, 100), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
A minimal MonoGame example displaying a counter that increments/decrements with keyboard input.
2
MonoGame Sprite Movement Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class SpriteMove : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Texture2D _sprite;
private Vector2 _position = new Vector2(100,100);
private float _speed = 2f;
public SpriteMove() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); _sprite = Content.Load<Texture2D>("player"); }
protected override void Update(GameTime gameTime)
{
var kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.Left)) _position.X -= _speed;
if (kstate.IsKeyDown(Keys.Right)) _position.X += _speed;
if (kstate.IsKeyDown(Keys.Up)) _position.Y -= _speed;
if (kstate.IsKeyDown(Keys.Down)) _position.Y += _speed;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_spriteBatch.Draw(_sprite, _position, Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
Moves a texture sprite using arrow keys.
3
MonoGame FPS Display Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class FpsDisplay : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteFont _font;
public FpsDisplay() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); _font = Content.Load<SpriteFont>("Arial"); }
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
int fps = (int)(1 / gameTime.ElapsedGameTime.TotalSeconds);
_spriteBatch.DrawString(_font, $"FPS: {fps}", new Vector2(10,10), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
Displays frames per second on screen.
4
MonoGame Simple Animation Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class AnimationExample : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Texture2D _spriteSheet;
private int _frame = 0;
private double _timer = 0;
private Vector2 _position = new Vector2(100,100);
public AnimationExample() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); _spriteSheet = Content.Load<Texture2D>("sprite_sheet"); }
protected override void Update(GameTime gameTime)
{
_timer += gameTime.ElapsedGameTime.TotalSeconds;
if (_timer > 0.1) { _frame = (_frame + 1) % 4; _timer = 0; }
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_spriteBatch.Draw(_spriteSheet, _position, new Rectangle(32*_frame,0,32,32), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
Cycles through sprite frames to animate.
5
MonoGame Keyboard Input Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class KeyboardExample : Game
{
private GraphicsDeviceManager _graphics;
public KeyboardExample() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void Update(GameTime gameTime)
{
var kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.Space)) { System.Console.WriteLine("Space pressed"); }
base.Update(gameTime);
}
}
Prints a message when keys are pressed.
6
MonoGame Mouse Input Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class MouseExample : Game
{
private GraphicsDeviceManager _graphics;
public MouseExample() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void Update(GameTime gameTime)
{
var mstate = Mouse.GetState();
if (mstate.LeftButton == ButtonState.Pressed) { System.Console.WriteLine("Left click"); }
if (mstate.RightButton == ButtonState.Pressed) { System.Console.WriteLine("Right click"); }
base.Update(gameTime);
}
}
Detects left and right mouse clicks.
7
MonoGame Simple Physics Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class PhysicsExample : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Vector2 _position = new Vector2(100,0);
private float _vspeed = 0;
private float _gravity = 200f;
public PhysicsExample() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void Update(GameTime gameTime)
{
_vs = _vspeed + _gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
_position.Y += _vspeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
base.Update(gameTime);
}
}
Applies simple gravity to a falling object.
8
MonoGame Enemy Follow Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class EnemyFollow : Game
{
private GraphicsDeviceManager _graphics;
private Vector2 _enemyPos = new Vector2(50,50);
private Vector2 _playerPos = new Vector2(200,200);
private float _speed = 2f;
public EnemyFollow() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void Update(GameTime gameTime)
{
Vector2 dir = _playerPos - _enemyPos;
dir.Normalize();
_enemyPos += dir * _speed;
base.Update(gameTime);
}
}
Enemy moves towards player position each frame.
9
MonoGame Simple Shooting Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
public class ShootingExample : Game
{
private GraphicsDeviceManager _graphics;
private List<Vector2> bullets = new List<Vector2>();
private Vector2 playerPos = new Vector2(100,100);
public ShootingExample() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Space)) { bullets.Add(playerPos); }
for(int i=0;i<bullets.Count;i++) { bullets[i] += new Vector2(5,0); }
base.Update(gameTime);
}
}
Player shoots bullets when pressing space.
10
MonoGame Simple Random Walker Example
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class RandomWalker : Game
{
private GraphicsDeviceManager _graphics;
private Vector2 pos = new Vector2(100,100);
private Random rnd = new Random();
public RandomWalker() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
protected override void Update(GameTime gameTime)
{
pos.X += rnd.Next(-1,2);
pos.Y += rnd.Next(-1,2);
base.Update(gameTime);
}
}
Moves a point randomly each frame.