网易官方」极客战记(codecombat)攻略-沙漠-跟上时间-keeping-time

(点击图片进入关卡)

筹集资金,及时支持你的盟友,来对抗食人部落!

简介

有了 time ,你可以根据过了多长时间来改变你的行动。

另外, if 语句可以和 else-if 搭配使用:

if hero.time < 10:
    # 攻击
elif hero.time < 30:
    # 收集硬币

默认代码

# 使用你的新技能来选择你要做什么 hero.time
while True:
    # 如果是头十秒,进攻。
    if hero.time < 10:
        pass
    # 反之,如果是前35秒,收集金币。
    elif hero.time < 35:
        pass
    # 后35秒,加入救助。
    else:
        pass

概览

您可以使用手表的 time 功能来确定自上次按下 运行 以来已经过了多久了。

在这一关, 在前 10 秒内,你应该协助抵抗食人魔,就像这样:

if hero.time < 10:
    # 攻击

接下来,我们使用 else-if 语句来链接 if 语句:

if hero.time < 10:
    # 攻击
elif hero.time < 30:
    # collect coins

第二个 if 语句表示,如果 time 小于 30s,则收集硬币。 但是这个 if 语句连接到第一个 if 语句 有一个 else 子句,所以硬币收集只会在第一个 if 语句是 false ,和第二个 if 语句是 true 时发生。

最后的 else 条款告诉你,当头两条 if 语句错误时,帮助盟友与食人魔战斗。

提示:如果你在最后的战斗中遇到困难,当你的生命值过低时,要让你的英雄撤退到安全的地方! 提示: 如果enemy.type 是 "palisade" ,你可能也想避免攻击。

跟上时间 解法

# 使用你的新技能来选择你要做什么 hero.time
while True:
    # 如果是头十秒,进攻。
    if hero.time < 10:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
    # 反之,如果是前35秒,收集金币。
    elif hero.time < 35:
        coin = hero.findNearestItem()
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
    # 后35秒,加入救助。
    else:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
 
本攻略发于极客战记官方教学栏目,原文地址为:
原文地址:https://www.cnblogs.com/codecombat/p/13217515.html