「网易官方」极客战记(codecombat)攻略-沙漠-强壮的沙牦牛-the-mighty-sand-yak

(点击图片进入关卡)

在沙漠里通过避开强壮的沙牦牛测试你的勇气

简介

如果一只牦牛在 10 米以内,通过在 hero.pos.x 上加 10 以躲到右边!

x = hero.pos.x
y = hero.pos.y
x = x + 10
hero.moveXY(x, y)

默认代码

# 当牦牛靠近时向右移动10米来躲避
# 躲避4头牦牛完成此关
while True:
    # 使用你的灵石获取你当前的 x 和 y 位置。
    x = hero.pos.x
    y = hero.pos.y

 

    # 找到最近的耗牛。
    yak = hero.findNearestEnemy()

 

    # 使用 if 仅仅当牦牛少于10米距离的时候。
    if hero.distanceTo(yak) < 10:
        # 向右移动,添加10到你的x位置。

 

        # 使用 moveXY 来移动!

 

        pass

 

概览

hero.pos 属性代表你的英雄的当前位置。 这个 pos 对象有两个属性, x 和 y :

x = hero.pos.x
y = hero.pos.y

这些号码代表英雄在地图上的位置。

所以,如果你想将你的英雄移动 10 米到他当前位置的右侧,那将是:

x = x + 10

y 也是一样的(没有上下运动)。

用 hero.moveXY(x, y) 移动到新的坐标。

有时你可能会看到这一切都写在一行上,比如:

hero.moveXY(hero.pos.x + 10, hero.pos.y)

强壮的沙牦牛 解法

# 当牦牛靠近时向右移动10米来躲避
# 躲避4头牦牛完成此关
while True:
    # 使用你的灵石获取你当前的 x 和 y 位置。
    x = hero.pos.x
    y = hero.pos.y

 

    # 找到最近的耗牛。
    yak = hero.findNearestEnemy()

 

    # 使用 if 仅仅当牦牛少于10米距离的时候。
    if hero.distanceTo(yak) < 10:
        # 向右移动,添加10到你的x位置。
        x += 10
        # Use moveXY to move!
        hero.moveXY(x, y)
 
原文地址:https://www.cnblogs.com/codecombat/p/12966959.html