「网易官方」极客战记(codecombat)攻略-沙漠-掠夺者

「网易官方」极客战记(codecombat)攻略-沙漠-掠夺者

这里有好多钱有没有人要呀? 没有人我就要啦~

简介

那个前行的部队有很多金币。为了拿到手,你要打破他们的阵营!

首先,用 while 语句收集所有存在的金币。

coin = hero.findNearest(hero.findItems())

while coin:

# 收集金币

coin = hero.findNearest(hero.findItems())

然后, 当(while) 最近敌人的 health 大于 0 时一直攻击。

默认代码

# 打几下泡泡人捡走掉出的币

while True:

coin = hero.findNearestItem()

# 当存在金币时:

# 移动到金币处。

# ‘coin’应该是最近的那枚 捡到手以后要另找一枚最近的

enemy = hero.findNearestEnemy()

if enemy:

# 如果敌人还会动

# 就打它

pass

概览

while 表达式是一种有用的结构, 例如,它允许您在存在某些内容时执行一些操作。 例如,您可以在活着时观察敌人的健康状况。

while robot.health > 0:

hero.say("The robot's alive")

while-expression 允许检查一些 thangs 的存在:

weakOgre = hero.findNearestEnemy()

while weakOgre:

hero.attack(weakOgre)

hero.say("One hit, one frag! Next!")

weakOgre = hero.findNearestEnemy() #重新分配(更新)变量

掠夺者解法

# 打几下泡泡人捡走掉出的币

while True:

coin = hero.findNearestItem()

# 当存在金币时:

while coin:

# 移动到金币处。

hero.moveXY(coin.pos.x, coin.pos.y)

# ‘coin’应该是最近的那枚 捡到手以后要另找一枚最近的

coin = hero.findNearest(hero.findItems())

enemy = hero.findNearestEnemy()

if enemy:

# 如果敌人还会动

while enemy.health > 0:

# 就打它

hero.attack(enemy)


本攻略发于极客战记官方教学栏目,原文地址为:

https://codecombat.163.com/news/jikezhanji-lueduozhe

极客战记——学编程,用玩的!


分享到:


相關文章: