Book Image

XNA 4 3D Game Development by Example: Beginner's Guide

By : Kurt Jaegers
Book Image

XNA 4 3D Game Development by Example: Beginner's Guide

By: Kurt Jaegers

Overview of this book

Move beyond the world of flat 2D-based game development and discover how to create your own exciting 3D games with Microsoft XNA 4.0. Create a 3D maze, fire shells at enemy tanks, and drive a rover on the surface of Mars while being attacked by alien saucers."XNA 4 3D Game Development by Example: Beginner's Guide" takes you step-by-step through the creation of three different 3D video games with Microsoft XNA 4.0. Learn by doing as you explore the worlds of 3D graphics and game design.This book takes a step-by-step approach to building 3D games with Microsoft XNA, describing each section of code in depth and explaining the topics and concepts covered in detail. From the basics of a 3D camera system to an introduction to writing DirectX shader code, the games in this book cover a wide variety of both 3D graphics and game design topics. Generate random mazes, load and animate 3D models, create particle-based explosions, and combine 2D and 3D techniques to build a user interface."XNA 4 3D Game Development by Example: Beginner's Guide" will give you the knowledge to bring your own 3D game creations to life.
Table of Contents (16 chapters)
XNA 4 3D Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Time for action – drawing Speller


To draw the visual components of our Speller game , perform the following steps:

  1. 1. Alter the GraphicsDevice.Clear(Color.CornflowerBlue) call and replace Color.CornflowerBlue with Color.Black to set the background color.

  2. 2. Add the following code after the call to clear the display:

    spriteBatch.Begin();
    spriteBatch.Draw(playerSquare, playerPosition, Color.White);
    
    foreach (GameLetter letter in letters)
    {
        Color letterColor = Color.White;
    
        if (letter.WasHit)
            letterColor = Color.Red;
    
        spriteBatch.DrawString(
            letterFont, 
            letter.Letter, 
            letter.Position, 
            letterColor);
    }
    
    spriteBatch.DrawString(
        letterFont, 
        "Spell: ", 
        new Vector2(
            this.Window.ClientBounds.Width / 2 - 100,
            this.Window.ClientBounds.Height - 25), 
        Color.White);
    
    string beforeWord = currentWord.Substring(0, currentLetterIndex);
    string currentLetter = currentWord.Substring(currentLetterIndex, 1);
    string afterWord = "";
    
    if (currentWord.Length > currentLetterIndex)afterWord = currentWord.Substring(currentLetterIndex + 1);
    
    spriteBatch.DrawString(
        letterFont, 
        beforeWord, 
        new Vector2(
            this.Window.ClientBounds.Width / 2,
            this.Window.ClientBounds.Height - 25), 
        Color.Green);
    
    spriteBatch.DrawString(
        letterFont, 
        currentLetter, 
        new Vector2(
            this.Window.ClientBounds.Width / 2 +   
                letterFont.MeasureString(beforeWord).X,
            this.Window.ClientBounds.Height - 25), 
        Color.Yellow);
    
    spriteBatch.DrawString(
        letterFont, 
        afterWord, 
        new Vector2(
          this.Window.ClientBounds.Width / 2 + 
             letterFont.MeasureString(beforeWord+currentLetterIndex).X,
          this.Window.ClientBounds.Height - 25), 
        Color.LightBlue);
    
    spriteBatch.DrawString(
        letterFont, 
        "Score: " + playerScore.ToString(), 
        Vector2.Zero, 
        Color.White);
    
    spriteBatch.End();

What just happened?

When using the SpriteBatch class, any calls to draw graphics or text must be wrapped in calls to Begin() and End(). SpriteBatch.Begin() prepares the rendering system for drawing 2D graphics and sets up a specialized render state. This is necessary because all 2D graphics in XNA are actually drawn in 3D, with the projection and orientation configurations in the render state to display the 2D images properly.

In our case, the only graphical image we are drawing is the square that represents the player. We draw this with a simple call to SpriteBatch.Draw(), which requires the texture we will use, the location where the texture will be drawn on the screen (relative to the upper-left corner of the display area), and a tint color. Because our square image is white, we could set any color we wish here and the player's square would take on that color when displayed. We will use that to our advantage in just a moment when we draw the text of the word the player is trying to spell.

After the player has been drawn, we loop through each of the letters in the letters list and use the SpriteBatch.DrawString() method to draw the letter at its position, using the letterFont we created earlier. Normally, we will draw the letters in white, but if the player runs into this letter (and it is not the letter they are supposed to hit) we will draw it in red.

Next, we need to display the word that the player is attempting to spell. We display the text Spell: near the bottom center of the display, using the bounds of the current window to determine the location to draw.

In order to colorize the word properly, we need to split the word into different parts as what the player has already spelled, the current letter they are targeting, and the letters after the current letter. We do this using the Substring() method of the string class, and then draw these three components with different color tints. We utilize the MeasureString() method of letterFont to determine how much space each of these components occupies on the screen so that we can position the subsequent strings properly.

Finally, we display the player's score at the upper-left corner of the screen.