Game Logic 游戲邏輯
The final step of our game is to fill the game logic parts. We're implied the existence of a set of methods on the Game class already. Time to work out what they need to do.
譯:完成游戲的最后一步是編寫游戲邏輯部分。我們隱含了Game類的一組方法的存在。現在是時候來看看他們需要做什么了。
notifyDeath()
This method is called to indicate that the player has been killed. This can happen if an alien hits the player or an alien goes off the bottom of the screen.
譯:如果這個方法被調用,說明玩家的飛船已經被殺死。如果一個外星人擊中了玩家飛船或者一個外星人飛躍了屏幕的底部,游戲就結束了。
notifyWin()
This method is called to indicate that the player has won the game. In the current game this is a result of killing all the aliens.
譯:這個方法被調用說明玩家取得了游戲的勝利。在本游戲中游戲勝利是所有外星人被殺死的結果。
notifyAlienKilled()
Calling this method indicates that an alien has been killed as a result of a collision registered by the ShotEntity. Once all the aliens have been killed the player has won. The code looks like this:
譯:調用此方法表示一個外星人已經被 ShotEntity (子彈)被擊中而死。一旦所有的外星人都被殺死后,玩家就取得了勝利。代碼如下:
public void notifyAlienKilled() {
// reduce the alient count, if there are none left, the player has won!
//外星人數量減一,如果沒有剩余的外星人,玩家勝利
alienCount--;
if (alienCount == 0) {
notifyWin();
}
// if there are still some aliens left then they all need to get faster, so
// speed up all the existing aliens 如果還有一些剩余的外星人,他們都需要加速移動,因此加速所有存在的外星人
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
if (entity instanceof AlienEntity) {
// speed up by 2% 速度加快2%
entity.setHorizontalMovement(entity.getHorizontalMovement() * 1.02);
}
}
}
In addition to recording if all the aliens have been killed we also speed the aliens up a bit. Every time an alien is killed the rest speed up by 2%.
譯:另外,除了記錄外星人實體是否已經被殺死以外(alienCount--),我們還要使所有剩余的外星人移動速度提升一些。每當一個外星人被殺死之后其余的外星人的速度提升2%。
The last step we need is to support entity based game logic. We implied the requirement when building the AlienEntity. When a single alien detects the edge of the screen we want all the aliens to change direction. To support this we're going to add two sections of code.
譯:最后一步,我們需要基于游戲邏輯支持實體。我們在構建 AlienEntity 時隱含了一個需求:當一個單獨的外星人檢測到了屏幕的邊緣,我們希望所有的外星人改變運動的方向。為了支持這一點,我們要添加兩個代碼片段:
Entity Logic 實體的原理
Each entity should be allowed to support its own logic. To facilitate this we going to add a method to the Entity class. doLogic() will allow subclasses of Entity to define a bit of logic that will be run whenever game logic is requested (see below). In AlienEntity for instance we request that game logic be run when the edge of the screen is detected. The doLogic() implementation in AlienEntity looks like this:
譯:每一個實體應該允許支持自己的邏輯。為了實現這一點,我們要在 Entity 類中添加一個方法。doLogic() 將允許 Entity 子類定義一些邏輯,在游戲邏輯要求時執行(見下文)。例如AlienEntity ,我們要求在檢測到屏幕邊緣時運行AlienEntity 自己的游戲邏輯(向相反方向移動并向屏幕下方移動一點)。 AlienEntity 的 doLogic() 方法的實現代碼是這樣的:
public void doLogic() {
// swap over horizontal movement and move down the
// screen a bit 交換了水平運動的方向并向屏幕下方移動一點
dx = -dx;
y += 10;
// if we've reached the bottom of the screen then the player
// dies 如果外星人到達了屏幕的底部,玩家失敗
if (y > 570) {
game.notifyDeath();
}
}
So, when an alien detects the edge of the screen it signals that game logic should be run on entities. The alien entities will change direction (dx = -dx) and move down the screen a bit (y += 10). Finally, if the alien has moved off the bottom of the screen then notify the game that the player is dead.
譯:因此,當一個外星人檢測到屏幕邊緣,它應該指示所有的外星人執行游戲邏輯。這些外星人實體將會改變移動方向(dx = -dx),并且向屏幕下方移動一點(y += 10)。最后,如果外星人實體移出了屏幕的底端,通知游戲玩家已經死亡。
Entity Logic Infrastructure 實體邏輯基礎
To complete the game logic at the entity level we need to add a method on the Game class that will indicate that the entity game logic should be run. First, we add this method on Game:
譯:為了完成實體級別的游戲邏輯,我們需要在Game類中添加一個方法,用來說明實體的游戲邏輯應該被執行。首先,我們在Game類中增加這個方法:
public void updateLogic() {
logicRequiredThisLoop = true;
}
This flag indicates that in the game loop we should run the logic associated with every entity currently in the game. To achieve this we add this in the game loop:
譯:此標志表示在游戲循環中,我們當前應該執行游戲中的每一個實體的游戲邏輯(即執行doLogic())。為了實現這一點,我們在游戲循環中添加這樣的代碼:
// if a game event has indicated that game logic should
// be resolved, cycle round every entity requesting that
// their personal logic should be considered.
if (logicRequiredThisLoop) {
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.doLogic();
}
logicRequiredThisLoop = false;
}
If the flag is set, then cycle round the entities calling the doLogic() method on each. Finally, reset the flag so the logic doesn't automatically get run next loop.
譯:如果這個標志的值被設置了(置為true),那么循環調用每一個實體doLogic()方法。最后,重新設置這個變量值(置為false),以使游戲邏輯不能在下一次循環自動執行。
Finishing Off
Hopefully this tutorial has been of some help. If you look through the provided source code you'll find a selection of additions which complete the game more fully. These arn't covered in the tutorial because of their intricacy. However, the comments in the code should make the extra bits and pieces easy to understand.
譯:希望本教程能給大家一些幫助。如果您通讀了我們提供的源代碼,你會發現一些補充選擇,能使游戲更加完整。本教程沒有覆蓋它們,因為它們比較復雜。然而,代碼中的注釋應該能使那些額外的、零零碎碎的補充點容易理解。
If you have any comments or corrects feel free to mail me here
譯:如果您有任何意見或修正請點擊這里發送免費電子郵件給我。
學軟件開發,到蜂鳥科技!
超強的師資力量 、完善的課程體系 、超低的培訓價格 、真實的企業項目。
網址:www.ntcsoft.com
電話:0371-63839606
鄭州軟件開發興趣小組群:38236716
posted on 2010-11-26 00:02
whistler 閱讀(345)
評論(0) 編輯 收藏