現(xiàn)在,我們可以坐下來開始用 C++ 編寫這個 Kill Bad Alients 游戲了。首先定義幾個對象來分別代表飛船、玩家的子彈、敵人和敵人的子彈。然后再編寫代碼來繪制這些對象。還需要編寫代碼來讓這些對象可以隨著時間的推移而到處移動。另外,也需要編寫游戲邏輯、外星人 AI 以及能感知用戶擊鍵用意的代碼等等。
如下所示是另一個取自假想的 Kill Bad Alients 游戲的例子?紤]代表玩家子彈的那個對象。需要頻繁地調用函數(shù)來將其向上移動以便檢查該子彈是否會擊中任何目標。我可能會按如下所示編寫實現(xiàn)這個功能的代碼:
// This procedure moves the bullet upwards. It's called
//NUM_BULLET_MOVES_PER_SECOND times per second. It returns TRUE if the
//bullet is to be erased (because it hit a target or the top of the screen) and FALSE
//otherwise.
Boolean player_bullet::move_it()
{
Boolean is_destroyed = FALSE;
// Calculate the bullet's new position.
[Small chunk of code.]
// See if an enemy is in the new position. If so, call enemy destruction call and
// set is_destroyed to TRUE
[small chunk of code]
// See if bullet hits top of screen. If so, set is_destroyed to TRUE