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 – 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.