程序清单8-20 对一对新的由玩家控制的宝石对象进行初始化
/// <summary>
/// Create two new gems for the player to control.
/// </summary>
private void InitPlayerGems()
{
// Instantiate and initialize two new gems for the player to control.
// Set the gem colors to be those colors stored for the next gems.
_playerGems[0] = new CObjGem(this, BOARD_GEMS_ACROSS / 2, 0,
_playerNextGems[0].GemColor);
_playerGems[1] = new CObjGem(this, BOARD_GEMS_ACROSS / 2, 1,
_playerNextGems[1].GemColor);
// These are the player controlled gems
_playerGems[0].GemType = CObjGem.GemTypes.PlayerControlled;
_playerGems[1].GemType = CObjGem.GemTypes.PlayerControlled;
// Set the gems as falling into the position we have set
_playerGems[0].FallDistance = 1;
_playerGems[1].FallDistance = 1;
// Set the drop speed to increase based on the number of pieces already
// dropped.
_playerGems[0].FallSpeed = 0.02f + (_piecesDropped * 0.0004f);
_playerGems[1].FallSpeed = _playerGems[0].FallSpeed;
// Add the gems to the game
GameObjects.Add(_playerGems[0]);
GameObjects.Add(_playerGems[1]);
// Check that the board space is actually available.
// If not, the game is finished.
CheckGameOver();
if (GameOver)
{
// The game is finished, so no further work is required here
return;
}
// Set two new 'next' gems
_playerNextGems[0].GemColor = GenerateRandomGemColor();
_playerNextGems[1].GemColor = GenerateRandomGemColor();
// Flag the next gems as having moved so that they are re-rendered in
// their new colors.
_playerNextGems[0].HasMoved = true;
_playerNextGems[1].HasMoved = true;
// Increase the pieces dropped count
_piecesDropped += 1;
}