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

Time for action – Game1 declarations

  1. Double click on the Game1.cs file in Solution Explorer to reactivate the Game1.cs code file window.
  2. Add the following declarations to the Game1 class member declaration area:
    GameBoard gameBoard;
    
    Vector2 gameBoardDisplayOrigin = new Vector2(70, 89);
    
    int playerScore = 0;
    
    enum GameStates { TitleScreen, Playing };
    GameStates gameState = GameStates.TitleScreen;
    
    Rectangle EmptyPiece = new Rectangle(1, 247, 40, 40);
    
    const float MinTimeSinceLastInput = 0.25f;
    float timeSinceLastInput = 0.0f;

What just happened?

The gameBoard instance of GameBoard will hold all of the playing pieces, while the gameBoardDisplayOrigin vector points to where on the screen the board will be drawn. Using a vector like this makes it easy to move the board in the event that you wish to change the layout of your game screen.

As we did in SquareChase, we store the player's score and will display it in the window title bar.

In order to implement a simple game state mechanism, we define two game states. When in the TitleScreen state, the game's title image will be displayed and the game will wait until the user presses the Space bar to start the game. The state will then switch to Playing, which will display the game board and allow the user to play.

If you look at the sprite sheet for the game, the pipe images themselves do not cover the entire 40x40 pixel area of a game square. In order to provide a background, an empty tile image will be drawn in each square first. The EmptyPiece Rectangle is a convenient pointer to where the empty background is located on the sprite sheet.

Just as we used an accumulating timer in SquareChase to determine how long to leave a square in place before moving it to a new location, we will use the same timing mechanism to make sure that a single click by the user does not send a game piece spinning unpredictably. Remember that the Update() method will be executing up to 60 times each second, so slowing the pace of user input is necessary to make the game respond in a way that feels natural.

Initialization

Before we can use the gameBoard instance, it needs to be initialized. We will also need to enable the mouse cursor.