Class Game
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
Content
The ContentManager of this Game.
public ContentManager Content { get; set; }
Property Value
Exceptions
- ArgumentNullException
If Content is set to
.null
GraphicsDevice
Gets the GraphicsDevice used for rendering by this Game.
public GraphicsDevice GraphicsDevice { get; }
Property Value
Exceptions
- InvalidOperationException
There is no GraphicsDevice attached to this Game.
InactiveSleepTime
Gets or sets time to sleep between frames when the game is not active
public TimeSpan InactiveSleepTime { get; set; }
Property Value
IsActive
Indicates if the game is the focused application.
public bool IsActive { get; }
Property Value
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
IsMouseVisible
Indicates if the mouse cursor is visible on the game screen.
public bool IsMouseVisible { get; set; }
Property Value
LaunchParameters
The start up parameters for this Game.
public LaunchParameters LaunchParameters { get; }
Property Value
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
Services
Get a container holding service providers attached to this Game.
public GameServiceContainer Services { get; }
Property Value
TargetElapsedTime
The time between frames when running with a fixed time step. IsFixedTimeStep
public TimeSpan TargetElapsedTime { get; set; }
Property Value
Exceptions
- ArgumentOutOfRangeException
Target elapsed time must be strictly larger than zero.
Window
The system window that this game is displayed on.
public GameWindow Window { get; }
Property Value
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
if Draw(GameTime) should be called,true
if it should not.false
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
GameTimeA 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
OnDeactivated(object, EventArgs)
Called when the game loses focus. Raises the Deactivated event.
protected virtual void OnDeactivated(object sender, EventArgs args)
Parameters
sender
objectThis Game.
args
EventArgsThe arguments to the Deactivated event.
OnExiting(object, ExitingEventArgs)
Called when the game is exiting. Raises the Exiting event.
protected virtual void OnExiting(object sender, ExitingEventArgs args)
Parameters
sender
objectThis Game.
args
ExitingEventArgsThe 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
GameRunBehaviorIndicate 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
GameTimeThe elapsed time since the last call to Update(GameTime).
Events
Activated
Raised when the game gains focus.
public event EventHandler<EventArgs> Activated
Event Type
Deactivated
Raised when the game loses focus.
public event EventHandler<EventArgs> Deactivated
Event Type
Disposed
Raised when this game is being disposed.
public event EventHandler<EventArgs> Disposed
Event Type
Exiting
Raised when this game is exiting.
public event EventHandler<ExitingEventArgs> Exiting