程序清單8-18 聲明_playerGems數(shù)組及_playerNextGems數(shù)組
// Declare an array to hold the pair of gems that are dropping under player
// control
private CObjGem[] _playerGems = new CObjGem[2];
// Declare an array to hold the next gems that will be brought into play
private CObjGem[] _playerNextGems = new CObjGem[2];
我們每次添加一對(duì)玩家控制的寶石時(shí),就從_playerNextGems數(shù)組中將它們的顏色復(fù)制過來。這樣可以確保預(yù)告的下一對(duì)來到的寶石的實(shí)際顏色。為了確保寶石的預(yù)覽信息能夠出現(xiàn)在游戲的右上方,在生成玩家控制的寶石之前就對(duì)它執(zhí)行初始化。
預(yù)覽寶石是在名為InitNextGems的函數(shù)中進(jìn)行初始化的,如程序清單8-19所示。在游戲的Reset函數(shù)中添加對(duì)該函數(shù)的調(diào)用。
程序清單8-19 對(duì)Next Gem對(duì)象進(jìn)行初始化
/// <summary>
/// Create the two "next piece" gems to display in the corner of the screen.
/// This should be called just once per game as it is being reset.
/// </summary>
private void InitNextGems()
{
// Instantiate two new gems.
// The gems have Y positions of 0 and 1 so that they appear one above
// the other in the Next Piece display.
// We also generate initial random colors for the two gems here too.
_playerNextGems[0] = new CObjGem(this, 0, 0, GenerateRandomGemColor());
_playerNextGems[1] = new CObjGem(this, 0, 1, GenerateRandomGemColor());
// These are the 'next' gems -- this affects their position within the
// screen
_playerNextGems[0].GemType = CObjGem.GemTypes.NextGem;
_playerNextGems[1].GemType = CObjGem.GemTypes.NextGem;
// Add the gems to the game
GameObjects.Add(_playerNextGems[0]);
GameObjects.Add(_playerNextGems[1]);
}
既然我們知道了下一對(duì)出現(xiàn)的寶石使用的顏色,就可以對(duì)玩家控制的寶石進(jìn)行初始化。這些操作已經(jīng)創(chuàng)建在一個(gè)名為InitPlayerGems的函數(shù)中,如程序清單8-20所示。在Reset函數(shù)中,當(dāng)調(diào)用了InitNextGems函數(shù)后也調(diào)用了它。游戲開始后,每次為玩家顯示一對(duì)新的寶石時(shí)都會(huì)調(diào)用它;稍后您就會(huì)看到如何對(duì)它進(jìn)行調(diào)用。