CodeCombat森林關卡Python代碼


地牢關卡過完,接下來是邊緣的森林!

 

1,森林保衛戰

hero.moveUp()
hero.buildXY("fence", 40, 52)
hero.moveDown()
hero.moveDown()
hero.buildXY("fence", 40, 20)

 

 

2,羊腸小道

# It's the first point of the path.
hero.moveXY(36, 59)
# Move at the next points of the path.
hero.moveXY(37, 13)
# Build a fence to stop the ogre.
hero.moveXY(79,18)
hero.moveXY(73,61)
hero.moveXY(79,18)

 

 

3,叢林里的隔間

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

while True:
    enemy = hero.findNearestEnemy()
    # 使用一個 “if” 語句去檢查是否有敵人存在:
    if enemy:
        # 攻擊敵人如果它存在的話
        hero.attack(enemy)
        hero.attack(enemy)

 

 

5,背靠背

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # 亦或主動出擊...
         hero.attack(enemy)
         hero.attack(enemy)
    else:
        # 亦或回到你的陣地防守。
        hero.say(" 我看不到敵人")
        hero.moveXY(40, 34)
        pass

 

 

6,森林劈裂者

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,邊遠地區的對峙

while True:
    # 使用 ‘isReady’ 中的一個 “if-statement” 的語句來檢查 “cleave”
    if hero.isReady("cleave"):
        # 劈斬!
        enemy = hero.findNearestEnemy()
        hero.cleave(enemy)
    # 或者,如果 cleave 還沒准備好的話:
    else:
        # 說一點什么來嚇走曼切堪食人魔
        hero.say(" 快滾吧!刀劍無眼 !  ")
    pass

 

 

 8,測距儀

 

enemy1 = "Gort"
distance1 = hero.distanceTo(enemy1)
hero.say(distance1)
enemy2 = "Smasher"
distance2 = hero.distanceTo(enemy2)
# 將distance2變數作為參數,傳入say()方法
hero.say(distance2)
# 測量並說出剩余敵人與英雄間的距離
# 不要向你的友軍進行射擊!
enemy3 = "Charles"
enemy4 = "Gorgnub"
distance4 = hero.distanceTo(enemy4)
hero.say(distance4)

 

9,保護農民

while True:
    enemy = hero.findNearestEnemy()
    distance = hero.distanceTo(enemy)
    if distance < 10:
        # 如果他們與農民太近,就攻擊他們
        hero.attack(enemy)
        pass
    # 否則的話,呆在農民旁邊!
    hero.moveXY(40, 37)

 

 

10,瘋狂的食人魔

# 地上另一個讓英雄打開的寶箱!
# 攻擊寶箱以打開它
# 有些食人魔可不會呆呆地站着挨打!
# 當食人魔離你太近時,你得學着保護你自己
while True:
    enemy = hero.findNearestEnemy()
    distance = hero.distanceTo(enemy)
    if hero.isReady("cleave"):
        # 首先,定期使用旋風斬(cleave)當技能就緒的時候:
        hero.cleave(enemy)
        pass
    elif distance < 5:
        # 攻擊靠近並離你最近的食人魔
        hero.attack(enemy)
        pass
    else:
        # 否則,試着打破寶箱看看:
        hero.attack("Chest")
        pass

 

 

11,躍火林中

# 在這關,別碰惡魔石!往其他方向移動避開它們!
while True:
    evilstone = hero.findNearestItem()
    if evilstone:
        pos = evilstone.pos
        if pos.x == 34:
            # 如果惡魔石在左邊,走到右邊。
            hero.moveXY(46, 22)
            pass
        else:
            # 如果惡魔石在右邊,走到左邊。
            hero.moveXY(34, 22)
            pass
    else:
        # If there's no evilstone, go to the middle.
        hero.moveXY(40, 22)
        pass

 

 12,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,Backwoods Fork

# This function has a parameter.
# 參數是一種給函數傳遞信息的方式。
def checkAndAttack(target):
    # The 'target' parameter is just a variable!
    # 它包含了函數調用時的參數。
    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,Leave it to Cleaver

# This shows how to define a function called cleaveWhenClose
# The function defines a parameter called target
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        hero.cleave(enemy)
        # else, just attack target!
    hero.attack(enemy)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the enemy as target.
        cleaveWhenClose(enemy)

 

 

15,友人和敵人

# 農民和士兵聚集在森林。
# 命令農民戰斗,苦工遠離!

while True:
    friend = hero.findNearestFriend()
    if friend:
        hero.say("To battle, " + friend.id + "!")
    # 尋找最近的敵人,然后讓他們滾蛋
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.say("Scram," + enemy.id + "!")

 

16,巫師之門

# Move to Laszlo and get his secret number.
hero.moveXY(30, 13)
las = hero.findNearestFriend().getSecret()

# 向 Laszlo 的數字中加7就能得到 Erzsebet的號碼
# Move to Erzsebet and say her magic number.
erz = las + 7
hero.moveXY(17, 26)
hero.say(erz)

# 將 Erzsebet 的數字除以 4 得到 Simoyi 的數字。
# Go to Simonyi and tell him his number.
sim =  erz / 4
hero.moveXY(30, 39)
hero.say(sim)

# 將 Simonyi 的數字乘以 Laszlo 的數字得到 Agata 的數字。
# Go to Agata and tell her her number.
Aga = sim * las
hero.moveXY(43, 26)
hero.say(Aga)

 

 

17,金幣屑

# 跟隨硬幣的軌跡來到紅色 X 標記的出口

while True:
    # 這能找到最近的敵人。
    item = hero.findNearestItem()
    if item:
        # 這將物品的 pos,就是坐標,存儲在變量中。
        itemPosition = item.pos
        # 將物品的 X 和 Y 坐標放進變量。
        itemX = itemPosition.x
        itemY = itemPosition.y
        # Now, use moveXY to move to itemX and itemY:
        hero.moveXY(itemX, itemY)

 

 

18,Blind Distance

# 你的任務是告訴他獸人的距離。

# 這個函數尋找最近的敵人,並返回距離。
# If there is no enemy, the function returns 0.
def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 0
    if enemy:
        result = hero.distanceTo(enemy)
    return result

while True:
    # Call nearestEnemyDistance() and
    # save the result in the variable enemyDistance.
    enemyDistance = nearestEnemyDistance()
    # If the enemyDistance is greater than 0: 
    if enemyDistance:
        # Say the value of enemyDistance variable.
        hero.say(enemyDistance)

 

 

 

 

 

 

可選關卡1:競技場

# 在決斗中擊敗敵人的英雄!
while True:
    # 在一個循環中找到並攻擊敵人
    # 當你完成的時候,提交到多人天梯系統中!
    enemy = hero.findNearestEnemy()
    hero.attack(enemy)
    if len(hero.findNearestEnemy()) > 5:
        hero.shield()
    else:
        hero.attack(enemy)

 


免責聲明!

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



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