【网易官方】极客战记(codecombat)攻略-森林-金属探测器metal-detector

如果黄金遇上食人魔,食人魔将跑向黄金。

简介

那个食人魔营地受到很好的保护,但是我们可以减少食人魔的数量。 我们把大炮藏在森林里,准备金币。 我们的计划很简单:一枚硬币出现了,一个怪物在为硬币奔跑,我们向硬币射击,完成了。

你的任务是做测距仪。等一枚硬币,然后 say``distanceTo() 硬币。

默认代码

# 炮兵使用硬币作为目标。
# 你将是炮兵的测距仪。
# 编写函数。
def coinDistance():
    # 找到最近的硬币,

 

    # 如果有一枚硬币,返回它的距离。

 

    # 否则,返回0(零)。

 

    pass
while True:
    distance = coinDistance()
    if distance > 0:
        # 说`distance`。

 

        pass

概览

您已经了解了以前关卡的函数 return 。 现在是证明你的知识的时候了! 完成函数,不要忘了 return 。

为了获得一个项目的距离,使用眼镜方法 hero.distanceTo(item) )。 如果没有硬币,你的函数应该返回0(零)。

金属探测器 解法

# 炮兵使用硬币作为目标。
# 你将是炮兵的测距仪。
# 编写函数。
def coinDistance():
    # 找到最近的硬币,
    coin = hero.findNearestItem()
    # 如果有一枚硬币,返回它的距离。
    if coin:
        distance = hero.distanceTo(coin)
        return distance
    # 否则,返回0(零)。
    else:
        return 0
while True:
    distance = coinDistance()
    if distance > 0:
        # 说`distance`。
        hero.say(distance)
 
 
本攻略发于极客战记官方教学栏目,原文地址为:

猜你喜欢

转载自www.cnblogs.com/codecombat/p/12355670.html
今日推荐