程序清单8-21 检测游戏区域看游戏是否应该结束
/// <summary>
/// Check to see whether the player gem board position is occupied by
/// a gem already on the board. If this is the case when the player
/// gems are first added, the board is full and the game is over.
/// </summary>
/// <remarks>If the game is found to be over, the GameOver property
/// will be set to true.</remarks>
private void CheckGameOver()
{
// Are the positions to which the player gems have been added already
// occupied?
if (_gameBoard[_playerGems[1].BoardXPos, _playerGems[1].BoardYPos] != null)
{
// They are, so the game is finished...
// Stop the player gems from moving
_playerGems[0].FallSpeed = 0;
_playerGems[1].FallSpeed = 0;
// Initialize the game over sequence
GameOver = true;
}
}
假设游戏还在运行,我们就生成两个新的宝石,颜色随机。设置完颜色后,还需要对下一对宝石中每个宝石的HasMoved属性进行设置,这样游戏引擎才知道要对它们进行重绘。
将_piecesDropped变量的值增加后,对新的玩家控制的宝石所要做的初始化工作就完成了。
CheckGameOver函数只需要查看_playerGems[1]所在的位置是否被填满即可。这个特殊的宝石总是位于另一个宝石之下。所以这就是我们需要检查的空间,以确保两个位置都为空。
如果代码发现该位置被占用了,就开始执行结束游戏的代码,首先要将两个玩家控制的宝石的FallSpeed设置为0,使它们不再继续下落。然后再将GameOver属性设置为true。设置该属性将会使游戏的其他功能全部停止,并向用户显示一条消息;稍后我们将更详细地讨论这个过程。