程序清單8-11 將游戲區(qū)域清空
/// <summary>
/// Remove all of the gems present on the board.
/// </summary>
private void ClearBoard()
{
for (int x=0; x<BOARD_GEMS_ACROSS; x++)
{
for (int y=0; y<BOARD_GEMS_DOWN; y++)
{
// Does this location contain a gem?
if (_gameBoard[x,y] != null)
{
// Yes, so instruct it to terminate and then remove it from the
// board
_gameBoard[x, y].Terminate = true;
_gameBoard[x,y] = null;
}
}
}
}
UpdateForm函數可以將任何所需的信息從游戲引擎復制到游戲窗體中。我們唯一需要復制的是玩家的得分,但以后還有其他信息要添加的話(如生命數、能量、導彈數量等),將它們集中到一個函數中是很有用的。每當這些變量中的一個發(fā)生變化時,只需要調用該函數就可以將變化顯示給玩家。
當修改了玩家的得分后(將它重置為0),必須從Reset函數中調用UpdateForm函數,UpdateForm函數如程序清單8-12所示。
程序清單8-12 使用游戲中的信息對窗體進行更新
/// <summary>
/// Copy information from the game on to the game form so that it can be
/// seen by the player.
/// </summary>
private void UpdateForm()
{
// Make sure we have finished initializing and we have a form to update
if (_gameForm != null)
{
// Set the player score
_gameForm.SetScore(_playerScore);
}
}