[Python] Codecombat 攻略 Sarven 沙漠 (1-43关)截止至36关

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

第二界面:Sarven沙漠(43关)
时间:4-11小时
内容:算术运算,计数器,while循环,break(跳出循环),数组,字符串比较,寻找最小最大值。
网页:https://cn.codecombat.com/play/desert

闯关:
第1关:强壮的沙牦牛
子网页:https://cn.codecombat.com/play/level/the-mighty-sand-yak?

# 当牦牛靠近时向右移动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 = x + 10
        # 使用 moveXY 来移动!
        hero.moveXY(x, y)
        pass

第2关:绿洲
子网页:https://cn.codecombat.com/play/level/oasis?

# 向绿洲移动
# Move left to avoid nearby yaks.

while True:
    x = hero.pos.x
    y = hero.pos.y
    enemy = hero.findNearestEnemy()
    if enemy and hero.distanceTo(enemy) < 10:
        # 通过在你的X坐标上减去10来移动到左边
        x = x - 10
        # Use moveXY to move to the new x, y position.
        hero.moveXY(x, y)
        pass
    else:
        # 通过在你的X坐标上加上10来移动到右边
        x = x + 10
        # Use moveXY to move to the new x, y position.
        hero.moveXY(x, y)
        pass

第3关:盆地的践踏
子网页:https://cn.codecombat.com/play/level/basin-stampede?

# Keep moving right, but adjust up and down as you go.

while True:
    enemy = hero.findNearestEnemy()
    xPos = hero.pos.x + 5
    yPos = 17
    if enemy:
        # Adjust y up or down to get away from yaks.
        if enemy.pos.y > hero.pos.y:
            # If the Yak is above you, subtract 3 from yPos.
            yPos = yPos - 3
            pass
        elif enemy.pos.y < hero.pos.y:
            # If the Yak is below you, add 3 to yPos.
            yPos = yPos + 3
            pass
    hero.moveXY(xPos, yPos)

第4关:萨文路
子网页:https://cn.codecombat.com/play/level/sarven-road?

第一次:

# 到达绿洲。小心新的敌人:食人魔侦察兵!
# 通过添加你当前的X位置和Y位置以向上向右走

while True:
    # If there's an enemy, attack.
    enemy = hero.findNearestEnemy()
    xPos = hero.pos.x
    yPos = hero.pos.y
    if enemy:
        hero.attack(enemy)
    # Else, keep moving up and to the right. 
    else:
        xPos = xPos + 5
        yPos = yPos + 5
        hero.moveXY(xPos, yPos)
    pass

第5关:十字路口
子网页:https://cn.codecombat.com/play/level/crossroads?

# 使用  fire-trap 打败进攻的食人魔

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If the enemy is to the left of the hero:
        if enemy.pos.x < hero.pos.x:
            # 如果敌人从左边进攻,在左边建一个fire-trap(火焰陷阱)
            hero.buildXY("fire-trap", 25, 34)
            pass
        # If the enemy is to the right of the hero:
        elif enemy.pos.x > hero.pos.x:
            # 如果敌人从右边进攻,在右边建一个fire-trap(火焰陷阱)
            hero.buildXY("fire-trap", 55, 34)
            pass
        # If the enemy is below the hero:
        elif enemy.pos.y < hero.pos.y:
            # 如果敌人从下边进攻,在下边建一个fire-trap(火焰陷阱)
            hero.buildXY("fire-trap", 40, 19)
            pass
        # If the enemy is above the hero:
        elif enemy.pos.y > hero.pos.y:
            # 如果敌人从上边进攻,在上边建一个fire-trap(火焰陷阱)
            hero.buildXY("fire-trap", 40, 49)
            pass
    # Move back to the center.
    hero.moveXY(40, 34)

第6关:雷蹄
子网页:https://cn.codecombat.com/play/level/thunderhooves?

# 到达绿洲,
# 用栅栏引导砂牦牛到你去的地方

while True:
    yak = hero.findNearestEnemy()
    if yak:
        # 如果它的 y 值大于你的,那么耗牛在你前面
        if yak.pos.y > hero.pos.y:
            # 如果耗牛在你前面,在它后面10米建立一个栅栏
            hero.buildXY("fence", yak.pos.x, yak.pos.y - 10)
        else: 
            if yak.pos.y < hero.pos.y:
            # 如果耗牛在你后面,在它前面10m 建立一个栅栏
                hero.buildXY("fence", yak.pos.x, yak.pos.y + 10)
        pass
    else:
        # 向右移动10走向绿洲
        hero.moveXY(hero.pos.x + 10, hero.pos.y)
        pass

第7关:截擎
子网页:https://cn.codecombat.com/play/level/interception?

# Stand between the peasant and the tower.

while True:
    enemy = hero.findNearestEnemy()
    friend = hero.findNearestFriend()
    # Calculate x by adding friend.pos.x to enemy.pos.x
    # 然后除以2
    # Check the guide if you need more help!
    x = (friend.pos.x + enemy.pos.x) / 2
    # Now do the same for y
    y = (friend.pos.y + enemy.pos.y) / 2
    # 移动到您计算的X和Y坐标。
    hero.moveXY(x, y)

第8关:猎杀行动
子网页:https://cn.codecombat.com/play/level/operation-killdeer?

# 引诱这些食人魔进入陷阱,这些食人魔非常小心
# 只有英雄受伤了他们才会跟着英雄

# 这个函数会检查英雄的生命值
# 并返回一个布尔型(Boolean)的值。

def shouldRun():
    if hero.health < hero.maxHealth / 2:
        return True
    else:
        return False

while True:
    enemy = hero.findNearestEnemy()
    # 当shouldRun()返回True时 移动到X点 True
    if shouldRun():
        hero.moveXY(75, 37)
    # 否则,攻击!
    else:
        hero.attack(enemy)

第9关:医疗注意
子网页:https://cn.codecombat.com/play/level/medical-attention?

# 当你生命值少于一半时,请求医疗人员的治疗。

while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    enemy = hero.findNearestEnemy()
    
    # 如果你当前的健康值少于下限,
    # 移动到治疗点说『heal me』
    # 否则的话,攻击。你需要战斗的更狠点!
    if currentHealth < healingThreshold:
        hero.moveXY(65, 46) 
        hero.say("heal me")
    elif enemy:
        hero.attack(enemy)

第10关:跟上时间
子网页:https://cn.codecombat.com/play/level/keeping-time?

# 使用你的新技能来选择你要做什么 hero.now()

while True:
    # 如果是头十秒,进攻。
    if hero.now() < 10:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        pass
    # 反之,如果是前35秒,收集金币。
    elif hero.now() < 35:
        coin = hero.findNearestItem()
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
        pass
    # 后35秒,加入救助。
    else:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        pass

第11关:Sarven哨兵
子网页:https://cn.codecombat.com/play/level/sarven-sentry?

# 使用不同的颜色旗子来执行不同的任务。

while True:
    flagGreen = hero.findFlag("green")
    flagBlack = hero.findFlag("black")
    # 如果是绿色旗子,就建立一个栅栏。

    if flagGreen:
        # Build a "fence" at flagGreen's position.
        hero.buildXY("fence", flagGreen.pos.x, flagGreen.pos.y)
        # 记住要捡起旗子,在你都完成之后!
        hero.pickUpFlag(flagGreen)

    # 如果是黑色旗子,就建立一个火焰陷阱
    if flagBlack:
        # Build a "fire-trap" at flagBlack's position.
        hero.buildXY("fire-trap", flagBlack.pos.x, flagBlack.pos.y)
        # 记住要捡起旗子,在你都完成之后!
        hero.pickUpFlag(flagBlack)

    # 回到中间。
    hero.moveXY(43, 31)

第12关:囤积黄金
子网页:https://cn.codecombat.com/play/level/hoarding-gold?

# 收集25金币,然后告诉 Naria 总数
# 当金币总数大于25,使用 break 来停止收集金币。

totalGold = 0
while True:
    coin = hero.findNearestItem()
    if coin:
        # 捡起金币
        hero.moveXY(coin.pos.x, coin.pos.y)
        # 将金币的价值加进 totalGold.(查看帮助了解更多.)
        # 使用以下方法得到它的价值::  coin.value
        totalGold = totalGold + coin.value
        pass
    if totalGold >= 25:
        # 这会中断循环并且执行循环下面的语句
        # The loop ends, code after the loop will run.
        break
# 完成收集金币! hero.moveXY(58, 33) # 去告诉 Naria 你收集了多少金币。 hero.say(totalGold)

第13关:诱饵钻
子网页:https://cn.codecombat.com/play/level/decoy-drill?

# 我们在测试一个新的战斗单位:诱饵。
# 创建4个诱饵,然后汇报给 Naria

decoysBuilt = 0
while True:
    coin = hero.findNearestItem()
    if coin:
        # 掠夺金币!
        hero.moveXY(coin.pos.x, coin.pos.y)
        pass
    # 每个诱饵消费25个金币。
    # 让它知道当你有超过25个金币的时候
    if hero.gold >= 25:
        # buildXY a "decoy"
        hero.buildXY("decoy", hero.pos.x, hero.pos.y)
        # 当你一直走的时候,保持统计你创建的诱饵的数量。
        decoysBuilt += 1
    if decoysBuilt == 4:
        # 当你创建了4个诱饵时跳出循环
        break
        pass
    
hero.say("完成创建诱饵!")
hero.moveXY(14, 36)
# 去找 Naria 并告诉她你创建了多少个诱饵。
hero.say("I duilded "+decoysBuilt+" decoys !")

第14关:守书人
子网页:https://cn.codecombat.com/play/level/bookkeeper?

# 奋战沙场15秒。
# Keep count whenever an enemy is defeated.

defeated = 0
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        if enemy.health <= 0:
            defeated = defeated + 1
    if hero.now() > 15:
        break
# Tell Naria how many enemies you defeated.
hero.moveXY(59, 33)
hero.say("I defeated "+defeated+" enemies !")

# 收集金币,直到时间达到30秒
while True:
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    if hero.now() > 30:
        break
# Tell Naria how much gold you collected.
hero.moveXY(59, 33)
hero.say("I got "+hero.gold+" golds !")

# 攻击敌人,直到时间达到45秒
# 记得重置击败的敌人数。
defeated = 0
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        if enemy.health <= 0:
            defeated = defeated +1
    if hero.now() > 45:
        break
# Tell Naria how many enemies you defeated.
hero.moveXY(59, 33)
hero.say("I defeated "+defeated+" enemies !")

第15关:连续的炼金术
子网页:https://cn.codecombat.com/play/level/continuous-alchemy?

# Race munchkins to the water distilled by Omarn Brewstone!
# 使用 "continue" 验证丛林中的条件。

while True:
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    
    # 如果没有敌人,跳出循环继续。
    if not enemy:
        continue
    
    # 如果有敌人却没物品,要一瓶药,跳到下次循环。
    if not item:
        hero.say("把喝的拿来!")
        continue
    
    # 使用 if 语句检查物品的类型。如果类型是 "poison",跳出循环继续运行。
    if item.type =="poison":
        continue
# 如果不是,那瓶子里装的是水,所以走向它,返回出发点! # so moveXY to the potion, then back to the start! if item.type !="poison": hero.moveXY(44, 35) hero.moveXY(34, 47)

第16关:复查
子网页:https://cn.codecombat.com/play/level/double-cheek?

# 第一点,打败6位ogres~
# Then collect coins until you have 30 gold.

# 变量用来对ogres计数
defeatedOgres = 0

# 没打败6位ogres,就继续打
while defeatedOgres < 6:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        defeatedOgres += 1
    else:
        hero.say("Ogres!")

# Move to the right side of the map.
hero.moveXY(49, 36)

# 钱没攒够30块,就继续捡
while hero.gold < 30:
    # 寻找并收集金币
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    # 去掉这行 say()。
    #hero.say("我应该收集金币!")

# 移动到出口。
hero.moveXY(76, 32)

第17关:沙漠战役
子网页:https://cn.codecombat.com/play/level/desert-combat?

# while循环重复直到条件为否。

ordersGiven = 0
y = 50
while ordersGiven < 5:
    # 在站场上移动和排列你的盟友。 (如果你是直接在他们面前,他们只能听到你的。)
    hero.moveXY(10, y)
    # 用hero.say的方法说“Attack”来命令你的盟友
    # 只有当你的英雄在 “X” 位置的时候 他们才能听到你的说什么
    hero.say("Attack!")
    y = y - 10
    # 确保ordersGiven增加
    ordersGiven += 1
    
while True:
    enemy = hero.findNearestEnemy()
    # 当你下达完命令,立即加入战斗!
    if enemy:
        hero.attack(enemy)

第18关:尘埃
子网页:https://cn.codecombat.com/play/level/dust?

# 使用循环直到你有足够的击杀10个芒奇金人 

attacks = 0
while attacks < 10:
    # 攻击最近的敌人!
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    # Incrementing means to increase by 1.
    # 增加你的攻击统计量。
    attacks += 1

# 当你完成后,撤退到伏击点。
hero.say("I should retreat!") #∆ Don't just stand there blabbering!
hero.moveXY(79, 33)

第19关:别冲过去,安静点
子网页:https://cn.codecombat.com/play/level/dont-rush-be-quiet?

# Dodge the cannons and collect 8 gems.
# Watch out, cannons are ready to fire!
# 以一个特殊的方式缓慢移动去迷惑敌人

# This function returns a value from 0 to 30:
def mod30(n):
    if n >= 30:
        return n - 30
    else:
        return n

# 这一功能将会返回一个从0到40的值
def mod40(n):
    # 使用一个 “if” 语句去返回正确的值
    if n >= 40:
        return n - 40
    else:
        return n

# You don't need to change the following code:
while True:
    time = hero.now()
    x = mod30(time) + 25
    y = mod40(time) + 10
    hero.moveXY(x, y)

第20关:Zig Zag and Zoom
子网页:https://cn.codecombat.com/play/level/zig-zag-and-zoom?

# 从死亡峡谷逃出!
# 使用真正的求余函数走出Z字形路线。

# This function returns a value from 0 to 15:
def mod15(n):
    while n >= 15:
        n -= 15
    return n

# 这个函数应该会反馈一个从0到9的值
def mod9(n):
    # 在返回前使用 while 循环修改参数。
    while n>= 9:
        n -= 9
    return n

# Don't change the following code:
while True:
    time = self.now()
    if time < 30:
        y = 10 + 3 * mod15(time)
    else:
        y = 20 + 3 * mod9(time)
    x = 10 + time
    self.moveXY(x, y)

第21关:沙漠三角洲
子网页:https://cn.codecombat.com/play/level/desert-delta?

# 只攻击在敌军名称(enemyNames)数组中的敌人
# Be sure to attack in order! 0 -> 1 -> 2 -> 3
enemyNames = ["Kog", "Godel", "Vorobun", "Rexxar"]

hero.attack(enemyNames[0])
hero.attack(enemyNames[1])
# 攻击 enemyNames[2]
hero.attack(enemyNames[2])
# 攻击最后一个元素。
hero.attack(enemyNames[3])

第22关:立方雷区
子网页:https://cn.codecombat.com/play/level/cubic-minefield?

# 穿过雷区

# 这个函数返回乘以次数的数字。
def mult(number, times):
    total = 0
    while times > 0:
        total += number
        times -= 1
    return total

# 这个函数返回乘方的数字。
def power(number, exponent):
    total = 1
    # 补全函数。
    while exponent > 0:
        total *= number
        exponent -= 1
    return total

# 别修改这些代码
# You can find coefficients for the equation on the tower
tower = hero.findFriends()[0]
a = tower.a
b = tower.b
c = tower.c
d = tower.d
x = hero.pos.x

while True:
    # To find the path use a cubic equation
    y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0)
    hero.moveXY(x, y)
    x = x + 5

第23关:Sarven救世主
子网页:https://cn.codecombat.com/play/level/sarven-savior?

# 一个数组(Array)就是物品的数列。
# 这个数组是一个朋友名字的数列。

friendNames = ['Joan', 'Ronan', 'Nikita', 'Augustus']

# 数组从零开始计数,不是1!
friendIndex = 0

# 循环该数组中的每一个名字
# 使用 len()方法来得到列表的长度。
while friendIndex < len(friendNames):
    # 使用方括号来获得数组中的名字。
    friendName = friendNames[friendIndex]
    # 告诉你的朋友回家。
    # 使用+来连接两个字符串。
    hero.say(friendName + ', go home!')
    # 增加索引来获取数组中的下一个名字
    friendIndex += 1

# 回去建造栅栏让食人魔远离。
hero.moveXY(29, 30)
hero.buildXY("fence", 30, 30)

第24关:Bank Raid
子网页:https://cn.codecombat.com/play/level/bank-raid?

# 等待食人魔出现,然后打败它们,并收集金币。

while True:
    enemies = hero.findEnemies()
    
    # enemyIndex 用于迭代数组。
    enemyIndex = 0
    
    # While enemyIndex is less than len(enemies)
    while enemyIndex < len(enemies):
        # Attack the enemy at enemyIndex
        enemy = enemies[enemyIndex]
        hero.attack(enemy)
        # 给 enemyIndex 加上 1。
        enemyIndex += 1
        
    coins = hero.findItems()
    # coinIndex is used to iterate the coins array.
    coinIndex = 0
    
    while coinIndex < len(coins):
        # 用 coinIndex 从 coins 数组得到一个金币。
        coin = coins[coinIndex]
        # 收集那个金币。
        hero.moveXY(coin.pos.x, coin.pos.y)
        # 给 coinIndex 的值增加 1。
        coinIndex += 1

第25关:游魂
子网页:https://cn.codecombat.com/play/level/wandering-souls?

# 攻击骷髅捡走宝石

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # Attack while enemy has health.
        while enemy.health > 0:
            hero.attack(enemy)
        enemyIndex += 1
        
    items = hero.findItems()
    itemIndex = 0
    # 遍历所有的物品。
    while itemIndex < len(items):
        item = items[itemIndex]
        # While the distance greater than 2:        
        while hero.distanceTo(item) > 2:
            # 试着拿到物品
            hero.moveXY(item.pos.x, item.pos.y)
        # 别忘了让itemIndex变大 (itemIndex+=1)或(itemIndex=itemIndex+1)
        itemIndex += 1

第26关:团队合作
子网页:https://cn.codecombat.com/play/level/team-work?

# Gems will disappear soon. You'll need help!

# findItems() returns an array of items.
items = hero.findItems()

# Get the first gem from the array.
# Don't forget that the first index is 0.
gem0 = items[0]

# # 告诉 Bruno 拿到 gem0
hero.say("Bruno " + gem0)

# You can reference the gem without a variable.
hero.say("Matilda " + items[1])

# Create a variable for the last gem, items[2]:
gem2 =items[2]
# Move to that gem's position using moveXY()
hero.moveXY(gem2.pos.x, gem2.pos.y)

第27关:潜伏
子网页:https://cn.codecombat.com/play/level/lurkers?

# 用findEnemies把敌人存在数组enemies中
# 只攻击萨满巫师,不要攻击牦牛!

enemies = hero.findEnemies()
enemyIndex = 0

# 把这段代码用一个while loop 功能循环遍历所有的敌人
# 当 enemyIndex 小于 enemies 的长度时:
while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    if enemy.type == 'shaman':
        while enemy.health > 0:
            hero.attack(enemy)
# Remember to increment enemyIndex
    enemyIndex += 1

第28关:捡闪亮东西的人
子网页:https://cn.codecombat.com/play/level/shine-getter?

# 很快的获取最多的金币

while True:
    coins = hero.findItems()
    coinIndex = 0
    
    # 把这个封装进循环里枚举所有的硬币
    while coinIndex < len(coins):
        coin = coins[coinIndex]
    # 金币价值3点。
        if coin.value == 3:
        # 只捡金币。
            hero.moveXY(coin.pos.x, coin.pos.y)
        coinIndex += 1
        pass

第29关:Sarven牧羊人
子网页:https://cn.codecombat.com/play/level/sarven-shepherd?

# 使用 while 循环来对付食人魔。

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    # 将攻击逻辑放到 while 循环里来攻击所有的敌人。
    # Find the array's length with:  len(enemies)
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
    # "!=" 意思是 "不等于"
        if enemy.type != "sand-yak":
        # 当敌人的健康值大于0,攻击它!
            while enemy.health > 0:
                hero.attack(enemy)
        enemyIndex += 1
        pass
    # 在两波敌人之间,移动回中央。
    hero.moveXY(40, 30)

第30关:掠夺者
子网页:https://cn.codecombat.com/play/level/marauder?

# 打几下泡泡人捡走掉出的币

while True:
    coin = hero.findNearestItem()
    # 当存在金币时:
    if coin:
        # 移动到金币处。
        hero.moveXY(coin.pos.x, coin.pos.y)
        # ‘coin’应该是最近的那枚 捡到手以后要另找一枚最近的
        coin += 1
        
    enemy = hero.findNearestEnemy()
    if enemy:
        # 如果敌人还会动
        while enemy.health > 0:
            # 就打它
            hero.attack(enemy)
        pass

第31关:疯狂的Maxer
子网页:https://cn.codecombat.com/play/level/mad-maxer?

# 优先杀掉最远的敌人。

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = hero.findEnemies()

    # 查看全部敌人,找出最远的那个。
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1

        # 是不是存在远得看不到的敌人?
        distance = hero.distanceTo(target)
        if distance > maxDistance:
            maxDistance = distance
            farthest = target

    if farthest:
        # 干掉最远的敌人!
        # 如果敌人血量大于0就保持攻击。
        while farthest.health > 0:
            if hero.isReady("cleave"):
                hero.cleave(farthest)
            elif hero.isReady("bash"):
                hero.bash(farthest)
            else:
                hero.attack(farthest)
        pass

 

第32关:沙蛇
子网页:https://cn.codecombat.com/play/level/sand-snakes?

# 这片区域布满了火焰陷阱。幸好我们之前派出了侦察员,他沿路在地上留下了宝石作为暗号,我们只需要顺着最近的宝石走就能躲过这些陷阱。

# 沙漠峡谷似乎会干扰你使用眼镜的findNearest技能!
# 你需要自己找到离你最近的宝石。

while True:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # 搜索所有的宝石,找到离你最近的那一颗。
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        # 如果宝石与你的距离小于“最近距离(nearestDistance)”
        if distance < nearestDistance:
            # 设置该宝石为离你最近的宝石
            nearest = coin
            # 设置该距离为“最近距离(nearestDistance)”
            nearestDistance = distance 
            
    # 如果找到离你最近的宝石,移动英雄岛宝石的位置。你需要使用moveXY,不需要你抄近路,也不会踩到陷阱。
    if nearest:
        hero.moveXY(nearest.pos.x, nearest.pos.y)

第33关:Brittle Moral
子网页:https://cn.codecombat.com/play/level/brittle-morale?

# You have one arrow. Make it count!

# 这将返回一个最多生命值的敌人
def findStrongestEnemy(enemies):
    strongest = None
    strongestHealth = 0
    enemyIndex = 0
    # 当 enemyIndex 少于敌人的长度
    while enemyIndex < len(enemies):
        # Set an enemy variable to enemies[enemyIndex]
        enemy = enemies[enemyIndex]
        # 如果 enemy.health 大于 strongestHealth
        if enemy.health > strongestHealth:
            # 将 strongest 赋值为 enemy
            strongest = enemy
            # Set strongestHealth to enemy.health
            strongestHealth = enemy.health 
        # 让 enemyIndex 递增
        enemyIndex += 1
    return strongest

enemies = hero.findEnemies()
leader = findStrongestEnemy(enemies)
if leader:
    hero.say(leader)

第34关:一打宝石
子网页:https://cn.codecombat.com/play/level/diamond-dozen?

【装备更换】剑:攻击值越高越好,我是228.57(192.2DPS)    鞋子:速度+2.5

# 打败前来劫掠的食人魔,让他们把金币交出来!

def findMostHealth(enemies):
    target = None
    targetHealth = 0
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > targetHealth:
            target = enemy
            targetHealth = enemy.health
        enemyIndex += 1
    return target

def valueOverDistance(item):
    return item.value / hero.distanceTo(item)

# 返回有最高 valueOverDistance(item) 的物品。
def findBestItem(items):
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    
    # 循环于 items 数组内。
    # 发现这个物品的最高 valueOverDistance()
    while itemsIndex < len(items):
        item = items[itemsIndex]
        if valueOverDistance(item) > bestValue:
            bestItem = item
            bestValue = valueOverDistance(item)
        itemsIndex += 1
    return bestItem
    

while True:
    enemies = hero.findEnemies()
    enemy = findMostHealth(enemies)
    if enemy and enemy.health > 15:
        while enemy.health > 0:
            hero.attack(enemy)
    else:
        coins = hero.findItems()
        coin = None
        coin = findBestItem(coins)
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)

  

第35关:Wishing Well
子网页:https://cn.codecombat.com/play/level/wishing-well?

# 你需要104的金钱,不多也不少。

less = "Nimis"
more = "Non satis"
requiredGold = 104

# 此函数计算所有的硬币值的总和。
def sumCoinValues(coins):
    coinIndex = 0
    totalValue = 0
    # 遍历所有的金币。
    while coinIndex < len(coins):
        totalValue += coins[coinIndex].value
        coinIndex += 1
    return totalValue

def collectAllCoins():
    item = hero.findNearest(hero.findItems())
    while item:
        hero.moveXY(item.pos.x, item.pos.y)
        item = hero.findNearest(hero.findItems())

while True:
    items = hero.findItems()
    # 获得硬币的总值
    goldAmount = sumCoinValues(items)
    # 如果有金币,那么金币数目 (goldAmount) 不会是零
    if goldAmount != 0:
        # If goldAmount is less than requiredGold
        # 那就说“Non satis”
        if goldAmount < requiredGold:
            hero.say("Non satis")
        # If goldAmount is greater than requiredGold
        # 那么说出“Nimis”。
        if goldAmount > requiredGold:
            hero.say("Nimis")
        # 如果 “goldAmount” 等于 “requiredGold”
        # 如果有刚好 104 金币,就全部收集。
        if goldAmount == requiredGold:
            collectAllCoins()
        pass

【挑战升级】第36关:Sarven 斗殴
子网页:https://cn.codecombat.com/play/level/sarven-brawl?

第一次:【我设定英雄回到的位置,比较不容易被射箭的敌人从远距离射伤】

# 活两分钟。
# 如果你赢了,接下来会变得更难,当然也会有更多奖励。
# 如果你输了,你必须等一天之后再提交。
# 记得,每一次提交都会获得不同的地图。

while True:
    enemy = hero.findNearestEnemy()
    
    if enemy:
        if hero.isReady("bash"):
            hero.bash(enemy)
        else:
            hero.attack(enemy)
    else:
        hero.moveXY(50, 70)

 

第37关:
子网页:

第38关:
子网页:

第39关:
子网页:

第40关:
子网页:

第41关:
子网页:

第42关:
子网页: 

第43关:审判
子网页:https://cn.codecombat.com/play/level/the-trials?

未通关

初学Python。

请多指教。

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

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