Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Adding Draw Code

The final step is adding code to draw the game. Replace your existing Draw method in the Game1 class with the following:

protected override void Draw(GameTime gameTime)
{
    // Only draw when game is active
    if (this.IsActive)
    {
        // Based on the current game state,
        // call the appropriate method
        switch (currentGameState)
        {
            case GameState.SignIn:
            case GameState.FindSession:
            case GameState.CreateSession:
                GraphicsDevice.Clear(Color.DarkBlue);
                break;

            case GameState.Start:
                DrawStartScreen(  );
                break;


            case GameState.InGame:
                DrawInGameScreen(gameTime);
                break;

            case GameState.GameOver:
                DrawGameOverScreen(  );
                break;

        }

    }

    base.Draw(gameTime);
}

This method, like the Update method, will perform certain actions only when the game is active. This is to prevent drawing when the gamer services windows are open. The method then calls other methods based on the game state.

Notice that the SignIn, FindSession, and CreateSession game states do nothing but draw a blank screen by calling GraphicsDevice.Clear. This is because other gamer services activities are going on during these game states, and no drawing on the screen is needed.

So, let's start with the next one. Add the following DrawStartScreen method to your Game1 class:

private void DrawStartScreen( ) { // Clear screen GraphicsDevice.Clear(Color.AliceBlue); // Draw text for intro splash screen spriteBatch.Begin( ); // Draw instructions string text = "The dynamite player chases the gears\n"; text += networkSession.Host.Gamertag + " is the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page