posts - 0,  comments - 6,  trackbacks - 0

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 阅读(318) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿(2)

我参与的团队

文章档案(22)

搜索

  •  

最新评论