Code Library
Community MonoGame Code Samples
A curated collection of MonoGame snippets contributed by the community. Easily copy and adapt code for your projects.
Find your next learning target
Want a faster way to spot what to learn next? Run the challenge hub that combines all tutorial knowledge checks, adds bonus questions, and links you back to the right tutorials when you miss something.
Core Loop & States
Screens & Rendering
Interaction & UI
Download the runnable sample project
Use the verified ZIP bundle for this article to review the extracted example files, run the sample host, and repackage the sources without bin or obj folders.
Prism-powered sample
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
public class SampleGame : Game
{
private static readonly Color Ink = new Color(13, 17, 23);
private static readonly Color Cyan = new Color(77, 225, 255);
private Color _background = Color.CornflowerBlue;
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
Exit();
}
var seconds = (int)gameTime.TotalGameTime.TotalSeconds;
_background = (seconds % 2 == 0) ? Ink : Cyan;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(_background);
base.Draw(gameTime);
}
}
dotnet tool install -g dotnet-mgcb-editor
mgcb-editor --platform:DesktopDX
Full Markdown source remains in the archived repository under docs/code_samples.md and related files for deeper reading.