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.

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

Line numbers, copy-to-clipboard, language labels, downloads, and highlighted focus lines are enabled for every block.

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);
    }
}

Download the sample: UpdateLoop.cs

dotnet tool install -g dotnet-mgcb-editor
mgcb-editor --platform:DesktopDX

Download the script: pipeline.ps1

Full Markdown source remains in the archived repository under docs/code_samples.md and related files for deeper reading.