Time for action – adding variables to the class declaration area

  1. Right below the Private WithEvents spriteBatch As SpriteBatch line, add the following:
    Private rand As New Random()
    Private squareTexture As Texture2D
    Private currentSquare As Rectangle
    Private playerScore As Integer
    Private timeRemaining As Single
    Private Const TimePerSquare As Single = 0.75
    Private colors() As Color = {Color.Red, Color.Green, Color.Blue}

Tip

Downloading the example code and colored images

You can download the example code files and the colored images 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.

What just happened?

These are all the variables you will need for the SquareChase MINI-GAME. Here is a quick breakdown:

  • rand: This instance of the Random class is used to generate random numbers through the Next() method. You will use this to generate random coordinates for the squares that will be drawn to the screen.
  • squareTexture: The Texture2D class holds a 2D image. We will define a small texture in memory to use when drawing the square.
  • currentSquare: The XNA Framework defines a structure called Rectangle that can be used to represent an area of the display, by storing the x and y position of the upper-left corner along with a width and height. SquareChase will generate random squares and store the location in this variable.
  • playerScore: Players will score one point each time they successfully "catch" a square by clicking on it with their mouse. Their score accumulates in this integer variable.
  • timeRemaining: When a new square is generated, this float will be set to a value representing how many seconds it will remain active. When the counter reaches zero, the square will be removed and a new square generated.
  • TimePerSquare: This constant is used to set the length of time that a square will be displayed before it "runs away" from the player.
  • colors: This array of Color objects will be used when a square is drawn to cycle through the three colors in the array. The Color structure identifies a color by four components: red, green, blue, and alpha. Each of these components can be specified as a byte from 0 to 255, representing the intensity of that component in the color. Alpha represents how transparent the color is, allowing things already drawn behind it to show through. XNA drawing functions utilize pre-multiplied alpha, meaning that the alpha value has already been reflected in the other components of the color. We can accomplish this by simply creating a color with an RGB value and multiplying it by the desired alpha level we wish (between 0.0 and 1.0).

Tip

Data types

If your experience with Visual Basic is primarily VBScript-related, you may not be used to specifying data types (integer, single, Texture2D, and so on) to your variables. VBScript uses a generic "Variant" type that is interpreted by the runtime into a type that makes sense for what you are trying to do with it.

By contrast, Visual Basic .NET requires that your variables specify a data type that determines how they are allocated in memory and what you can use them for. In the case of SquareChase, we are using two basic data types:

Integer: It is a whole-number value between -2,147,483,648 and 2,147,483,647. This is equivalent to the C# type "int".

Single: This is a floating-point number. The numeric range for Singles depends on the digits of precision specified. This is equivalent to the C# type "float".

The Game1 class constructor

The XNA templates define an instance of the Microsoft.Xna.Framework.Game class with the default name Game1 as the primary component of your new game. Slightly more goes on behind the scenes, as we will see when we add an XNA game to a Windows form in Chapter 8, Gemstone Hunter: Put on your Platform Shoes, but for now, we can consider the Game1 constructor as the first thing that happens when our XNA game is executed. The class constructor is identified as Public Sub New(), and by default, the constructor contains only two lines:

graphics = New GraphicsDeviceManager(Me)
Content.RootDirectory = "Content"

For most of the games in this book, we will not need to make extensive modifications to the Game1 constructor, as its only job is to establish a link to the GraphicsDeviceManager object, and set the default directory for the Content object, which is used to load images, sound, and other game content.

Tip

Objects, classes, and methods

Many of the items that we define in our code will be classes. A Class is logical grouping of data and code. There are many built-in classes in Visual Basic and XNA, including things such as Integer and SpritBatch. Our Game1 file is itself a class definition, based on the XNA Game class. We can define new classes either from scratch, or based on existing classes. When they are based on other classes, they are said to inherit from a base class. We create instances of our class in order to use them, called objects. In the previous code snippet, the graphics object is assigned a new instance of the GraphicsDeviceManager class.

The code portions of an object are called Methods. In Visual Basic, methods are either Subs or Functions. A class can have a special Sub called New, which is the class constructor. When an instance of the class is created, the constructor is run and can be passed values to initialize the new object to a known state.

The Initialize() method

After the constructor has finished and your XNA game begins to run, the Initialize() method is called. This method only runs once, and the default code created with a new project template simply calls the base version of the method. The Initialize() method is the ideal place to set up features, such as screen resolution, toggling full screen mode, and enabling the use of a mouse in a Windows project. Other game objects that do not rely on external content, such as graphics and sound resources, can also be initialized here.