Table of Contents

Class Game

Namespace
Microsoft.Xna.Framework
Assembly
MonoGame.Framework.dll

This class is the entry point for most games. Handles setting up a window and graphics and runs a game loop that calls Update(GameTime) and Draw(GameTime).

public class Game : IDisposable
Inheritance
Game
Implements
Inherited Members

Constructors

Game()

Create a Game.

public Game()

Properties

Components

A collection of game components attached to this Game.

public GameComponentCollection Components { get; }

Property Value

GameComponentCollection

Content

The ContentManager of this Game.

public ContentManager Content { get; set; }

Property Value

ContentManager

Exceptions

ArgumentNullException

If Content is set to

null
.

GraphicsDevice

Gets the GraphicsDevice used for rendering by this Game.

public GraphicsDevice GraphicsDevice { get; }

Property Value

GraphicsDevice

Exceptions

InvalidOperationException

There is no GraphicsDevice attached to this Game.

InactiveSleepTime

public TimeSpan InactiveSleepTime { get; set; }

Property Value

TimeSpan

IsActive

Indicates if the game is the focused application.

public bool IsActive { get; }

Property Value

bool

IsFixedTimeStep

Indicates if this game is running with a fixed time between frames.

When set to

true
the target time between frames is given by TargetElapsedTime.
public bool IsFixedTimeStep { get; set; }

Property Value

bool

IsMouseVisible

Indicates if the mouse cursor is visible on the game screen.

public bool IsMouseVisible { get; set; }

Property Value

bool

LaunchParameters

The start up parameters for this Game.

public LaunchParameters LaunchParameters { get; }

Property Value

LaunchParameters

MaxElapsedTime

The maximum amount of time we will frameskip over and only perform Update calls with no Draw calls. MonoGame extension.

public TimeSpan MaxElapsedTime { get; set; }

Property Value

TimeSpan

Services

Get a container holding service providers attached to this Game.

public GameServiceContainer Services { get; }

Property Value

GameServiceContainer

TargetElapsedTime

The time between frames when running with a fixed time step. IsFixedTimeStep

public TimeSpan TargetElapsedTime { get; set; }

Property Value

TimeSpan

Exceptions

ArgumentOutOfRangeException

Target elapsed time must be strictly larger than zero.

Window

The system window that this game is displayed on.

[CLSCompliant(false)]
public GameWindow Window { get; }

Property Value

GameWindow

Methods

BeginDraw()

Called right before Draw(GameTime) is normally called. Can return

false
to let the game loop not call Draw(GameTime).
protected virtual bool BeginDraw()

Returns

bool
true
if Draw(GameTime) should be called,
false
if it should not.

BeginRun()

Called after Initialize(), but before the first call to Update(GameTime).

protected virtual void BeginRun()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

Draw(GameTime)

Called when the game should draw a frame.

Draws the DrawableGameComponent instances attached to this game. Override this to render your game.

protected virtual void Draw(GameTime gameTime)

Parameters

gameTime GameTime

A GameTime instance containing the elapsed time since the last call to Draw(GameTime) and the total time elapsed since the game started.

EndDraw()

Called right after Draw(GameTime). Presents the rendered frame in the GameWindow.

protected virtual void EndDraw()

EndRun()

Called when the game loop has been terminated before exiting.

protected virtual void EndRun()

Exit()

Exit the game at the end of this tick.

public void Exit()

~Game()

protected ~Game()

Initialize()

Override this to initialize the game and load any needed non-graphical resources.

Initializes attached GameComponent instances and calls LoadContent().

protected virtual void Initialize()

LoadContent()

Override this to load graphical resources required by the game.

protected virtual void LoadContent()

OnActivated(object, EventArgs)

Called when the game gains focus. Raises the Activated event.

protected virtual void OnActivated(object sender, EventArgs args)

Parameters

sender object

This Game.

args EventArgs

The arguments to the Activated event.

OnDeactivated(object, EventArgs)

Called when the game loses focus. Raises the Deactivated event.

protected virtual void OnDeactivated(object sender, EventArgs args)

Parameters

sender object

This Game.

args EventArgs

The arguments to the Deactivated event.

OnExiting(object, EventArgs)

Called when the game is exiting. Raises the Exiting event.

protected virtual void OnExiting(object sender, EventArgs args)

Parameters

sender object

This Game.

args EventArgs

The arguments to the Exiting event.

ResetElapsedTime()

Reset the elapsed game time to Zero.

public void ResetElapsedTime()

Run()

Run the game using the default GameRunBehavior for the current platform.

public void Run()

Run(GameRunBehavior)

Run the game.

public void Run(GameRunBehavior runBehavior)

Parameters

runBehavior GameRunBehavior

Indicate if the game should be run synchronously or asynchronously.

RunOneFrame()

Run the game for one frame, then exit.

public void RunOneFrame()

SuppressDraw()

Supress calling Draw(GameTime) in the game loop.

public void SuppressDraw()

Tick()

Run one iteration of the game loop.

Makes at least one call to Update(GameTime) and exactly one call to Draw(GameTime) if drawing is not supressed. When IsFixedTimeStep is set to

false
this will make exactly one call to Update(GameTime).
public void Tick()

UnloadContent()

Override this to unload graphical resources loaded by the game.

protected virtual void UnloadContent()

Update(GameTime)

Called when the game should update.

Updates the GameComponent instances attached to this game. Override this to update your game.

protected virtual void Update(GameTime gameTime)

Parameters

gameTime GameTime

The elapsed time since the last call to Update(GameTime).

Events

Activated

Raised when the game gains focus.

public event EventHandler<EventArgs> Activated

Event Type

EventHandler<EventArgs>

Deactivated

Raised when the game loses focus.

public event EventHandler<EventArgs> Deactivated

Event Type

EventHandler<EventArgs>

Disposed

Raised when this game is being disposed.

public event EventHandler<EventArgs> Disposed

Event Type

EventHandler<EventArgs>

Exiting

Raised when this game is exiting.

public event EventHandler<EventArgs> Exiting

Event Type

EventHandler<EventArgs>