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 – words and letters


To implement the CheckForNewWord() and its helper methods, we will perform the following steps:

  1. 1. Add the PickAWord() method to the end of the Game1 class, after Draw():

    private string PickAWord()
    {
        switch (rand.Next(15))
        {
            case 0: return "CAT"; 
            case 1: return "DOG"; 
            case 2: return "MILK";
            case 3: return "SUN";
            case 4: return "SKY";
            case 5: return "RAIN";
            case 6: return "SNOW";
            case 7: return "FAR";
            case 8: return "NEAR";
            case 9: return "FRIEND";
            case 10: return "GAME";
            case 11: return "XNA";
            case 12: return "PLAY";
            case 13: return "RUN";
            case 14: return "FUN";
        }
    
        return "BUG";
    }
  2. 2. Add the FillLetters() method to the Game1 class, after PickAWord():

    private void FillLetters(string word)
    {
      Rectangle safeArea = new Rectangle(
        this.Window.ClientBounds.Width / 2 - playerSquare.Width,
        this.Window.ClientBounds.Height / 2 - playerSquare.Height,
        playerSquare.Width * 2,
        playerSquare.Height * 2);
    
      string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
      List<Vector2> locations = new List<Vector2>();
      for (int x=25;x < this.Window.ClientBounds.Width - 50;x += 50)
      {
        for (int y=25;y < this.Window.ClientBounds.Height - 50;y += 50)
        {
          Rectangle locationRect = new Rectangle(
            x,
            y,
            (int)letterFont.MeasureString("W").X,
            (int)letterFont.MeasureString("W").Y);
    
          if (!safeArea.Intersects(locationRect))
          {
            locations.Add(new Vector2(x, y));
          }
        }
      }
    
      letters.Clear();
      for (int x = 0; x < 20; x++)
      {
        GameLetter thisLetter = new GameLetter();
    
        if (x < word.Length)
            thisLetter.Letter = word.Substring(x, 1);
        else
            thisLetter.Letter = alphabet.Substring(
                rand.Next(0,26),1);
    
            int location = rand.Next(0,locations.Count);
            thisLetter.Position = locations[location];
            thisLetter.WasHit = false;
            locations.RemoveAt(location);
      
            letters.Add(thisLetter);
        }
    
    }
  3. 3. Add the CheckForNewWord() method to the end of the Game1 class, after FillLetters():

    private void CheckForNewWord()
    {
        if (currentLetterIndex >= currentWord.Length)
        {
            playerPosition = new Vector2(
                this.Window.ClientBounds.Width / 2,
                this.Window.ClientBounds.Height / 2);
            currentWord = PickAWord();
            currentLetterIndex = 0;
            FillLetters(currentWord);
        }
    }

What just happened?

In step 1, we generate a random number using the Next() method of the Random class. Given an integer value, Next() will return an integer between zero and that number minus one, meaning we will have a return value from zero to fourteen. Using a select statement, we return the randomly determined word. Note that we should never hit the last return statement in the function, so if we are ever asked to spell the word BUG, we know something is wrong.

The FillLetters() method is used to populate the letters list with letters and their locations on the screen. We could simply generate random locations for each letter, but then this would leave us with the potential for letters overlapping each other, requiring a check as each letter is generated to ensure this does not happen.

Instead, we will generate a list of potential letter positions by building the locations list. This list will contain each of the possible places on the screen where we will put a letter by spacing through a grid and adding entries every 25 pixels in the x and y directions. The exception is that we define an area in the center of the screen where the player will start and we will not place letters. This allows the player to start each round without being in contact with any of the game letters.

Once we have our list of locations, we clear the letters list and generate 20 letters. We start with the letters required to spell the target word, pulling letters from the currentWord string until we reach the end. After that, the letters will come from the alphabet string randomly. Each letter is assigned one of the locations from the locations list, and that location is then removed from the list so we will not have two letters on top of each other.

Lastly, the CheckForNewWord() method checks to see if currentLetterIndex is larger than the length of currentWord. If it is, the player's position is reset to the center of the screen and a new word is generated using PickAWord(). currentLetterIndex is reset, and the letters list is rebuilt using the FillLetters() method.

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