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.