1. Home
  2. /
  3. Monogame
  4. /
  5. Simple Counter Example

Simple Counter Example - Monogame Typing CST Test

Loading…

Simple Counter Example — Monogame Code

A minimal MonoGame example displaying a counter that increments/decrements with keyboard input.

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

Monogame Language Guide

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.

Primary Use Cases

  • ▸2D and 3D games
  • ▸Cross-platform indie games
  • ▸PC, mobile, and console games
  • ▸Educational game development
  • ▸Prototyping and experimental game projects

Notable Features

  • ▸C# and .NET development
  • ▸2D and 3D rendering support
  • ▸Cross-platform deployment
  • ▸Low-level graphics API access (DirectX, OpenGL, Metal)
  • ▸Audio, input, networking, and shader support

Origin & Creator

MonoGame was created as an open-source implementation of Microsoft XNA in 2009 by Xamarin contributors, continuing XNA’s legacy for cross-platform development.

Industrial Note

MonoGame is popular for developers who want XNA-style development in C# with multi-platform deployment, including Windows, Mac, Linux, iOS, Android, consoles, and web via WebAssembly.

More Monogame Typing Exercises

MonoGame Sprite Movement ExampleMonoGame FPS Display ExampleMonoGame Simple Animation ExampleMonoGame Keyboard Input ExampleMonoGame Mouse Input ExampleMonoGame Simple Physics ExampleMonoGame Enemy Follow ExampleMonoGame Simple Shooting ExampleMonoGame Simple Random Walker Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher