Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
XNA 4 3D Game Development by Example: Beginner's Guide

You're reading from   XNA 4 3D Game Development by Example: Beginner's Guide Create action-packed 3D games with the Microsoft XNA Framework with this book and ebook.

Arrow left icon
Product type Paperback
Published in Sep 2012
Publisher Packt
ISBN-13 9781849687089
Length 322 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Kurt Jaegers Kurt Jaegers
Author Profile Icon Kurt Jaegers
Kurt Jaegers
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

XNA 4 3D Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introduction to XNA FREE CHAPTER 2. Cube Chaser – A Flat 3D World 3. Cube Chaser – It's A-Mazing! 4. Cube Chaser – Finding Your Way 5. Tank Battles – A War-torn Land 6. Tank Battles – The Big Guns 7. Tank Battles – Shooting Things 8. Tank Battles – Ending the War 9. Mars Runner 10. Mars Runner – Reaching the Finish Line

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.

You have been reading a chapter from
XNA 4 3D Game Development by Example: Beginner's Guide
Published in: Sep 2012
Publisher: Packt
ISBN-13: 9781849687089
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image