「网易官方」极客战记(codecombat)攻略-山峰-激流回旋-slalom

(点击图片进入关卡)

避免陷阱小心的收集宝石。

简介

当使用 “移动” 时,需要构造一个对象文字作为参数传递。

您可以指定这样的对象文字: pos = { "x": 20, "y": 35 } 。

对象文字是由与之相关联的键组成的。例如 pos.x 将返回‘20’,而 pos.y 将返回‘35’。

默认代码

# 使用对象枚举来走安全的路,并收集宝石。
# 在本关你不能够使用 moveXY()方法!使用 move()来移动
gems = hero.findItems()
while hero.pos.x < 20:
    # move()移动物体通过 x 和 y 的属性,不仅仅是数字。
    hero.move({'x': 20, 'y': 35})
while hero.pos.x < 25:
        # 一个宝石的位置是一个对象,有 x 和 y 属性。
        gem0 = gems[0]
        hero.move(gem0.pos)
# 当你的 x 小于30的时候,
# 使用物体移动到30,35位置
# 当你的 x 小于35的时候
# 移动到宝石[1]的位置
# 拿到最后一对宝石!

概览

当使用 “move” 时,可能需要构造一个对象文字作为参数传递。

# In Python, an object literal is also called a dictionary
ob = { "x": 20, "y": 35 }

记住 move 不会阻止你的代码执行吗? 在这个关卡中,当 pos.x 小于某个数字时,您将使用 while 循环继续向一个位置移动。

要移动到 X 标记,请将对象文字传递给 move 方法。

要移动到宝石,请使用宝石的 post 对象作为 move 的参数。

激流回旋解法

# 使用对象枚举来走安全的路,并收集宝石。
# 在本关你不能够使用 moveXY()方法!使用 move()来移动
gems = hero.findItems()
while hero.pos.x < 20:
    # move()移动物体通过 x 和 y 的属性,不仅仅是数字。
    hero.move({'x': 20, 'y': 35})
while hero.pos.x < 25:
        # 一个宝石的位置是一个对象,有 x 和 y 属性。
        gem0 = gems[0]
        hero.move(gem0.pos)
# 当你的 x 小于30的时候,
# 使用物体移动到30,35位置
while hero.pos.x < 30:
    hero.move({'x': 30, 'y': 35})
# 当你的 x 小于35的时候
# 移动到宝石[1]的位置
while hero.pos.x < 35:
    gem1 = gems[1]
    hero.move(gem1.pos)
# 拿到最后一对宝石!
while hero.pos.x < 40:
    hero.move({'x': 40, 'y':35})
while hero.pos.x < 45:
    gem2 = gems[2]
    hero.move(gem2.pos)
while hero.pos.x < 50:
    hero.move({'x': 50, 'y':35})
while hero.pos.x < 55:
    gem3 = gems[3]
    hero.move(gem3.pos)
 
本攻略发于极客战记官方教学栏目,原文地址为:
原文地址:https://www.cnblogs.com/codecombat/p/13552129.html