程序清單8-21 檢測游戲區(qū)域看游戲是否應該結(jié)束
/// <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函數(shù)只需要查看_playerGems[1]所在的位置是否被填滿即可。這個特殊的寶石總是位于另一個寶石之下。所以這就是我們需要檢查的空間,以確保兩個位置都為空。
如果代碼發(fā)現(xiàn)該位置被占用了,就開始執(zhí)行結(jié)束游戲的代碼,首先要將兩個玩家控制的寶石的FallSpeed設置為0,使它們不再繼續(xù)下落。然后再將GameOver屬性設置為true。設置該屬性將會使游戲的其他功能全部停止,并向用戶顯示一條消息;稍后我們將更詳細地討論這個過程。