CodeCombat这一款游戏我在之前已经有介绍过。简单来说,就是一个可以学习代码的游戏,这里有特定的游戏函数来训练新手熟悉代码中的基本逻辑。while循环,for循环,if判断,数组等等
今天这一关很有意思,首次使用使用hero.findEnemies()来一次吧所有的敌人都查询到一个数组里面,然后使用enemies[enemyIndex]来调用每一个敌人,一下次把效率提高了不少。
很好的游戏,让我可以继续学习。

游戏源码如下:
# Wait for ogres, defeat them and collect gold.
while True:
enemies = hero.findEnemies()
# enemyIndex is used to iterate the enemies array.
enemyIndex = 0
# While enemyIndex is less than len(enemies)
while enemyIndex < len(enemies):
# Attack the enemy at enemyIndex
enemy = enemies[enemyIndex]
hero.attack(enemy)
# Increase enemyIndex by one.
enemyIndex += 1
coins = hero.findItems()
# coinIndex is used to iterate the coins array.
coinIndex = 0
while coinIndex < len(coins):
# Get a coin from the coins array using coinIndex
coin = coins[coinIndex]
# Collect that coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Increase coinIndex by one.
coinIndex += 1