[Python] Codecombat攻略 远边的森林 Forest (1-40关)

首页:https://cn.codecombat.com/play
语言:Python

第二界面:远边的森林Forest(40关)
时间:2-6小时
内容:if/else、关系操作符、对象属性、处理输入
网页:https://cn.codecombat.com/play/forest

闯关:
第1关:森林保卫战
子网页:https://cn.codecombat.com/play/level/defense-of-plainswood?

# 建立两个围栏保护村民
# 把鼠标放在地图上得到X,Y坐标

self.buildXY("fence", 40, 52);
self.buildXY("fence", 40, 20);

第2关:羊肠小道
子网页:https://cn.codecombat.com/play/level/winding-trail?

# 到小路的尽头去,并在那儿修一个栅栏。
# 利用你的 moveXY(x, y)坐标移动功能。

# It's the first point of the path.
hero.moveXY(36, 59)
# Move at the next points of the path.
hero.moveXY(37, 12)
# Build a fence to stop the ogre.
hero.buildXY("fence", 72, 25)

 

第3关:丛林里的间隔
子网页:https://cn.codecombat.com/play/level/woodland-cubbies?

# 在丛林里头探索,但务必提高警觉!
# 这些丛林角落隔间可能会藏有ogres!

hero.moveXY(19, 33)
enemy = hero.findNearestEnemy()
# 条件判断式将会检查该变数是否参考到一个ogre
if enemy:
    hero.attack(enemy)
    hero.attack(enemy)

hero.moveXY(49, 51)
enemy = hero.findNearestEnemy()
if enemy:
    # 在这里撰写攻击敌人指令
    hero.attack(enemy)
    hero.attack(enemy)
    # pass没有特别的意思,只是用来协助结束条件判断式,写不写都可以
    pass

hero.moveXY(58, 14)
enemy = hero.findNearestEnemy()
# 使用条件判断式来确认敌人是否存在
if enemy:
    # 如果敌人存在就攻击他
    hero.attack(enemy)
    hero.attack(enemy)

第4关:If-stravaganza
子网页:https://cn.codecombat.com/play/level/if-stravaganza?

# 消灭从他们自己的营地里出来的食人魔

while True:
    enemy = hero.findNearestEnemy()
    # 使用一个 “if” 语句去检查是否有敌人存在:
    if enemy:
        # 攻击敌人如果它存在的话
        hero.attack(enemy)
        hero.attack(enemy)

第5关:背靠背
子网页:https://cn.codecombat.com/play/level/back-to-back?

# 呆在中间防守!

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # 亦或主动出击...
        hero.attack(enemy)
        hero.attack(enemy)
        pass
    else:
        # 亦或回到你的阵地防守。
        hero.moveXY(40, 34)
        pass

 

第6关:森林劈裂者
子网页:https://cn.codecombat.com/play/level/woodland-cleaver?

# 尽可能经常使用你的新技能“cleave”

hero.moveXY(23, 23)
while True:
    enemy = hero.findNearestEnemy()
    if hero.isReady("cleave"):
        # “Cleave”掉敌人!
        hero.cleave(enemy)
        pass
    else:
        # 否则(如果“cleave”还没准备好),就用你的普通攻击
        hero.attack(enemy)
        pass

  

第7关:边远地区的对峙
子网页:https://cn.codecombat.com/play/level/backwoods-standoff?

# 这些曼切堪食人魔害怕英雄!
# 说些什么,他们会吓得往后退。
# 但是,有足够的曼切堪食人魔,他们将联合起来伏击你!小心!
# 每当`cleave`(横劈)冷却时间完成,立即用它清除敌人。

while True:
    # 使用 ‘isReady’ 中的一个 “if-statement” 的语句来检查 “cleave”
    if hero.isReady("cleave"):
        # 劈斩!
        hero.cleave()
    # 或者,如果 cleave 还没准备好的话:
    else:
        # 说一点什么来吓走曼切堪食人魔
        hero.say("Bool!")
    pass

第8关:测距仪
子网页:https://cn.codecombat.com/play/level/range-finder?

# 瘦人正在森林里头巡逻!
# 使用distanceTo方法来计算敌人与英雄间的距离
# 说出每个敌人和英雄间的距离以告知大砲要轰炸哪里

enemy1 = "Gort"
distance1 = hero.distanceTo(enemy1)
hero.say(distance1)

enemy2 = "Smasher"
# 将distance2变数作为参数,传入say()方法
distance2 = hero.distanceTo(enemy2)
# 测量并说出剩余敌人与英雄间的距离
hero.say(distance2)

# 不要向你的友军进行射击!
enemy3 = "Charles"

enemy4 = "Gorgnub"
distance4 = hero.distanceTo(enemy4)
hero.say(distance4)

 

第9关:保护农民
子网页:https://cn.codecombat.com/play/level/peasant-protection?

while True:
    enemy = hero.findNearestEnemy()
    distance = hero.distanceTo(enemy)
    if distance < 10:
        # 如果他们与农民太近,就攻击他们
        hero.attack(enemy)
        pass
    # 否则的话,呆在农民旁边!
    else:
        hero.moveXY(40, 37)

第10关:疯狂的食人魔
子网页:https://cn.codecombat.com/play/level/maniac-munchkins?

# 地上另一个让英雄打开的宝箱!
# 攻击宝箱以打开它
# 有些食人魔可不会呆呆地站着挨打!
# 当食人魔离你太近时,你得学着保护你自己

while True:
    enemy = hero.findNearestEnemy()
    distance = hero.distanceTo(enemy)
    # 首先,定期使用旋风斩(cleave)当技能就绪的时候:
    if hero.isReady("cleave"):
        hero.cleave(enemy)
        pass
    # 攻击靠近并离你最近的食人魔
    elif distance < 5:
        hero.attack(enemy)
        pass
    # 否则,试着打破宝箱看看:
    else:
        hero.attack("Chest")
        pass

 

第11关:跃火林中
子网页:https://cn.codecombat.com/play/level/forest-fire-dancing?

# 在这关,别碰恶魔石!往其他方向移动避开它们!
while True:
    evilstone = hero.findNearestItem()
    if evilstone:
        pos = evilstone.pos
        # 如果恶魔石在左边,走到右边。
        if pos.x == 34:
            hero.moveXY(43, 17)
            pass
        else:
            # 如果恶魔石在右边,走到左边。
            if pos.x == 46:
                hero.moveXY(37, 17)
            pass
    else:
        # If there's no evilstone, go to the middle.
        if pos.x == '':
            hero.moveXY(43,22 )
        pass

 

第12关:村庄漫游者
子网页:https://cn.codecombat.com/play/level/village-rover?

# This defines a function called findAndAttackEnemy
def findAndAttackEnemy():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)

# This code is not part of the function.
while True:
    # Now you can patrol the village using findAndAttackEnemy
    hero.moveXY(35, 34)
    findAndAttackEnemy()
    
    # Now move to the right entrance.
    hero.moveXY(60, 31)
    # Use findAndAttackEnemy
    findAndAttackEnemy()

 

第13关:边远地区交叉口
子网页:https://cn.codecombat.com/play/level/backwoods-fork?

# 一大波兽人正在到来!
# 使用 checkAndAttack 函数让代码易读。

# 这个函式有一个参数
# 参数是一种给函数传递信息的方式。
def checkAndAttack(target):
    # 'target'参数只是一个变数!
    # 它包含了函数调用时的参数。
    if target:
        hero.attack(target)
    hero.moveXY(43, 34)

while True:
    hero.moveXY(58, 52)
    topEnemy = hero.findNearestEnemy()
    checkAndAttack(topEnemy)
    
    # 移动到底部的X标记处。
    hero.moveXY(58, 16)
    # 创建名为 bottomEnemy 的变量,寻找最近敌人。
    bottomEnemy = hero.findNearestEnemy()
    # 使用 checkAndAttack 函数,并包括 bottomEnemy 变量。
    checkAndAttack(bottomEnemy)

 

第14关:无敌旋风斩
子网页:https://cn.codecombat.com/play/level/leave-it-to-cleaver?

# 這裡顯示如何定義一個稱為cleaveWhenClose的函式
# 函式定義了一個參數稱為target
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # 把你的攻擊程式碼放在這裡
        # 如果旋風斬就緒,對目標施放旋風斬
        if hero.isReady("cleave"):
            hero.cleave(target)
        # 否則,直接攻擊目標!
        else:
            hero.attack(target)

# 這裡的程式碼不是函式的一部份.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # 記得在cleaveWhenClose函式裡面,我們改用target作為指向敵人的變數.
        cleaveWhenClose(enemy)

第15关:边远地区小伙伴
子网页:https://cn.codecombat.com/play/level/backwoods-buddy?

# You now have a pet!

def speak(event):
    # 你的宠物应该用pet.say()来回应
    pet.say("Meow!")
    pass

# 这告诉你的宠物去执行speak()函式当她听到什么
pet.on("hear", speak)
# 和你的宠物说话!
hero.say("Hello Kitty")

 

第16关:去接住
子网页:https://cn.codecombat.com/play/level/go-fetch? 

# 你被结实的陷阱给困住了!
# 派出你的宠物去拿回治疗药水!

def goFetch():
    # 你可以在事件处理函式里面使用回圈.
    while True:
        potion = hero.findNearestItem()
        if potion:
            # 用 “pet.fetch()” 去让你的宠物捡药水:
            pet.fetch(potion)
            pass

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

 

第17关:朋友与敌人
子网页:https://cn.codecombat.com/play/level/friend-and-foe?

# 农民和士兵聚集在森林。
# 命令农民战斗,苦工远离!

while True:
    friend = hero.findNearestFriend()
    if friend:
        hero.say("To battle, " + friend.id + "!")
    # 寻找最近的敌人,然后让他们滚蛋
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.say("Go away,"+enemy.id+"!")

第18关:巫师之门
子网页:https://cn.codecombat.com/play/level/the-wizards-door?

# 去找Laszlo并取得他的密码数字.
hero.moveXY(30, 13)
las = hero.findNearestFriend().getSecret()

# 向 Laszlo 的数字中加7就能得到 Erzsebet的号码
# 去找Erzsebet并告诉她魔法数字.
erz = las + 7
hero.moveXY(17, 26)
hero.say(erz)

# 将 Erzsebet 的数字除以 4 得到 Simoyi 的数字。
# 去找Simonyi并告诉他数字.
sim = erz / 4
hero.moveXY(30, 39)
hero.say(sim)

# 将 Simonyi 的数字乘以 Laszlo 的数字得到 Agata 的数字。
# 去找Agata并告诉她数字.
aga = sim * las
hero.moveXY(43, 26)
hero.say(aga)

第19关:宝库追逐战
子网页:https://cn.codecombat.com/play/level/coincrumbs?

# 跟随硬币的轨迹来到红色 X 标记的出口

while True:
    # 这能找到最近的敌人。
    item = hero.findNearestItem()
    if item:
        # 这将物品的 pos,就是坐标,存储在变量中。
        itemPosition = item.pos
        # 将物品的 X 和 Y 坐标放进变量。
        itemX = itemPosition.x
        itemY = itemPosition.y
        # 现在,使用moveXY来移动到itemX和itemY:
        hero.moveXY(itemX, itemY)

第20关:看不见的距离
子网页:https://cn.codecombat.com/play/level/blind-distance?

# 你的任务是告诉他兽人的距离。

# 这个函数寻找最近的敌人,并返回距离。
# 假如没有敌人,函式会回传0.
def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 0
    if enemy:
        result = hero.distanceTo(enemy)
    return result

while True:
    # 呼叫 nearestEnemyDistance() 和
    # 储存结果到 enemyDistance变数里头.
    enemyDistance = nearestEnemyDistance()
    # 假如 enemyDistance 大于0:
    if enemyDistance > 0:
        # 说出 enemyDistance变数的值.
        hero.say(enemyDistance)

 

第21关:困兽之斗
子网页:https://cn.codecombat.com/play/level/hit-and-freeze?

# 你掉进陷阱里了!别动!你会受伤的!

# 这个函数检查敌人是否在攻击范围。
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    # 几乎所有的剑攻击距离都是3.
    if distance <= 3:
        return True
    else:
        return False

# 只有兽人在范围内才攻击他们.
while True:
    # 找到最近的敌人,并将其储存在一个变量中。
    enemy = hero.findNearestEnemy()
    # 调用 inAttackRange(enemy),将 enemy 作为参数
    # 把结果保存于 “canAttack” 变量中
    canAttack = inAttackRange(enemy)
    # 假如储存在 canAttack的结果是 True, 然后下手!
    if canAttack:
        hero.attack(enemy)
    pass

简化版:

while True:
    enemy = hero.findNearestEnemy()
    if hero.distanceTo(enemy) <= 3:
        hero.attack(enemy)

第22关:盐碱地
子网页:https://cn.codecombat.com/play/level/salted-earth?

# 兽人正在攻击附近的殖民地!
# 小心,兽人在地上放了毒药。
# 收集硬币并打败兽人,但是要避开毛怪(burls)和毒药!

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
        
    item = hero.findNearestItem()
    # 检查物品类型,确保英雄不会捡起毒药!
    # 寻找类型:'gem' 和 'coin'
    if item.type == "gem" or item.type == "coin":
        pos = item.pos
        x = pos.x
        y = pos.y
        hero.moveXY(x, y)

第23关:春雷
子网页:https://cn.codecombat.com/play/level/spring-thunder?

# 某些金币和宝石会吸引闪电.
# 这个英雄应只收集银币和蓝宝石

while True:
    item = hero.findNearestItem()
    # 银币的价值为2.
    # 收集该物品,如果其类型等于"coin"
    # AND item.value 相等于2
    if item.type == "coin" and item.value == 2:
        hero.moveXY(item.pos.x, item.pos.y)
    # 一个蓝宝石价值10
    # 收集该物品,如果其类型等于"gem"
    # AND item.value等于10.
    if item.type == "gem" and item.value == 10:
        hero.moveXY(item.pos.x, item.pos.y)

第24关:平常的一天
子网页:https://cn.codecombat.com/play/level/usual-day?

# 打败食人魔,收集金币。一切都那么平常。
# 使用 与(AND) 在同一行检查存在性和类型。

while True:
    enemy = hero.findNearestEnemy()
    # 有了与(AND),只在敌人存在时检查类型
    if enemy and enemy.type == "munchkin":
        hero.attack(enemy);
    # 寻找最近的物品
    item = hero.findNearestItem()
    # 如果有名为 “coin” (金币)的物品存在,那就快去收集它!
    if item and item.type == "coin":
        hero.moveXY(item.pos.x, item.pos.y)

第25关:穿越
子网页:https://cn.codecombat.com/play/level/passing-through?

# 不要冒犯友善兽人的仪式

while True:
    item = hero.findNearestItem()
    if item:
        # 如果物品的类型不等于"gem"
        if item.type != "gem":
            # 然后跟随你的宠物。
            hero.moveXY(pet.pos.x, pet.pos.y)
        # 否则:
        else:
            # 移动到宝石的坐标。
            hero.moveXY(item.pos.x, item.pos.y)

第26关:有用的对手
子网页:https://cn.codecombat.com/play/level/useful-competitors?

# 这片金币地中暗藏了致命的毒药。
# 兽人正在进攻,而苦力尝试偷你的金币!
# 只在敌人类型不是 "peon" 的时候攻击。

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if enemy.type != "peon":
            hero.attack(enemy)
    item = hero.findNearestItem()
    if item:
        # 只在物品的类型不是 "poison" 的时候收集。
        if item.type != "poison":
            hero.moveXY(item.pos.x, item.pos.y)
        pass

第27关:逻辑之路
子网页:https://cn.codecombat.com/play/level/logical-path?

# 从巫师那得到两个秘密的真假值
hero.moveXY(14, 24)
secretA = hero.findNearestFriend().getSecretA()
secretB = hero.findNearestFriend().getSecretB()

# 如果 secretA 和 secretB 都为真,走上面的路;否则,走下面。
# 查看提示,学会写逻辑表达式。
secretC = secretA and secretB
if secretC:
    hero.moveXY(20, 33)
else:
    hero.moveXY(20, 15)
hero.moveXY(26, 24)

# 如果 secretA 和 secretB 中有一个为真,走上面。
secretD = secretA or secretB
if secretD:
    hero.moveXY(32, 33)
else:
    hero.moveXY(32, 15)
hero.moveXY(38, 24)

# If secretB is NOT true, take the high path.
secretE = not secretB
if secretE:
    hero.moveXY(44, 33)
else:
    hero.moveXY(44, 15)
hero.moveXY(50, 24)

第28关:回到多刺疏林农场
子网页:https://cn.codecombat.com/play/level/return-to-thornbush-farm?

# 这个函数 “maybeBuildTrap” 定义了两个参数
def maybeBuildTrap(x, y):
    # 使用x和y作为座标来移动过去
    hero.moveXY(x, y)
    enemy = hero.findNearestEnemy()
    if enemy:
        pass
        # 使用 buildXY 在所给 x 和 y 坐标建造 "fire-trap".
        hero.buildXY("fire-trap", x, y)
        
while True:
    # 这叫做 maybeBuildTrap,带有上方入口的坐标。
    maybeBuildTrap(43, 50)
    # 现在在左边入口使用maybeBuildTrap
    maybeBuildTrap(25, 34)
    # 现在用 “maybeBuildTrap” 炸开底部的门口!
    maybeBuildTrap(43, 20)

第29关:收集金币
子网页:https://cn.codecombat.com/play/level/coinucopia?

# 当你放好旗帜后点提交.
# 点击提交后,旗帜按钮出现在左下角. 

while True:
    flag = hero.findFlag()
    if flag:
        hero.pickUpFlag(flag)
    else:
        hero.say("为英雄放置一面旗帜来移动.")

第30关:金币草地
子网页:https://cn.codecombat.com/play/level/copper-meadows?

# 收集每片草地的所有金币。
# 使用旗子在草地间移动。
# 当你准备好放置旗子时点击“提交”

while True:
    flag = hero.findFlag()
    if flag:
        pass  # “pass”是一个占位符,它没有任何作用
        # Pick up the flag.
        hero.pickUpFlag(flag)
    else:
        # Automatically move to the nearest item you see.
        item = hero.findNearestItem()
        if item:
            position = item.pos
            x = position.x
            y = position.y
            hero.moveXY(x, y)

第31关:插旗子
子网页:https://cn.codecombat.com/play/level/drop-the-flag?

# 在你想要建造陷阱的位置插旗
# 当你没有在建造陷阱的时候,收集金币!

while True:
    flag = hero.findFlag()
    if flag:
        # 我们该如何通过旗子的位置得到 flagX 和 flagY 呢?
        # (向下看如何得到物品的 x 和 y)
        flagPos = flag.pos
        flagX = flagPos.x
        flagY = flagPos.y
        hero.buildXY("fire-trap", flagX, flagY)
        hero.pickUpFlag(flag)
    else:
        item = hero.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            hero.moveXY(itemX, itemY)

第32关:小心陷阱
子网页:https://cn.codecombat.com/play/level/mind-the-trap?

# 如果你试图攻击一个远处的敌人,你的英雄会忽略掉所有的旗子而朝它冲过去。
# 你需要确保你只攻击靠近自己的敌人!

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
if flag: # 去拔旗子。 hero.pickUpFlag(flag) hero.say("我应该去把旗子拔起来。") elif enemy: # 仅当敌人的距离小于10米时才攻击。 distance = hero.distanceTo(enemy) if distance < 10: hero.attack(enemy)

第33关:通信尸体
子网页:https://cn.codecombat.com/play/level/signal-corpse?

# 你可以使用旗子来选择不同的策略
# 在这关,绿色旗子代表你要移动到旗子处。
# 遇到黑旗就意味着你要劈开旗子
# The doctor will heal you at the Red X

while True:
    green = hero.findFlag("green")
    black = hero.findFlag("black")
    nearest = hero.findNearestEnemy()    
    if green:
        hero.pickUpFlag(green)
    elif black and hero.isReady("cleave"):
        hero.pickUpFlag(black)
        # 劈斩!
        hero.cleave(black)
    elif nearest and hero.distanceTo(nearest) < 10:
        # 攻击!
        hero.attack(nearest)
        pass

第34关:丰富的觅食
子网页:https://cn.codecombat.com/play/level/rich-forager?

# 使用 if 和 else if 来处理任何情况
# 放置它来防御敌人,收集金币
# 确保你从物品商店买到伟大的盔甲,建议400点以上的健康。

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    if flag:
        # 当我发现旗子的时候发生了什么?
        hero.pickUpFlag(flag)
    elif enemy:
        # 当我找到敌人的时候发生了什么?
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
    elif item:
        # 当我找到一个物品的时候,发生了什么?
        hero.moveXY(item.pos.x, item.pos.y)

第35关:围攻Stonehold
子网页:https://cn.codecombat.com/play/level/siege-of-stonehold?

# 帮助你的朋友击败索科塔派出的手下。
# 你需要更好的装备和策略去赢得战斗。
# 标记可能有用,不过这取决于你——要有创造性哦!
# 在围栏后有位医生。移动到 X 处得到治疗!

while True:
    enemy = hero.findNearestEnemy()
    flag = hero.findFlag()
    ready = hero.isReady("cleave")
    if flag:
        hero.moveXY(flag.pos.x, flag.pos.y)
        hero.pickUpFlag(flag)
    elif enemy:
        distance = hero.distanceTo(enemy)
        if distance < 10 and ready:
            hero.cleave(enemy)
        else:
            hero.attack(enemy)

【竞技AI】第36关:竞技场
子网页:https://cn.codecombat.com/play/level/dueling-grounds?

第一次:

# 在决斗中击败敌人的英雄!

while True:
    # 在一个循环中找到并攻击敌人
    # 当你完成的时候,提交到多人天梯系统中!
    enemy = hero.findNearestEnemy()
    if hero.isReady("cleave"):
        hero.cleave(enemy)
    else:
        hero.attack(enemy)

第二次:

# 在决斗中击败敌人的英雄!

while True:
    # 在一个循环中找到并攻击敌人
    # 当你完成的时候,提交到多人天梯系统中!
    enemy = hero.findNearestEnemy()
    if hero.isReady("cleave"):
        hero.cleave(enemy)
    elif hero.isReady("bash"):
        hero.bash(enemy)
    else:
        hero.attack(enemy)

【挑战升级】第37关:野外逃亡
子网页:https://cn.codecombat.com/play/level/backwoods-brawl?

【未通关】

第38关:野马
子网页:https://cn.codecombat.com/play/level/wild-horses?

while True:
    # 你怎么寻找最近的友好单位?
    # 马=?
    horse = hero.findNearest(hero.findFriends())
    
    if horse:
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        if x1 >= 1:
            # 移动到马的y坐标,但使用x1作为x坐标。
            hero.moveXY(x1, horse.pos.y)
        elif x2 <= 79:
            # 移动到马的y坐标,但使用x2作为x坐标。
            hero.moveXY(x2, horse.pos.y)
        distance = hero.distanceTo(horse)
        if distance <= 10:
            hero.say("Whoa")
            # 移到到红色的x来使马返回农场。
            hero.moveXY(27, 54)
            # 移回牧场开始寻找下一匹马。

【挑战升级】第39关:边远宝藏
子网页:https://cn.codecombat.com/play/level/backwoods-treasure?

# 从2~3个树丛里 收集100个金币
# 如果你赢了,接下来会变得更难,当然也会有更多奖励。
# 如果你输了,需要等待一天再次挑战
# 记得,每一次提交都会获得不同的地图。

while True:
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    flag = hero.findFlag("green")
    
    if flag:
        hero.pickUpFlag(flag)
    if enemy:  
        distance = hero.distanceTo(enemy)
        if distance < 8:
            if hero.isReady("cleave"):
                hero.cleave(enemy)
            elif hero.isReady("bash"):
                hero.bash(enemy)
            else:
                hero.attack(enemy)
    if item:
        hero.moveXY(item.pos.x, item.pos.y)

【竞技AI】第40关:多人宝藏
子网页:https://cn.codecombat.com/play/level/multiplayer-treasure-grove?

# 当第一个收集100个金币的人!
# 如果你死了,重生的时候只有原来67%的金币

while True:
    # 找到金币并攻击敌人
    # 使用旗子和特殊的移动策略来赢得比赛!
    flag = hero.findFlag()
    item = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    if flag:
        hero.pickUpFlag(flag)
    elif item:
        hero.moveXY(item.pos.x, item.pos.y)
    elif enemy:
        distance = hero.distanceTo(enemy)
        if distance < 5:
            hero.attack(enemy)

初学Python。

请多指教。

-----转载请注明出处,否则作者有权追究法律责任。

原文地址:https://www.cnblogs.com/learningpython-xinersubai/p/7661466.html