【网易官方】极客战记(codecombat)攻略-森林-去拿过来go-fetch

你陷入了设计好的陷阱中!派你的宠物去寻求帮助!

简介

你的宠物知道怎么拿取物品!

item = hero.findNearestItem()
if item:
    pet.fetch(item)

默认代码

# 你被困在了陷阱中!
# 派遣宠物拿取治疗药水!
def goFetch():
    # 你可以在处理函数中使用循环。
    while True:
        potion = hero.findNearestItem()
        if potion:
            # 用 “pet.fetch()” 去让你的宠物捡药水:

 

            pass
# 当宠物被召唤出来时,会触发 "spawn" 事件。
# 这让你的宠物在关卡开始时运行 goFetch()函数。
pet.on("spawn", goFetch)

概览

留意你的宠物只能拿到特定种类的物品,比如药水。

这关还有个注意的地方是 "spawn" (生成)事件。

之前你用的是 "hear" 事件,让宠物对听到的东西做出反应。

而 "spawn" 事件只会在关卡开始后,宠物被召唤出来时发生。

虽然 "spawn" 事件只发生一次,但你还是可以在事件处理程序中使用 while 循环重复执行代码。

去拿过来 解法

# 你被困在了陷阱中!
# 派遣宠物拿取治疗药水!
def goFetch():
    # 你可以在处理函数中使用循环。
    while True:
        potion = hero.findNearestItem()
        if potion:
            # 用 “pet.fetch()” 去让你的宠物捡药水:
            pet.fetch(potion)
# 当宠物被召唤出来时,会触发 "spawn" 事件。
# 这让你的宠物在关卡开始时运行 goFetch()函数。
pet.on("spawn", goFetch)
 
 
本攻略发于极客战记官方教学栏目,原文地址为:
原文地址:https://www.cnblogs.com/codecombat/p/12272778.html