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 – declaring new member variables


Just after the graphics and spriteBatch declarations, add the following code snippet to include the new members:

SpriteFont letterFont;
Texture2D playerSquare;

Vector2 playerPosition;
Vector2 moveDirection; 
int playerScore;

Random rand = new Random();

string currentWord = "NONE";
int currentLetterIndex = 99;
class GameLetter
{
    public string Letter;   
    public Vector2 Position;
    public bool WasHit;
}
List<GameLetter> letters = new List<GameLetter>();
const float playerSpeed = 200.0f;

What just happened?

We have declared all of the member variables we will need for the Speller game. The letterFont member will hold the sprite font object that we added to the content project earlier, and work in conjunction with the predefined spriteBatch object to draw text on the screen.

The square image that will represent the player will be stored in the Texture2D member called playerSquare. We can use the Texture2D objects to hold graphics that we wish to draw to the screen using the SpriteBatch class.

The playerPosition Vector2 value will be used to hold the positions of the player, while moveDirection stores a vector pointing in the direction that the player is currently moving. Each time the player picks up a correct letter, playerScore will be incremented. Hitting an incorrect letter will cost the player one point.

An instance of the Random class, rand, will be used to select which word to use in each round and to place letters on the screen in random locations.

In order to keep track of which word the player is currently working on, we store that word in the currentWord variable, and track the number of letters that have been spelled in that word in currentLetterIndex.

The letters that are being displayed on the screen need several pieces of information to keep track of them. First, we need to know which letter is being displayed; next, we need to know the position the letter should occupy on the screen. Finally we need some way for our code to recognize that after we have hit an incorrect letter, we lose some of our score for it, but that we may spend several game update frames in contact with that letter and should not lose some of our score more than once for the infraction.

Note

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

All three pieces of information are wrapped into a child class of the Game1 class called GameLetter. If we were not intentionally keeping everything in Speller in the Game1 class, we would most likely create a separate code file for the GameLetter class for organizational purposes. Since Speller will be very straightforward, we will leave it inside Game1 for now.

As the GameLetter class defines a letter, we need a way to store all of the letters currently on the screen, so we have declared letters as a .NET List collection object. A List is similar to an array in that it can store a number of values of the same type, but it has the advantage that we can add and remove items from it dynamically via the Add() and RemoveAt() methods.

Finally, we declare the playerSpeed variable, which will indicate how fast the player's cube moves around the screen in response to the player's input. This value is stored in pixels per second, so in our case, one second of movement will move the character 200 pixels across 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