[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。

請多指教。

 

-----轉載請注明出處,否則作者有權追究法律責任。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM