「网易官方」极客战记(codecombat)攻略-沙漠-Sarven前哨基地-sarven-outpost

(点击图片进入关卡)

在沙漠中的前哨基地巡逻。避开食人魔,注意你的表!

简介

样例代码显示了如何根据时间在一个圆中巡逻。

在每次移动之后,巡逻状态下继续寻找并击败任何敌人。

默认代码

# 怪物正在攻击附近的前哨基地!
# 指挥英雄保卫定居点。
# 巡逻时带上你的手表,没有食人魔可以通过。

 

while True:
    polarPos = hero.time / 4
    # 数在20到60之间。
    xPos = 40 + Math.cos(polarPos) * 20
    # 数在14到54之间。
    yPos = 34 + Math.sin(polarPos) * 20
    hero.moveXY(xPos, yPos)
    # 寻找食人魔并打败他们!
    # 当他们的健康在0以上时,确保进行攻击。

概览

在沙漠巡逻是一项艰巨的工作。以时间为基础的巡逻必须依靠英雄的手表来完成这一任务。

为了帮助理解运动代码是什么,请考虑以下部分:

polarPos = hero.time / 4
xPos = 40 + Math.cos(polarPos) * 20
yPos = 34 + Math.sin(polarPos) * 20

记住,‘循环’可以用来检查一个条件,直到它是真的为止。例如,痊愈直到75%以上的血量:

while hero.health < hero.maxHealth * 3 / 4:
    hero.say("I need more heals!")

Sarven前哨基地 解法

# 怪物正在攻击附近的前哨基地!
# 指挥英雄保卫定居点。
# 巡逻时带上你的手表,没有食人魔可以通过。

 

while True:
    polarPos = hero.time / 4
    # 数在20到60之间。
    xPos = 40 + Math.cos(polarPos) * 20
    # 数在14到54之间。
    yPos = 34 + Math.sin(polarPos) * 20
    hero.moveXY(xPos, yPos)
    # 寻找食人魔并打败他们!
    # 当他们的健康在0以上时,确保进行攻击。
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
 
本攻略发于极客战记官方教学栏目,原文地址为:
 
原文地址:https://www.cnblogs.com/codecombat/p/13259197.html