Learn Monogame - 10 Code Examples & CST Typing Practice Test
MonoGame is an open-source, cross-platform game development framework written in C# and based on Microsoft’s XNA framework. It enables developers to build 2D and 3D games for multiple platforms using a single codebase.
View all 10 Monogame code examples →
Learn MONOGAME with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Monogame
What is Monogame?
MonoGame is an open-source, cross-platform game development framework written in C# and based on Microsoft’s XNA framework. It enables developers to build 2D and 3D games for multiple platforms using a single codebase.
What are the primary use cases for Monogame?
2D and 3D games. Cross-platform indie games. PC, mobile, and console games. Educational game development. Prototyping and experimental game projects
What are the strengths of Monogame?
Powerful C# ecosystem. Cross-platform support including consoles. Flexible low-level rendering and 3D support. Active community and extensive tutorials. Good for both 2D and 3D games
What are the limitations of Monogame?
No visual editor; code-only workflow. Requires C# and .NET knowledge. Smaller asset marketplace compared to Unity/Unreal. Manual management of scenes and resources. No built-in physics engine (needs third-party)
How can I practice Monogame typing speed?
CodeSpeedTest offers 10+ real Monogame code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.