XNA 4.0 Game Development by Example: Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – adding variables to the class declaration area

  1. Right below the SpriteBatch spriteBatch; line, add the following:
    Random rand = new Random();
    Texture2D squareTexture;
    Rectangle currentSquare;
    int playerScore = 0;
    float timeRemaining = 0.0f;
    const float TimePerSquare = 0.75f;
    Color[] colors = new Color[3] { Color.Red, Color.Green, Color.Blue };

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 via 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 two dimensional 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. The Alpha component determines the transparency of the color, with a value of 0 indicating that the color is fully transparent and 255 indicating a fully opaque color. Alternatively, each component of a color can be specified as a float between 0.0f (fully transparent) and 1.0f (fully opaque).

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, 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 Game1(), and by default, it contains only two lines:

graphics = new GraphicsDeviceManager(this);
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.

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 things like the screen resolution, toggle full screen mode, and enable the 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.