Python——極客戰記codecombat關卡代碼
標簽: 極客戰記 關卡代碼 codecombat python
- 關卡名稱是對應codecombat(網易163)上面的關卡名。
- 不到萬不得已,請各位不要走這條捷徑噢,畢竟思路比代碼本身更重要。
- 關卡代碼為參考代碼,如有錯誤,請私信作者,附上[網易極客戰記官方群]:288930456,[作者QQ]:397652008,歡迎大家前來探討。
- 本代碼屬于個人學習交流使用,請勿用做商業,如有侵權,請告知本人。
- 成果不易,轉載請注明出處,謝謝。
- kithgard地牢
- 邊地森林
- 沙漠
- 強壯的沙牦牛
- 綠洲
- 盆地的踐踏
- 薩文路
- 十字路口
- 雷蹄
- 操作“殺鹿”
- 醫療注意
- 跟上時間
- 囤積金幣
- 誘餌鉆
- 復查
- 沙漠戰役
- 塵埃
- 別沖過去,安靜點
- ZIG ZAG ANG ZOOM
- 沙漠三角形
- SARVEN 救世主
- BANK RAID
- 潛伏
- SARVEN 牧羊人
- 撿閃亮東西的人
- 瘋狂的MAXER
- 克隆沖突
- 峭壁追逐
- 激流渦旋
- 食人魔山谷挖寶
- 安息之云指揮官
- 傭兵山
- 木材守衛
- 零和
- 動物園的管理員
- 戰略祭品
- 狩獵派對
- 維他力量
- 雙生花
- 花的幾何學
- 山花林
- 獵手和獵物
- 圖書館的謀士
- 我們中的間諜
- 冰獵人
- 模擬侵略
- 吵鬧 安靜
- 方便敵方
- 鋼爪間隙
- 無暇的寶石對
- 馴鹿蘇醒
- 大炮陸戰隊
- 力量的源點
- 周界防御
- 網格雷區
- 聯合作戰(絕對防御)
- 勞心勞力
- 多少錢
- 火山戰士
- 矩形形態
- 人神
- 笨拙的圓
- 牦牛行徑
- 嚴峻的決心
- 尋找開始
- 峰會之門
- 冰川
kithgard地牢
地牢
#向寶石進發。
#小心尖刺!
#在下面輸入你的代碼,完成后點擊運行。
hero.moveRight()
hero.moveDown()
hero.moveRight()
深藏的寶石
# 利用你的移動命令收集所有寶石。
hero.moveRight()
hero.moveDown()
hero.moveUp()
hero.moveUp()
hero.moveRight()
幽靈守衛
# 避開食人魔的視線,收集寶石。
hero.moveRight()
hero.moveUp()
hero.moveRight()
hero.moveDown()
hero.moveRight()
真實姓名
# 抵御 Brak 和 Treg!
# 你必須攻擊小食人魔兩次。
hero.moveRight()
hero.attack("Brak")
hero.attack("Brak")
hero.moveRight()
hero.attack("Treg")
hero.attack("Treg")
高舉之劍
# 打敗食人魔
# 記住,每個攻擊兩次。
hero.attack("Rig")
hero.attack("Rig")
hero.attack("Gurt")
hero.attack("Gurt")
hero.attack("Ack")
hero.attack("Ack")
焰中舞動
# 代碼通常按編寫順序執行。
# 循環會多次重復一個代碼塊。
# 按Tab或4個空格,把移動指令縮進到循環內部。
while True:
hero.moveRight()
# 在這里給循環里加 moveLeft 命令。
hero.moveLeft()
KITHMAZE二度歷險
# 使用while-true循環穿越迷宮!
while True:
hero.moveRight()
# 再鍵入3條移動命令來完成這個迷宮:
hero.moveUp()
hero.moveRight()
hero.moveDown()
老對手
# 你可以像名牌那樣使用變量
enemy1 = "Kratt"
enemy2 = "Gert"
enemy3 = "Ursa"
# 你可以像名牌那樣使用變量
enemy1 = "Kratt"
enemy2 = "Gert"
enemy3 = "Ursa"
hero.attack(enemy1)
hero.attack(enemy1)
hero.attack(enemy2)
hero.attack(enemy2)
hero.attack(enemy3)
hero.attack(enemy3)
##########用下面的可能更方便#################
# while True:
# enemy = hero.findNearestEnemy()
# if enemy:
# hero.attack(enemy)
###########################################
名稱大師
# 你的英雄不知道這些敵人的名字!
# 這眼鏡給了你 “findNearestEnemy” 尋找最近敵人的能力。
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
矮人之亂
# 在 while true循環里,使用 findNearestEnemy() 并攻擊!
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
KITHMAZE最終歷險
# 使用while-true循環移動并攻擊目標
while True:
hero.moveRight()
hero.moveUp()
enemy = hero.findNearestEnemy()
hero.attack(enemy)
hero.attack(enemy)
hero.moveRight()
hero.moveDown()
hero.moveDown()
hero.moveUp()
KithGard之門
# 建造三個圍欄來阻擋食人魔!
hero.moveDown()
hero.buildXY("fence", 36, 34)
hero.buildXY("fence", 36, 30)
hero.buildXY("fence", 36, 27)
while True:
hero.moveRight()
邊地森林
平原森林保衛戰
# 在標記上建造兩個圍欄保護村民
# 把鼠標放在地圖上得到X,Y坐標
hero.buildXY("fence", 40, 52)
hero.buildXY("fence", 40, 20)
羊腸小道
# 到小路的盡頭去,并在那兒修一個柵欄。
# 利用你的 moveXY(x, y) 函數。
# 這是路徑的第一個節點。
hero.moveXY(36, 59)
# 移動到路徑的第二個節點。
hero.moveXY(37, 13)
# 移動到路徑的第三個節點
hero.moveXY(73, 63)
# 用盾牌提供保護
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.shield()
# 建造柵欄抵擋食人魔。
hero.buildXY("fence", 72, 25)
林地小屋
# 通過林地,務必留意危險!
# 這些森林小屋中可能有食人魔!
hero.moveXY(19, 33)
enemy = hero.findNearestEnemy()
# if語句會檢查某變量是否有食人魔。
if enemy:
hero.attack(enemy)
hero.attack(enemy)
pass
hero.moveXY(49, 51)
enemy = hero.findNearestEnemy()
if enemy:
# 攻擊這里的敵人:
hero.attack(enemy)
hero.attack(enemy)
# pass不做任何事情,它只負責結束if語句
pass
hero.moveXY(58, 14)
enemy = hero.findNearestEnemy()
# 使用if語句檢查敵人是否存在:
if enemy:
# 如果敵人存在,就攻擊它:
hero.attack(enemy)
hero.attack(enemy)
pass
if的盛宴
# 在食人魔的營地中打敗它們!
while True:
enemy = hero.findNearestEnemy()
# 使用一個 “if” 語句去檢查是否有敵人存在:
if enemy:
# 攻擊敵人,如果存在的話:
hero.attack(enemy)
hero.attack(enemy)
背靠背
# 待在中間防守!
while True:
enemy = hero.findNearestEnemy()
if enemy:
# 亦或主動出擊...
hero.attack(enemy)
pass
else:
# 亦或回到你的陣地防守。
hero.moveXY(40, 34)
hero.say("爸爸回來了!")
pass
森林劈斬者
# 盡可能經常使用你的新技能“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
邊地僵局
# 矮人正在攻擊!
# 攻擊會有規律的一波波襲來。
# 可以的話,使用劈斬來清理大量敵人。
while True:
enemy = hero.findNearestEnemy()
# 使用帶有‘isReady’的if語句來檢查 “cleave”
if hero.isReady("cleave"):
# 劈斬!
hero.cleave(enemy)
pass
# 否則,如果 cleave 還沒準備好的話:
else:
# 攻擊最近的食人魔!
hero.attack(enemy)
pass
測距儀
# 食人魔正在森林中巡視!
# 使用distanceTo方法找到敵人在哪。
# 說出與每個敵人的距離,告訴大炮向哪開火!
enemy1 = "Gort"
distance1 = hero.distanceTo(enemy1)
hero.say(distance1)
enemy2 = "Smasher"
distance2 = hero.distanceTo(enemy2)
# 說出distance2變量!
hero.say(distance2)
# 找到并說出與剩余敵人之間的距離:
# 不要攻擊友方!
friend3 = "Charles"
pass
enemy4 = "Gorgnub"
distance4 = hero.distanceTo(enemy4)
hero.say(distance4)
while True:
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)
if distance < 10:
# 如果他們與農民太近,就攻擊他們
# 當然,你也可以不用放大招
if hero.isReady("cleave"):
hero.cleave(enemy)
pass
else:
hero.attack(enemy)
pass
# 否則的話,呆在農民旁邊!使用else
else:
hero.moveXY(40, 38)
發狂的矮人
# 又一個寶箱等待英雄打開!
# 攻擊寶箱來打開它。
# 有些食人魔矮人可不會呆呆地站著挨打!
# 當食人魔離你太近時,你得學著保護你自己
while True:
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)
if hero.isReady("cleave"):
# 如果劈斬就緒,優先使用劈斬:
if hero.isReady("cleave"):
hero.cleave(enemy)
pass
elif distance < 5:
# 攻擊靠近并離你最近的食人魔矮人
hero.attack(enemy)
pass
else:
# 否則,嘗試打開寶箱:
# Use the name of the chest to attack: "Chest".
hero.attack("Chest")
pass
躍火林中
# 在這關,別碰惡魔石!往其他方向移動避開它們!
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:
# 如果沒有惡魔石,那就去到中間。
hero.moveXY(40, 22)
pass
鄉村漫游者
# 這定義了findAndAttackEnemy函數
def findAndAttackEnemy():
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
# 這段代碼不是函數的一部分。
while True:
# 現在你可以使用findAndAttackEnemy在村子里巡邏
hero.moveXY(35, 34)
findAndAttackEnemy()
# 現在移動到右側入口。
hero.moveXY(60, 31)
# 使用findAndAttackEnemy
findAndAttackEnemy()
邊地之叉
# 一大波食人魔正在到來!
# 使用 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 變量。
交給劈斬者
# 一大波食人魔正在到來!
# 使用 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 變量。
友人和敵人
# 農民和士兵聚集在森林。
# 命令農民戰斗,苦工遠離!
while True:
friend = hero.findNearestFriend()
if friend:
hero.say("To battle, " + friend.id + "!")
# 尋找最近的敵人,然后讓他們滾蛋
enemy = hero.findNearestEnemy()
if enemy:
hero.say("Go away!," + enemy.id + "!")
巫師之門
# 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(41, 26)
hero.say(aga)
未知的距離
# 你的任務是告訴他獸人的距離。
# 這個函數尋找最近的敵人,并返回距離。
# 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)
金幣屑
# 跟隨硬幣的軌跡來到紅色 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)
返回荊棘農場
# 這個函數 “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, 35)
# 在底部入口處使用“maybeBuildTrap” !
maybeBuildTrap(43, 20)
金幣草地
# 收集每片草地的所有金幣。
# 使用旗子在草地間移動。
# 當你準備好放置旗子時點擊“提交”
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)
插旗子
# 在你想要建造陷阱的位置插旗
# 當你沒有在建造陷阱的時候,收集金幣!
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)
小心陷阱
# 如果你試圖攻擊一個遠處的敵人,你的英雄會忽略掉所有的旗子而朝它沖過去。
# 你需要確保你只攻擊靠近自己的敵人!
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:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
hero.attack(enemy)
通信尸體
# 你可以使用旗子來選擇不同的策略
# 在這關,綠色旗子代表你要移動到旗子處。
# 遇到黑旗就意味著你要在旗子那使用cleave
# 醫生會在紅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(nearest)
elif nearest and hero.distanceTo(nearest) < 10:
# 攻擊!
hero.attack(nearest)
pass
豐富的覓食
# 使用 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)
圍攻STONEHOLD
# 這里可能需要你升級一下裝備才能打贏
# 幫助你的朋友擊敗索科塔派出的手下。
# 你需要更好的裝備和策略去贏得戰斗。
# 標記可能有用,不過這取決于你——要有創造性哦!
# 在圍欄后有位醫生。移動到 X 處得到治療!
while True:
# 創建旗子標識
flag = hero.findFlag("green")
# 發現周邊敵人
enemy = hero.findNearestEnemy()
# 到達旗子的位置
if flag:
hero.pickUpFlag(flag)
if enemy:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
hero.attack(enemy)
沙漠
強壯的沙牦牛
# 當牦牛靠近時向右移動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位置。
newX = x + 10
# 使用 moveXY 來移動!
hero.moveXY(newX, y)
pass
綠洲
# 向綠洲移動
# 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來移動到左邊
newX = x - 10
# Use moveXY to move to the new x, y position.
hero.moveXY(newX, y)
pass
else:
# 通過在你的X坐標上加上10來移動到右邊
plusX = x + 10
# Use moveXY to move to the new x, y position.
hero.moveXY(plusX, y)
pass
盆地的踐踏
# **如果發現代碼執行有問題,可能需要換一個瀏覽器再試試**,別問我怎么知道的,我快被牛頂死了!!!
# 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)
薩文路
# 可能需要升級一下裝備!!!
# 到達綠洲。小心新的敵人:食人魔偵察兵!
# 通過添加你當前的X位置和Y位置以向上向右走
while True:
# If there's an enemy, attack.
enemy = hero.findNearestEnemy()
if enemy:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
hero.attack(enemy)
else:
xPos = hero.pos.x
yPos = hero.pos.y
# Else, keep moving up and to the right.
xPos = xPos + 5
yPos = yPos +5
hero.moveXY(xPos, yPos)
pass
十字路口
# 使用 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)
雷蹄
# 到達綠洲,
# 用柵欄引導砂牦牛到你去的地方
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:
hero.buildXY("fence", yak.pos.x, yak.pos.y + 10)
# 如果耗牛在你后面,在它前面10m 建立一個柵欄
pass
else:
# 向右移動10走向綠洲
hero.moveXY(hero.pos.x + 10, hero.pos.y)
pass
操作“殺鹿”
# Lure the ogres into a trap. These ogres are careful.
# They will only follow if the hero is injured.
# This function checks the hero's health
# 并返回一個布爾型(Boolean)的值。
def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False
while True:
# Move to the X only if shouldRun() returns True
enemy = hero.findNearestEnemy()
value = shouldRun()
if value:
hero.moveXY(75, 37)
# Else, attack!
else:
if hero.isReady("cleave") and hero.distanceTo(enemy) < 5:
hero.cleave(enemy)
else:
hero.attack(enemy)
醫療注意
# 當你生命值少于一半時,請求醫療人員的治療。
while True:
currentHealth = hero.health
# 我的裝備不好,就改動了一下零界值
healingThreshold = (hero.maxHealth * 4) / 5
# 如果你當前的健康值少于下限,
# move to the healing point and say, "heal me".
# 否則的話,攻擊。你需要戰斗的更狠點!
enemy = hero.findNearestEnemy()
if currentHealth > healingThreshold:
if enemy:
distance = hero.distanceTo(enemy)
if hero.isReady("cleave") and distance < 5:
hero.cleave(enemy)
else:
hero.attack(enemy)
else:
hero.moveXY(65, 46)
hero.say("heal me")
跟上時間
# 使用你的新技能來選擇你要做什么 hero.time
while True:
# 需要買一件衣服來抵擋傷害!!!
# 如果是頭十秒,進攻。
if hero.time < 10:
enemy = hero.findNearestEnemy()
if enemy:
distance = hero.distanceTo(enemy)
if hero.isReady("cleave") and distance < 9:
hero.cleave(enemy)
else:
hero.attack(enemy)
pass
# 反之,如果是前35秒,收集金幣。
elif hero.time < 35:
coin = hero.findNearestItem()
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
pass
# 后35秒,加入救助。
else:
enemy = hero.findNearestEnemy()
if enemy:
distance = hero.distanceTo(enemy)
if hero.isReady("cleave") and distance < 9:
hero.cleave(enemy)
else:
hero.attack(enemy)
pass
囤積金幣
# 收集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 += coin.value
pass
if totalGold >= 25:
# 這會中斷循環并且執行循環下面的語句
# The loop ends, code after the loop will run.
break
# 完成收集金幣!
hero.moveXY(58, 33)
# 去告訴 Naria 你收集了多少金幣。
hero.say("你好 Naria, 我收集了 " + totalGold + " 枚金幣")
誘餌鉆
# 我們在測試一個新的戰斗單位:誘餌。
# 創建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 built " + decoysBuilt +' decoies!')
復查
# 第一點,打敗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()
hero.moveXY(coin.pos.x, coin.pos.y)
# 去掉這行 say()。
# hero.say("我應該收集金幣!")
# 移動到出口。
hero.moveXY(76, 32)
沙漠戰役
# while循環重復直到條件為否。
# 得去提升自己的護甲
ordersGiven = 0
while ordersGiven < 5:
# 在站場上移動和排列你的盟友。 (如果你是直接在他們面前,他們只能聽到你的。)
hero.moveXY(hero.pos.x, hero.pos.y - 10)
# Order your ally to "Attack!" with hero.say
# They can only hear you if you are on the X.
hero.say("Attack!")
# Be sure to increment ordersGiven!
ordersGiven += 1
while True:
enemy = hero.findNearestEnemy()
# 當你下達完命令,立即加入戰斗!
maxhealth = hero.maxHealth
if enemy:
distance = hero.distanceTo(enemy)
if hero.isReady("cleave") and distance < 9:
hero.cleave(enemy)
else:
hero.attack(enemy)
if hero.health < hero.maxHealth / 2:
hero.moveXY(10, 34)
塵埃
# 使用循環直到你有足夠的擊殺10個芒奇金人
attacks = 0
while attacks < 10:
# 攻擊最近的敵人!
enemy = hero.findNearestEnemy()
if enemy:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
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)
別沖過去,安靜點
# Dodge the cannons and collect 8 gems.
# Watch out, cannons are ready to fire!
# 以一個特殊的方式緩慢移動去迷惑敵人
# This function returns a value from 0 to 30 (0 <= n < 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.time
x = mod30(time) + 25
y = mod40(time) + 10
hero.moveXY(x, y)
ZIG ZAG ANG 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 = hero.time
if time < 30:
y = 10 + 3 * mod15(time)
else:
y = 20 + 3 * mod9(time)
x = 10 + time
hero.moveXY(x, y)
沙漠三角形
# 只攻擊在敵軍名稱(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])
SARVEN 救世主
# 一個數組(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(25, 30)
hero.buildXY("fence", 30, 30)
BANK RAID
# Wait for ogres, defeat them and collect gold.
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 數組得到一個金幣。
# 這里加上:如果敵人出現,則停止收集金幣,優先攻擊敵人
# 發現運行會超時,所以還是買厚一點的護甲吧,避免被砍死
# enemyOccur = hero.findNearestEnemy()
# if enemyOccur:
# break
coin = coins[coinIndex]
# 收集那個金幣。
hero.moveXY(coin.pos.x, coin.pos.y)
# 給 coinIndex 的值增加 1。
coinIndex += 1
潛伏
# 用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
SARVEN 牧羊人
# 使用 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)
pass
enemyIndex += 1
# 在兩波敵人之間,移動回中央。
hero.moveXY(40, 32)
撿閃亮東西的人
# 很快的獲取最多的金幣
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)
pass
coinIndex += 1
瘋狂的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:
hero.attack(farthest)
pass
克隆沖突
# 你需要更好的策略和好的裝備來贏得這關。
# 你的克隆體將會擁有和你一樣的裝備
# 但是,他們沒有經驗,不會用特殊技能。
# 自己提出的要求和思路:
# 注意:不要殺牛和thoktar
# 1.先殺死所有射箭的
def killBowman(enemies):
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type == 'archer':
while enemy.health > 0:
if hero.isReady("cleave") and hero.distanceTo(enemy) < 5:
hero.cleave(enemy)
else:
hero.attack(enemy)
enemyIndex += 1
# 2.再來殺其他人
def killsoldiers(enemies):
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != 'sand-yak':
while enemy.health > 0:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
hero.attack(enemy)
enemyIndex += 1
while True:
enemies = hero.findEnemies()
killBowman(enemies)
killsoldiers(enemies)
峭壁追逐
# 抓住 Pender Spellbane 去了解她的秘密。
while True:
# Pender是這里唯一的朋友,所以她總是在最近的位置。
pender = hero.findNearest(hero.findFriends())
if pender:
# moveXY()將移動到 Pender 在的位置,
# 但是她會向遠離你的位置移動。
# hero.moveXY(pender.pos.x, pender.pos.y)
# move()只一次移動一步。
# 所以你可以用它來追蹤你的目標。
hero.move(pender.pos)
激流渦旋
# 使用對象枚舉來走安全的路,并收集寶石。
# 在本關你不能夠使用 moveXY()方法!使用 move()來移動
gems = hero.findItems()
while hero.pos.x < 20:
# move()移動物體通過 x 和 y 的屬性,不僅僅是數字。
hero.move({'x': 20, 'y': 35})
while hero.pos.x < 25:
# 一個寶石的位置是一個對象,有 x 和 y 屬性。
gem0 = gems[0]
hero.move(gem0.pos)
# 當你的 x 小于30的時候,
# 使用物體移動到30,35位置
while hero.pos.x < 30:
hero.move({'x':30, 'y':35})
# 當你的 x 小于35的時候
# 移動到寶石[1]的位置
while hero.pos.x < 35:
gem1 = gems[1]
hero.move(gem1.pos)
# 拿到最后一對寶石!
while hero.pos.x < 40:
hero.move({'x': 40, 'y':35})
while hero.pos.x < 45:
gem2 = gems[2]
hero.move(gem2.pos)
while hero.pos.x < 50:
hero.move({'x': 50, 'y':35})
while hero.pos.x < 55:
gem3 = gems[3]
hero.move(gem3.pos)
食人魔山谷挖寶
# 一大群食人魔來之前你只有20秒時間!
# 盡可能去撿金幣,然后你撤退到你柵欄后面的基地里!
# 如果方法一和方法二都沒有湊齊60枚金幣,那可能就是英雄移動速度太慢了,得換英雄。
# 方法一:只收集最近的金幣
def findNearestCoin(coins):
nearestCoin = None
distance = 999
Value = 0
itemIndex = 0
while itemIndex < len(coins):
Value = hero.distanceTo(coins[itemIndex])
if Value < distance:
nearestCoin = coins[itemIndex]
distance = Value
itemIndex += 1
return nearestCoin
# 方法二:只收集面值最大的金幣
def findBiggestCoin(coins):
biggestCoin = None
Value = 0
cost = 0
itemIndex = 0
while itemIndex < len(coins):
cost = coins[itemIndex].value
if Value < cost:
Value = cost
biggestCoin = coins[itemIndex]
itemIndex += 1
return biggestCoin
while hero.time < 20:
# 收集金幣
# hero.say("我應該撿點金幣")
items = hero.findItems()
# 方法一:只收集最近的金幣
coin = findNearestCoin(items)
# 方法二:只收集面值最大的金幣
# coin = findBiggestCoin(items)
hero.move(coin.pos)
while hero.pos.x > 16:
# 撤退到柵欄后面
# hero.say("我應該撤退")
hero.moveXY(16, 38)
# 建立柵欄擋住食人魔
hero.buildXY("fence", 21, 38)
安息之云指揮官
# 召喚一些士兵,然后引導他們去你的基地。
# 每個士兵消耗20金幣。
while hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
soldiers = hero.findFriends()
soldierIndex = 0
# 添加一個while 循環來命令所有的士兵。
while soldierIndex < 5:
soldier = soldiers[soldierIndex]
hero.command(soldier, "move", {"x": 50, "y": 40})
soldierIndex += 1
# 去加入你的朋友!
hero.moveXY(50,36)
傭兵山
# 收集金幣招募士兵,指揮他們攻擊敵人。
while True:
# 走到最近的金幣處。
# 使用 move 取代 moveXY,以便于你可以不斷發出命令。
item = hero.findNearest(hero.findItems())
if item:
hero.move(item.pos)
# hero.say("我需要金幣!") # 刪掉這一行以避免卡頓
# 如果錢夠了就招募士兵。
if hero.gold > hero.costOf("soldier"):
hero.say("我應該召集些什么幫手!")
hero.summon("soldier")
enemy = hero.findNearest(hero.findEnemies())
if enemy:
# 遍歷你所有的士兵,命令他們攻擊。
soldiers = hero.findFriends()
soldierIndex = 0
while soldierIndex < len(soldiers):
soldier = soldiers[soldierIndex]
# 使用 attack 命令讓你的士兵們攻擊。
hero.command(soldier, "attack", enemy)
soldierIndex += 1
木材守衛
while True:
# 收集金子
item = hero.findNearestItem()
if item:
hero.move(item.pos)
# 如果你有足夠的金幣,召喚一個士兵。
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
# 使用 for 循環來命令每個士兵。
# for 循環有兩個部分『for X in Y』
# Y 是被循環的數組結構
# Y 中的每個元素都會執行,X 會被設置稱當前循環的個體
for friend in hero.findFriends():
if friend.type == "soldier":
hero.command(friend, "move", {'x': 70, 'y' :45})
enemy = friend.findNearestEnemy()
# 如果這有一個敵人,命令她攻擊。
# 否則的話,移動她到地圖的右邊。
if enemy:
hero.command(friend, "attack", enemy)
零和
# 在兩分鐘內擊退敵人。
hero.cast("goldstorm")
while True:
enemies = hero.findEnemies()
nearestEnemy = hero.findNearest(enemies)
item = hero.findNearest(hero.findItems())
hero.move(item.pos)
# 你的英雄能收集金幣,召喚部隊。
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
# 在站場上,她也可以命令你的盟友。
friends = hero.findFriends()
for friend in friends:
hero.command(friend, "attack", friend.findNearest(enemies))
# 使用你的英雄的能力力挽狂瀾。
動物園的管理員
# 保護籠子。
# 放一個士兵在每一個 X 的位置
points = []
points[0] = {"x": 33, "y": 42}
points[1] = {"x": 47, "y": 42}
points[2] = {"x": 33, "y": 26}
points[3] = {"x": 47, "y": 26}
# 1.收集80金幣。
while hero.gold < 80:
item = hero.findNearest(hero.findItems())
hero.move(item.pos)
# 2.建造4個士兵。
for i in range(4):
hero.summon("soldier")
# 3.派你的士兵到特定的位置上。
while True:
friends = hero.findFriends()
for j in range(len(friends)):
point = points[j]
friend = friends[j]
enemy = friend.findNearestEnemy()
if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
# 命令友方攻擊。
hero.command(friend, "attack", enemy)
else:
# 命令的朋友移動到特定點上。
hero.command(friend, "move", point)
戰略祭品
# 收集80金幣
while hero.gold < 80:
item = hero.findNearestItem()
hero.move(item.pos)
# 建造4個士兵用來做誘餌
for i in range(4):
hero.summon("soldier")
# 派你的士兵到指定位置。
points = []
points[0] = { "x": 13, "y": 73 }
points[1] = { "x": 51, "y": 73 }
points[2] = { "x": 51, "y": 53 }
points[3] = { "x": 90, "y": 52 }
friends = hero.findFriends()
# 使用范圍來在數組中循環
# 讓friends去指定地點,命令他們移動
for j in range(len(friends)):
point = points[j]
friend = friends[j]
hero.command(friend, "move", point)
狩獵派對
# 命令你的部隊向東移動,攻擊任何看到的食人魔。
# 使用 for 循環和 findFriends方法。
# 你能在你的士兵上使用findNearestEnemy()來獲取他們的而不是你的最近的敵人。
while True:
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
newX = friend.pos.x + 0.5
newY = friend.pos.y
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
else:
hero.command(friend, "move",{'x': newX, 'y': newY} )
維他力量
# 這關會教你怎么定義你自己的函數。
# 放在函數內的代碼并不會立刻執行, 而是先保存好, 以備后用.
# 這個函數會讓你的英雄收集最近的金幣。
def pickUpNearestCoin():
items = hero.findItems()
nearestCoin = hero.findNearest(items)
if nearestCoin:
hero.move(nearestCoin.pos)
# 這個函數會讓你的英雄召喚一個士兵。
def summonSoldier():
# 在這里寫下代碼:當你有足夠金幣時召喚士兵
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
# 這個函數會命令你的士兵攻擊最近的敵人
def commandSoldiers():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
else:
hero.command(soldier, "move", hero.pos)
while True:
# 在你的循環里,你可以"調用"你在上面定義的函數
# 下面幾行代碼會讓 "pickUpNearestCoin" 函數里的代碼被執行。
pickUpNearestCoin()
# 在這里調用 summonSoldier
summonSoldier()
# 在這里調用 commandSoldiers
commandSoldiers()
雙生花
# 如果花匠受傷了,雙生花會縮小!
def summonSoldiers():
if hero.gold >= hero.costOf("soldier"):
hero.summon("soldier")
# 定義函數:commandSoldiers
def commandSoldiers():
for friend in hero.findFriends():
if friend.type == "soldier":
enemy = friend.findNearestEnemy()
peasant = hero.findByType("peasant")[0]
if enemy:
hero.command(friend, "attack", enemy)
else:
hero.command(friend, "move", peasant.pos)
# 定義函數:pickUpNearestCoin
def pickUpNearestCoin():
items = hero.findItems()
nearestCoin = hero.findNearest(items)
if nearestCoin:
hero.move(nearestCoin.pos)
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
花的幾何學
# 你現在有花環! 你可以做:
# toggleFlowers(true/false) - 打開或關閉鮮花。
# setFlowerColor("random") - 也可以是 "pink", "red", "blue", "purple", "yellow", or "white"。
# 以下是繪制圖形的一些函數:
# x,y - 圖形的中心
# size - 圖形的大小(半徑,邊長)
def drawCircle(x, y, size):
angle = 0
hero.toggleFlowers(False)
while angle <= Math.PI * 2:
newX = x + (size * Math.cos(angle))
newY = y + (size * Math.sin(angle))
hero.moveXY(newX, newY)
hero.toggleFlowers(True)
angle += 0.2
def drawSquare(x, y, size):
hero.toggleFlowers(False)
cornerOffset = size / 2
hero.moveXY(x - cornerOffset, y - cornerOffset)
hero.toggleFlowers(True)
hero.moveXY(x + cornerOffset, y - cornerOffset)
hero.moveXY(x + cornerOffset, y + cornerOffset)
hero.moveXY(x - cornerOffset, y + cornerOffset)
hero.moveXY(x - cornerOffset, y - cornerOffset)
redX = {"x": 28, "y": 36}
whiteX = {"x": 44, "y": 36}
# 選擇一種顏色。
hero.setFlowerColor("red")
# 在redX上繪制一個大小為10的圓圈。
drawCircle(redX.x, redX.y, 10)
# 改變顏色!
hero.setFlowerColor("blue")
# 在whiteX上繪制一個大小為10的正方形。
drawSquare(whiteX.x, whiteX.y, 10)
# 現在嘗試繪制任何你想要的!
# 你現在有花環! 你可以做:
# toggleFlowers(true/false) - 打開或關閉鮮花。
# setFlowerColor("random") - 也可以是 "pink", "red", "blue", "purple", "yellow", or "white"。
# 以下是繪制圖形的一些函數:
# x,y - 圖形的中心
# size - 圖形的大小(半徑,邊長)
def drawCircle(x, y, size):
angle = 0
hero.toggleFlowers(False)
while angle <= Math.PI * 2:
newX = x + (size * Math.cos(angle))
newY = y + (size * Math.sin(angle))
hero.moveXY(newX, newY)
hero.toggleFlowers(True)
angle += 0.2
def drawSquare(x, y, size):
hero.toggleFlowers(False)
cornerOffset = size / 2
hero.moveXY(x - cornerOffset, y - cornerOffset)
hero.toggleFlowers(True)
hero.moveXY(x + cornerOffset, y - cornerOffset)
hero.moveXY(x + cornerOffset, y + cornerOffset)
hero.moveXY(x - cornerOffset, y + cornerOffset)
hero.moveXY(x - cornerOffset, y - cornerOffset)
redX = {"x": 28, "y": 36}
whiteX = {"x": 44, "y": 36}
# 選擇一種顏色。
hero.setFlowerColor("red")
# 在redX上繪制一個大小為10的圓圈。
drawCircle(redX.x, redX.y, 10)
# 改變顏色!
hero.setFlowerColor("blue")
# 在whiteX上繪制一個大小為10的正方形。
drawSquare(whiteX.x, whiteX.y, 10)
# 現在嘗試繪制任何你想要的!
山花林
# 這個關卡是制作花卉藝術的地方。
# 真正的目標是嘗試并獲得樂趣!
# 如果你畫了至少1000朵花的東西,那么你將在這個關卡上"succeed"。
def drawCircle(x, y, size):
while size < 50:
angle = 0
hero.toggleFlowers(False)
while angle <= Math.PI * 2:
newX = x + (size * Math.cos(angle))
newY = y + (size * Math.sin(angle))
hero.moveXY(newX, newY)
hero.toggleFlowers(True)
angle += 0.2
size += 5
centre = {'x':85, 'y':70}
hero.setFlowerColor("random")
drawCircle(centre.x, centre.y, 1)
獵手和獵物
# 食人魔正試圖除掉你的馴鹿!
# 當召喚士兵進攻時,讓弓箭手回來。
def pickUpCoin():
# 收集硬幣。
item = hero.findNearest(hero.findItems())
if item.type == 'coin' or item.type =='gem':
hero.move(item.pos)
pass
def summonTroops():
# 如果你有黃金就召喚士兵。
if hero.gold > hero.costOf("archer"):
hero.summon("archer")
#if hero.gold > hero.costOf("soldier"):
# hero.summon("soldier")
# 這個函數有一個名為士兵的參數。
# 參數就像變量一樣。
# 調用函數時確定參數的值。
def commandSoldier(soldier):
# 士兵要攻擊敵人。
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
else:
hero.command(soldier, "move", hero.pos)
# 編寫一個命令弓箭手的函數來告訴你的弓箭手怎么做!
# 它應該有一個參數將表示在被調用時傳遞給函數的射手。
# 弓箭手應該只攻擊距離25米以內的敵人,否則原地待命。
def commandArcher(archer):
enemy = archer.findNearestEnemy()
if enemy and archer.distanceTo(enemy) <= 25:
hero.command(archer, "attack", enemy)
else:
hero.command(archer, "move", hero.pos)
while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == "soldier":
# 這位朋友將被分配給commandSoldier函數中的士兵變量
commandSoldier(friend)
elif friend.type == "archer":
# 務必指揮你的弓箭手。
commandArcher(friend)
圖書館的謀士
# Hushbaum已被食人魔伏擊!
# 她正在忙著醫治她的士兵,你應該命令他們去戰斗!
# 如果食人魔認為他們可以攻擊到Hushbaum或弓箭手,那么他們會派出更多的部隊,所以將他們放在圈子里面!
# 士兵們圍成一圈防守起來。
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
hero.command(soldier, "defend", defendPos);
# 找到最強大的目標(生命值最高)
# 該函數返回一些東西! 當你調用函數時,你會得到一些回報。
def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()
# 找出哪個敵人的生命值最高,并將bestTarget設置為該敵人。
for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
# 如果有大食人魔的話,只關注弓箭手的火力。
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
# 如果最強大的目標擁有超過15的生命值,則攻擊該目標。 否則,攻擊最近的目標。
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
archerTarget = None
while True:
# 如果射手目標被擊敗或不存在,那就找一個新的。
if not archerTarget or archerTarget.health <= 0:
# 將archerTarget設置為findStrongestTarget()返回的目標
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
# 創建一個包含弓箭手的變量。
archers = hero.findByType("archer")
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers));
# 使用commandArcher()命令你的弓箭手
for j in range(len(archers)):
archer = archers[j]
commandArcher(archer)
我們中的間諜
# 內門可以撐很長時間。
# 然而,其中一個農民是食人魔的間諜!
# 有一個提示!間諜的名字中有一個字母是"z"。
# 這個函數可以檢查單詞中的特定字母。
# 一個字符串只是一個數組! 像數組一樣循環它。
def letterInWord(word, letter):
for i in range(len(word)):
character = word[i]
# 如果字符等于字母,則返回 True
if character == "z" or character == "Z":
return True
# 因為字母不在單詞里,所以返回 False
return False
spyLetter = "z"
friends = hero.findFriends()
for friend in friends:
friendName = friend.id
if letterInWord(friendName, spyLetter):
# 揭露間諜!
hero.say(friendName + " is a spy!")
else:
hero.say(friendName + " is a friend.")
冰獵人
# 追捕4只牦牛。 只選擇小的。
# 小牦牛名稱包含一個"bos"子字符串。
# 這個函數檢查一個單詞是否包含一個子字符串。
def isSubstring(word, substring):
# 我們通過迭代開始索引。
rightEdge = len(word) - len(substring)
# 循環訪問單詞的索引。
for i in range(rightEdge + 1):
# 對于它們中的每一個循環通過子字符串
for j in range(len(substring)):
# 為單詞的索引使用偏移量。
shiftedIndex = i + j
# 如果字母不一樣:
if word[shiftedIndex] != substring[j]:
# 檢查Word中的下一個開始索引。
break
# 如果它是子字符串中的最后一個字母:
if j == len(substring) - 1:
# 然后子字符串在單詞中。
return True
# 我們還沒有找到這個詞的子字符串。
return False
# 循環通過所有敵人。
enemies = hero.findEnemies()
for e in range(len(enemies)):
enemy = enemies[e]
# 使用函數isSubstring來檢查
# 37/5000 如果敵方名稱(id)包含“"bos":
if enemy and isSubstring(enemy.id, "bos"):
# 然后打敗它。
while enemy.health > 0:
hero.attack(enemy)
模擬侵略
# 保護村莊免受食人魔侵害。
# 注意食人魔,農民,其中有食人魔偽裝成“農民”。
# 該功能檢查文本是否以該詞開頭。
def startsWith(text, word):
# 如果這個詞更長,那么文本:
if len(word) > len(text):
return False
# 循環訪問單詞和文本的索引。
for index in range(len(word)):
# 如果具有相同索引的字符不同:
if word[index] != text[index]:
# 然后這個詞與文本不一致。
return False
# 我們檢查了所有的文本,它們都是一樣的。
return True
ogreNameStart = "Zog"
while True:
enemy = hero.findNearestEnemy()
suspect = hero.findNearest(hero.findFriends())
#if hero.gold > hero.costOf("archer"):
# hero.summon("archer")
#if suspect.type == "archer":
# soldier = suspect
# 使用函數“startsWith”來檢查
# 如果有嫌疑者的名字(id)以“Zog”開頭,則攻擊:
if enemy:
# 否則,如果有敵人,則攻擊它:
while enemy.health > 0:
# hero.command(soldier, "attack", suspect)
hero.attack(enemy)
# hero.command(soldier, "attack", enemy)
# 其他情況返回紅色的X標記:
if startsWith(suspect.id, ogreNameStart):
while suspect.health > 0:
hero.attack(suspect)
# hero.command(soldier, "attack", suspect)
hero.move({"x": 27, "y": 27})
# hero.command(soldier, "move", {"x": 27, "y": 27})
吵鬧 安靜
# 把英雄和他們的寵物移到出口處
def onHear(event):
# 獲取音量和口令。
words = event.message.split(" ")
volume = words[0]
password = words[1]
# 口令是否應該大聲:
if volume == "Loud":
# 寵物用大寫字母重復它。
pet.say(words[1].toUpperCase())
# 口令是否應該保持安靜:
if volume == "Quiet":
# 寵物以小寫字母重復。
pet.say(words[1].toLowerCase())
pet.moveXY(pet.pos.x+ 24, pet.pos.y)
def passDoor():
guard = hero.findNearest(hero.findFriends())
password = guard.password
# 口令是否應該大聲:
if guard.isLoud:
# 在口令上使用.toUpperCase()方法。
hero.say(password.toUpperCase())
pass
# 口令是否應該保持安靜:
elif guard.isQuiet:
# 在口令上使用.toLowerCase()方法。
hero.say(password.toLowerCase())
pass
hero.moveXY(hero.pos.x+ 24, hero.pos.y)
# 讓寵物聽到警衛的聲音。
pet.on("hear", onHear)
# 英雄通過門的代碼。
hero.moveXY(10, 14)
passDoor()
passDoor()
方便敵方
# Ogres are hiding in woods. Protect the peasants.
# The last word in the peasants' messages are a hint.
for x in range(8, 73, 16):
hero.moveXY(x, 22)
# 農民知道需要召喚誰。
peasant = hero.findNearest(hero.findFriends())
message = peasant.message
if message:
# 單詞由空格分隔。
words = message.split(" ")
# "words"是來自"message"的一組單詞。
# 最后一句話。這是必需的單位類型。
soldierName = words[1]
# 召喚所需的單位類型。
hero.summon(soldierName)
while True:
for i in range(len(hero.built)):
unit = hero.built[i]
# 命令單位保衛單位的陣地。
enemy = unit.findNearestEnemy()
if enemy:
hero.command(unit, "attack", enemy)
# 捍衛你最后一個點:
hero.moveXY(72, 22)
enemy = hero.findNearestEnemy()
if enemy and hero.distanceTo(enemy)<10:
while enemy.health > 0:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
hero.attack(enemy)
鋼爪間隙
# 這個關卡介紹了%操作符,也稱為模操作符。
# a % b返回除以b的余數
# 當索引可能大于長度時,這可以用于環繞數組的開頭。
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}]
summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]
# 你用360金開始建造一個士兵和弓箭手的混合體。
# Self.Bug是你曾經建造的一個部隊數組。
# 在這里,我們使用"len(self.built) % len(summonTypes)"來環繞召喚類型數組。
def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def commandTroops():
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
# 根據friendIndex使用%來環繞防衛點
friendIndex = defendPoints[i % len(defendPoints)]
# 命令你的手下捍衛防衛點
hero.command(friend, "move", friendIndex)
enemy = friend.findNearestEnemy()
if enemy:
if friend.distanceTo(enemy) < 25:
hero.command(friend, "attack", enemy)
if friend.health < friend.maxHealth / 3:
hero.command(friend, "move", {"x":50, "y":50})
while True:
summonTroops()
commandTroops()
無暇的寶石對
# 收集4對寶石。
# 每一對都必須包含相同的寶石。
# 該函數返回兩個具有相同值的項目。
def findValuePair(items):
# 檢查數組中的每個可能的對。
# 迭代索引'i'從0到最后一個。
for i in range(len(items)):
itemI = items[i];
# 從0到最后迭代索引'j'。
for j in range(len(items)):
# 如果它是相同的元素,則跳過它。
if i == j:
continue
itemJ = items[j];
# 如果我們發現一對有兩個相同的寶石,則返回它們。
if itemI.value == itemJ.value:
return [itemI, itemJ]
# 如果沒有存在配對,則返回一個空數組。
return None
while True:
gems = hero.findItems()
gemPair = findValuePair(gems)
# 如果發現寶石對存在,則收集寶石!
if gemPair:
gemA = gemPair[0]
gemB = gemPair[1]
# 移動到第一個寶石處。
hero.moveXY(gemA.pos.x, gemA.pos.y)
# 返回從向導中得到加速。
hero.moveXY(40,44)
# 然后移動到第二個寶石處。
hero.moveXY(gemB.pos.x, gemB.pos.y)
# 返回從向導中得到加速。
hero.moveXY(40,44)
pass
馴鹿蘇醒
# 這個數組包含每個馴鹿的狀態。
deerStatus = [ 'asleep', 'asleep', 'asleep', 'asleep', 'asleep' ]
# 這個數組包含了我們的馴鹿。
friends = hero.findFriends()
# 循環每只馴鹿,找到醒著的:
for deerIndex in range(len(friends)):
reindeer = friends[deerIndex]
if reindeer.pos.y > 30:
# Y位置>30的馴鹿不在筆下。
# 如果是這樣,把馴鹿的條目設置為"awake"。
deerStatus[deerIndex] = 'awake'
pass
# 循環歷遍每只鹿狀態并報告給Melek。
for statusIndex in range(len(deerStatus)):
# 告訴Merek馴鹿索引及其位置。
# 比如說“馴鹿2是睡著了”。
hero.say('reindeer ' + statusIndex + ' ' +deerStatus[statusIndex])
pass
大炮陸戰隊
# 我們應該派士兵去保衛村莊。
# 我們還需要清除舊的陷阱。
# 對于這兩個目標,我們將使用炮兵!
# 炮兵可以發射士兵和反陷阱。
# 偵察兵準備了著陸區的地圖。
# 該地圖是單元格為字符串的二維數組。
landingMap = hero.findNearest(hero.findFriends()).landingMap
# 向大炮顯示行,列和目標類型。
# 要獲取元素,請使用array [i] [j]
# 首先,讓我們看看第0行和第0列。
cell = landingMap[0][0]
# 接下來,說出坐標和有什么。
hero.say("行 0 列 0 " + cell)
# 下一個單元格是第3行和第2列.
hero.say("行 3 列 2 " + landingMap[3][2])
# 現在就為下一點做好準備:
# 第二行和第一列。
hero.say("行 2 列 1" + landingMap[2][1])
# 第1行和第0列。
hero.say("行 1 列 0" + landingMap[1][0])
# 第0行和第2列。
hero.say("行 0 列 2" + landingMap[0][2])
# 第一行和第三列。
hero.say("行 1 列 3" + landingMap[1][3])
力量的源點
# 你需要找到并摧毀3個骷髏。
# 骨骼和物品在力量的源點召喚。
# 移動到某一點并說出咒語:"VENI"。
# 若要找到所需的點,請使用巫師的地圖。
# 0是不好的點。 正數是好的。
spell = "VENI"
# 地圖的點是一個二維數組的數字。
wizard = hero.findNearest(hero.findFriends())
powerMap = wizard.powerMap
# 該函數將網格轉換為x-y坐標。
def convert(row, col):
return {'x': 16 + col * 12, 'y': 16 + row * 12}
# 通過PoPMAP循環查找正數。
# 首先,循環遍歷行的索引。
for i in range(len(powerMap)):
# 每一行都是一個數組。 遍歷它。
for j in range(len(powerMap[i])):
# 獲取i行和j列的值。
pointValue = powerMap[i][j]
# 如果是正數:
if pointValue > 0:
# 使用convert來獲取XY坐標。
position = convert(i, j)
# 移動到那里,說"VENI"并做好準備!
hero.moveXY(position.x, position.y)
hero.say(spell)
# 找到并攻擊敵人
enemy = hero.findNearestEnemy()
if enemy:
while enemy.health > 0:
hero.attack(enemy)
周界防御
# 我們需要在村子周圍建哨塔。
# 每個農民都可以建造一座塔。
# 向他們展示建造的地方。
# 這些塔是自動的,會攻擊城外所有的單位。
# 首先隨著步驟20,沿著北邊界(y = 60)從x = 40移動到x = 80。
# `range` 不包括第二個邊緣。
for x in range(40, 81, 20):
# 在每個點上移動并說點什么。
hero.moveXY(x, 60)
hero.say("Here")
# 接著負步驟-20,沿著東邊界(x=80)從y=40到y=20。
for y in range(40, 19, -20):
hero.moveXY(80, y)
hero.say("Here")
# 繼續其余兩個邊界。
# 接下來是反步驟-20.從x = 60到x = 20的南方邊界(y = 20),0。
for x in range(60, 19, -20):
# 在每個點上移動并說點什么。
hero.moveXY(x, 20)
hero.say("Here")
# 接下來是步驟20,從y = 40到y = 60的西邊界(x = 20)。
for y in range(40, 61, 20):
hero.moveXY(20, y)
hero.say("Here")
# 別忘了藏在村子里。
hero.moveXY(50, 40)
網格雷區
# 食人魔在村子里行進。
# 我們有90秒的時間建造雷區。
# 我們將用他們嚴格的隊形來對付他們。
# 使用嵌套循環來構建網格雷區。
# 首先用步驟8迭代x從12到60的坐標。
for x in range(12, 12 + 8 * 6, 8):
# 用步驟8,對于每一個X迭代,Y從12到68。
for y in range(12, 12 + 8 * 7, 8):
# 對于每個點建立"fire-trap"。
hero.buildXY("fire-trap", x, y)
pass
# 每列后,最好避免陷入自己的陷阱。
hero.moveXY(hero.pos.x + 8, hero.pos.y)
# 現在等待并注意即將到來的食人魔。
# 當他們靠近(距離英雄約20米)時,與你的英雄一起引爆地雷。
# 只要在最近的地雷移動即可。
while True:
enemy = hero.findNearestEnemy()
if enemy and hero.distanceTo(enemy) < 20:
hero.moveXY(hero.pos.x - 8, y)
聯合作戰(絕對防御)
# 練習用取模從數組中循環取值
defendPoints = [{'x':66, 'y':48},{'x':66, 'y':38},{'x':66, 'y':28},{'x':56, 'y':48}, {'x':56, 'y':38}, {'x':56, 'y':28}, {'x':46, 'y':48}, {'x':46, 'y':38}, {'x':46, 'y':28}]
# 在數組array中編排好兵種組合
summonTypes = ["archer", "soldier", "archer", "soldier", "archer", "soldier", "archer", "soldier"]
def summonTroops():
# 用%取模來循環預設的征兵方案 len(self.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def commandTroops():
for i in range(len(hero.built)):
friend = hero.built[i]
if friend.type != 'palisade':
# 用friendIndex使用%來環繞防衛點
friendIndex = defendPoints[i % len(defendPoints)]
hero.command(friend, "move", friendIndex)
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
def collectionCoins():
items = hero.findItems()
item = hero.findNearest(items)
enemy = hero.findNearestEnemy(enemy)
if enemy and hero.distanceTo(enemy) < 10:
while enemy.health > 0:
if hero.isReady("bash"):
hero.bash(enemy)
else:
hero.attack(enemy)
else:
if item.type == 'coin' or item.type == 'gem':
hero.move(item.pos)
while True:
collectionCoins()
summonTroops()
commandTroops()
# 目標是生存30秒,并且保持地雷完好至少30秒。
# 有一定概率失敗
defendPoints = [{'x':75, 'y':65}, {'x':75, 'y':45}, {'x':75, 'y':25}]
def summonTroops():
if hero.gold >= hero.costOf('griffin-rider'):
hero.summon('griffin-rider')
def commandAttack():
# 命令你的獅鷲騎士攻擊食人魔。
for i in range(len(hero.built)):
friend = hero.built[i]
if friend.type != 'palisade':
# 用friendIndex使用%來環繞防衛點
friendIndex = defendPoints[i % len(defendPoints)]
hero.command(friend, "move", friendIndex)
enemy = friend.findNearestEnemy()
if enemy and enemy.type != 'fangrider':
hero.command(friend, "attack", enemy)
def pickUpCoin():
# 收集硬幣
item = hero.findNearestItem()
if item.type == 'coin' or item.type == 'gem':
hero.move(item.pos)
def heroAttack():
# 你的英雄應該攻擊對方的騎士,跨過雷區的那些。
enemies = hero.findEnemies()
enemy = hero.findNearest(enemies)
if enemy and enemy.pos.x < 38:
while enemy.health > 0:
hero.attack(enemy)
while True:
# 調用一個函數,取決于目前決定要做什么。
summonTroops()
pickUpCoin()
commandAttack()
heroAttack()
勞心勞力
# 食人魔巫師為您準備了一堆驚喜。
# 定義一個 chooseTarget 函數,讓它接受 friend 參數的輸入
def chooseStrategy(friend):
# 根據士兵的類型返回要攻擊的目標。
# 士兵應該攻擊巫師,弓箭手應該攻擊最近的敵人。
if friend.type == 'soldier':
hero.command(friend, "move", {'x':60, 'y':40})
witches = hero.findByType("witch")
witch = friend.findNearest(witches)
if witch:
hero.command(friend, "attack", witch)
else:
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
if friend.type == 'archer':
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
while True:
orges = hero.findByType("ogre")
orge = hero.findNearest(orges)
friends = hero.findFriends()
for friend in friends:
# 用你的 chooseTarget 函數決定要攻擊什么。
chooseStrategy(friend)
多少錢
# 計算花園的周長和面積
# 并為此支付合理的價格。
# 柵欄和草坪的價格。
fencePrice = 3 # 每米。
lawnPrice = 2 # 每平方米。
# 你需要工頭。
foreman = hero.findNearest(hero.findFriends())
corners = foreman.corners
# 獲取有關花園的信息。
bottomLeft = corners.bottomLeft
topRight = corners.topRight
# 計算花園的尺寸。
width = topRight.x - bottomLeft.x
height = topRight.y - bottomLeft.y
# 找到花園的周邊(米):
perimeter = (width + height) * 2
# 使用fencePrice計算圍欄成本:
fenceCost = fencePrice * perimeter
# 查找花園面積(平方米):
area = width * height
# 使用lawnPrice計算草坪成本:
lawnCost = area * lawnPrice
# 總成本是柵欄和草坪成本的總和
totalCost = fenceCost + lawnCost
hero.say("The total price is " + totalCost)
# 支付賬單。
foreman.bill(totalCost)
火山戰士
# 完成圣騎士矩陣以保護村莊。
# 該函數找到最左邊的單位。
def findMostLeft(units):
if len(units) == 0:
return None
mostLeft = units[0]
for unit in units:
if unit.pos.x < mostLeft.pos.x:
mostLeft = unit
return mostLeft
# 這個函數找到最底部的單位:
def findMostBottom(units):
if len(units) == 0:
return None
mostBottom = units[0]
for unit in units:
if unit.pos.y < mostBottom.pos.y:
mostBottom = unit
return mostBottom
paladins = hero.findByType("paladin")
# 使用findMostLeft函數查找左上角的圣騎士:
left = findMostLeft(paladins)
# 用findMostBottom函數查找右下角的圣騎士:
bottom = findMostBottom(paladins)
# 使用左上角圣騎士的X坐標:
# 以及右下角圣騎士的Y坐標:
x = left.pos.x
y = bottom.pos.y
# 移到上一步中的{X,Y}點:
hero.moveXY(x, y)
# 在火山爆發時形成護盾:
while True:
hero.shield()
矩形形態
# 在矩形隊形中形成士兵和弓箭手。
# 單位之間的距離。
step = 8
# 首先組建士兵。
sergeant = hero.findNearest(hero.findByType("soldier"))
# 左下角的坐標。
soldierX = 8
soldierY = 8
# 地層的寬度和高度。
width = sergeant.rectWidth
height = sergeant.rectHeight
for x in range(soldierX, soldierX + width + 1, 8):
for y in range(soldierY, soldierY + height + 1, 8):
hero.summon("soldier")
lastUnit = hero.built[len(hero.built)-1]
# 使用x / y變量命令最后一個內置單元:
hero.command(lastUnit, "move", {'x':x, 'y':y})
# 接下來組建弓箭手。
sniper = hero.findNearest(hero.findByType("archer"))
# 左下角的坐標。
archerX1 = 48
archerY1 = 8
# 右上角的坐標。
archerX2 = sniper.archerX2
archerY2 = sniper.archerY2
for x in range(archerX1, archerX2 + 1, 8):
for y in range(archerY1, archerY2 + 1, 8):
# 召喚一個射手。
hero.summon("archer")
# 查找最后構建的單位。
lastUnit = hero.built[len(hero.built) - 1]
# 使用x / y變量命令最后一個內置單元:
hero.command(lastUnit, "move", {'x':x, 'y':y})
pass
人神
# 形成圍繞農民的單位矩形。
# 你需要2個弓箭手和2個士兵。
# 這個函數可以幫助你。
def summonAndSend(type, x, y):
hero.summon(type)
unit = hero.built[len(hero.built)-1]
hero.command(unit, "move", {"x": x, "y": y})
# 矩形應該圍繞農民形成。
centerUnit = hero.findNearest(hero.findFriends())
# 它是矩形的中心。
center = centerUnit.pos
# 你也需要高度和寬度。
rectWidth = centerUnit.rectWidth
rectHeight = centerUnit.rectHeight
# 第一名士兵到矩形的左下角。
leftBottomX = center.x - rectWidth / 2
leftBottomY = center.y - rectHeight / 2
summonAndSend("soldier", leftBottomX, leftBottomY)
# 弓箭手在左上角。
leftTopX = center.x - rectWidth / 2
leftTopY = center.y + rectHeight / 2
summonAndSend("archer", leftTopX, leftTopY)
# 召喚并派遣一名士兵到右上角。
rightTopX = center.x + rectWidth / 2
rightTopY = center.y + rectHeight /2
summonAndSend("soldier", rightTopX, rightTopY)
# 召喚并派遣一名弓箭手到右下角。
rightBottomX = center.x + rectWidth / 2
rightBottomY = center.y - rectHeight /2
summonAndSend("archer", rightBottomX, rightBottomY)
# 現在躲起來或戰斗。
# hero.moveXY(68, 59)
while True:
enemy = hero.findNearestEnemy()
if enemy:
while enemy.health > 0:
hero.attack(enemy)
笨拙的圓
# 找到那些不在圓內的士兵。
# 所有的士兵都應該在半徑范圍內:
circleRadius = 20
# 函數檢查單元是否放置在圓上。
# 以半徑為中心的英雄。
def onCircle(unit, radius):
distance = hero.distanceTo(unit)
# 我們檢查近似值。
inaccuracy = 2
minDistance = radius - inaccuracy
maxDistance = radius + inaccuracy
return distance <= maxDistance and distance >= minDistance
while True:
soldiers = hero.findByType("soldier")
for soldier in soldiers:
# 使用循環函數查找
# 如果士兵不在圈子里:
distance = onCircle(soldier, circleRadius)
if not distance:
# 然后說出他們的名字(id)以去掉那個:
hero.say(soldier)
pass
牦牛行徑
# 計算牦牛行徑圓周的周長。
# 第一個牦牛圈。
yak1 = hero.findNearestEnemy()
# 到牦牛的距離是半徑。
radius1 = hero.distanceTo(yak1)
# 周長按以下方式計算:
circumference1 = 2 * Math.PI * radius1
# 我們來說出結果。
hero.say(circumference1)
# 移動到下一個標記。
hero.moveXY(60, 34)
# 從第二個圈子里找到牦牛。
yak2 = hero.findNearestEnemy()
# 找到第二個圓的半徑。
radius2 = hero.distanceTo(yak2)
# 計算第二圓的周長:
circumference2 = 2 * Math.PI * radius2
# 我們來說出結果。
hero.say(circumference2)
嚴峻的決心
# 你的目標是保護 Reynaldo
def getboss():
boss=None
for target in hero.findEnemies():
if target.type=='warlock':
boss = target
return boss
# 找到生命值最低的武士
def lowestHealthPaladin():
lowestHealth = 99999
lowestFriend = None
friends = hero.findFriends()
for friend in friends:
if friend.type != "paladin":
continue
if friend.health < lowestHealth and friend.health/friend.maxHealth < 0.8:
lowestHealth = friend.health
lowestFriend = friend
return lowestFriend
def commandPaladin(paladin):
# 使用函數 lowestHealthPaladin() 找到生命值最低的武士,并治療
weaker = lowestHealthPaladin()
if weaker and paladin.canCast('heal',weaker):
hero.command(paladin, 'cast',"heal", weaker)
# if paladin.health/paladin.maxHealth<0.3:
# hero.comlmand(paladin, "shield")
else:
hero.command(paladin, "attack", friend.findNearestEnemy())
# 你能使用 paladin.canCast("heal") 和 command(paladin, "cast", "heal", target)
# 武士也能防御:command(paladin, "shield")
# 不要忘了他們還能攻擊
def commandPeasant(friend):
hero.command(friend, "move", friend.findNearestItem().pos)
def commandGriffin(friend):
# boss=getboss()
# if boss:
# hero.command(friend, "attack", boss)
# else:
# hero.command(friend, "attack", friend.findNearestEnemy())
hero.command(friend, "attack", friend.findNearestEnemy())
def commandFriends():
# 指揮你的隊友
friends = hero.findFriends()
for friend in friends:
if friend.type == "peasant":
commandPeasant(friend)
elif friend.type == "griffin-rider":
commandGriffin(friend)
elif friend.type == "paladin":
commandPaladin(friend)
while True:
commandFriends()
if hero.gold>hero.costOf('griffin-rider'):
hero.summon("griffin-rider")
# 召喚 格里芬騎士
尋找開始
# Senick試圖找到難以捉摸的Burleous Majoris!
# 但他不知道Burleous Majoris會有多大......
# 找到這個樹妖人口的平均大小作為基線!
# 該函數返回數組中所有樹妖的平均大小。
def averageSize(burls):
sum = sumSize(burls)
# 記住平均值是零件總和除以量!
return sum / len(burls)
# 這個函數應該返回所有尺寸的總和。
def sumSize(burls):
# 使用burls'size'實現sum函數:
sum = 0
#hero.say(len(burls))
for i in range(len(burls)):
burl = burls[i]
sum = sum + burl.size
return sum
while True:
# 通過調用'averageSize'函數來查找樹妖的平均大小。
monsters = hero.findEnemies()
averagesize = averageSize(monsters)
# 說出看到的樹妖的平均大小!
if averagesize:
hero.say(averagesize)
pass
else:
hero.say("I do not get averagesize!")
峰會之門
花了一段時間的峰會之門代碼公布出來,其中有些地方參考了國外大神。
如有bug,歡迎反饋。
summonTypes = ['griffin-rider']
tactick = 'hold'
stage = 1
# 這里定義如何召喚士兵
def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold > hero.costOf(type):
hero.summon(type)
# 讓戰士發現最低血量的友軍
def lowestHealthPaladin():
lowestHealth = 99999
lowestFriend = None
# 遍歷循環友軍
friends = hero.findFriends()
for friend in friends:
# 找到血量小于lowestHealth,并且進行賦值操作
if friend.health < lowestHealth and friend.health < friend.maxHealth:
# 改變血量最低值
lowestHealth = friend.health
# 賦值給血量最低的友軍
lowestFriend = friend
return lowestFriend
# 命令戰士進行補血或攻擊
def commandPaladin(paladin):
if (paladin.canCast("heal")):
# 優先治療英雄
if (hero.health < hero.maxHealth * 0.8):
target = self
else:
target = lowestHealthPaladin()
if target:
hero.command(paladin, "cast", "heal", target)
elif (paladin.health < 100):
# 再治療戰士
hero.command(paladin, "shield")
elif stage < 4:
# 如果關卡小于4,讓戰士在這里進行駐守
hero.command(paladin, "move", {'x': 94, 'y': 34})
elif stage == 5:
# 如果關卡等于5,讓戰士到達這里,只進行輔助加血而不攻擊
hero.command(paladin, "move", {'x': 284, 'y': 33})
else:
# 優先攻擊warlock,如果warlock已死,攻擊其他的enemy
target = hero.findNearestEnemy()
if (warlock):
target = warlock
if (target):
hero.command(paladin, "attack", target)
# 命令士兵
def commandSoldier(soldier):
target = hero.findNearestEnemy()
if (warlock):
target = warlock
if stage == 3:
hero.command(soldier, "move", {'x': 84, 'y': 34})
elif (target):
hero.command(soldier, "attack", target)
# 控制友軍的函數
def commandFriends():
friends = hero.findFriends()
for friend in friends:
if tactick == 'hold':
hero.command(friend, "defend", {'x': 1, 'y': 40})
elif friend.type == "paladin":
commandPaladin(friend)
else:
commandSoldier(friend)
# 英雄移動的函數模塊
def moveTo(position):
if (hero.isReady("jump")):
hero.jumpTo(position)
else:
hero.move(position)
# 英雄攻擊模塊
def attack(target):
if target:
if (hero.distanceTo(target) > 10):
moveTo(target.pos)
elif hero.canCast("chain-lightning", target):
hero.cast("chain-lightning", target)
else:
hero.attack(target)
# 尋找金幣的模塊
def pickUpNearestItem():
nearestItem = hero.findNearestItem()
if nearestItem:
moveTo(nearestItem.pos)
commandFriends()
hero.moveXY(31, 56)
while True:
# 找到最近的投石車
catapult = hero.findNearest(hero.findByType('catapult'))
# 找到最近的男巫
warlock = hero.findNearest(hero.findByType('warlock'))
# 找到最近的女巫
witch = hero.findNearest(hero.findByType('witch'))
# 找到最近的敵人
target = hero.findNearestEnemy()
# 找到最近的物品
nearestItem = hero.findNearestItem()
# 記錄英雄花費的時間
now = hero.time
# 邏輯如下,依次進行
if catapult:
stage = 1
attack(catapult)
elif now < 20:
tactick = 'defend'
stage = 2
moveTo({"x": 50, "y": 33})
elif stage < 4:
if target:
stage = 3
attack(target)
else:
moveTo({"x": 172, "y": 46})
if hero.pos.x > 170:
stage = 4
elif stage < 5:
if hero.pos.x < 240:
moveTo({"x": 274, "y": 35})
tactick = 'defend'
elif nearestItem and hero.distanceTo(nearestItem) < 10:
pickUpNearestItem()
tactick = 'attack'
elif (warlock):
target = warlock
summonTroops()
attack(target)
elif target and target.type != 'gates':
attack(target)
elif nearestItem and hero.distanceTo(nearestItem) < 45:
pickUpNearestItem()
tactick = 'defend'
summonTroops()
else:
attack(target)
if hero.pos.x > 290:
stage = 5
# 如果女巫存在,優先攻擊女巫
if (witch):
attack(witch)
tactick = 'attack'
else:
summonTroops()
attack(target)
commandFriends()
冰川
繞圈
#############################################################################
#鏡像你的盟友圍繞X移動。
# 向量可以當成是x,y位置。
# Vectors 可以反映兩個位置之間的距離和方向。
# 使用Vector.subtract(vector1,vector2)來查找從vector2到vector1的方向和距離
# 使用 Vector.add(vector1, vector2) 找到你從 vector1 到 vector2 的位置
################################# 程序邏輯 ###################################
# 1. 確定中心的位置
# 2. 找到農民的位置
# 3. 得到農民到中心的向量 vector
# 4. 中心向量加上vector就是英雄應該要去的位置moveToPos
# 5. 讓英雄移動到moveToPos(注意:moveToPos是矢量,包含著坐標,后面不要加.pos)
##############################################################################
# 在 X 點的中心創建一個新的 Vector
center = Vector(40, 34)
# 一個單位的位置實際是一個 Vector!
partner = hero.findByType("peasant")[0]
while True:
# 第一,您要找到伙伴位置到 X 中心的 Vector(距離和方向)。
vector = Vector.subtract(center, partner.pos)
# 第二,找到你的英雄應該移動的位置,從中心開始,然后跟隨向量。
moveToPos = Vector.add(center, vector)
# 第三,移動到moveToPos位置
hero.move(moveToPos)
pass
滑走
# Move to the red X mark while avoiding the yaks.
# use Vector.normalize(vector1) to create a vector in the same direction as vector1, but with a distance of 1
# use Vector.multiply(vector1, X) to create a vector in the same direction as vector1, but with its distance multiplied by X
#################################################################
###########################程序思路##############################
# 1. 得到目標與英雄間的向量
# 2. 向量歸一化
# 3. 歸一向量矢量化
# 4. 檢測英雄到牦牛的距離向量
# 5. 如果距離小于10,將牦牛到英雄的向量添加到英雄行走的向量中,避開牦牛。
##################################################################
# The point you want to get to.
goalPoint = Vector(78, 34)
while True:
goal = Vector.subtract(goalPoint, hero.pos)
goal = Vector.normalize(goal)
goal = Vector.multiply(goal, 10)
yak = hero.findNearestEnemy()
distance = hero.distanceTo(yak)
if distance < 10:
yak_vector = Vector.subtract(hero.pos, yak.pos)
yak_vector = Vector.normalize(yak_vector)
yak_vector = Vector.multiply(yak_vector, 10)
goal = Vector.add(yak_vector, goal)
moveToPos = Vector.add(hero.pos, goal)
hero.move(moveToPos)
轟炸行動
轟炸行動求弧度在示意圖如下所示:
# Incoming oscars!(這是食人魔的軍事言論)。
# 你需要計算它們的攻擊的角度。
# 用這個角度來指揮你的獅鷲轟炸機!
#####################################################################
#############################程序思路################################
# 1. 找到食人魔與英雄坐標之間的差值:(enemy.pos.y - hero.pos.y, enemy.pos.x - hero.pos.x)
# 2. 求其反正切函數
# 3. 將反正切函數轉換為度數
# 4. 說出度數。
# 5. 獅鷲獲取度數,進行轟炸(系統自動運行。)
#####################################################################
while True:
enemy = hero.findNearestEnemy()
if enemy:
# 找到攻擊的向量
raw = Math.atan2(enemy.pos.y - hero.pos.y, enemy.pos.x - hero.pos.x)
# 使用三角法找出弧度中的角度!
# 答案必須是度數!
# 用 (180 / Math.PI)把弧度變換成度數
degree = raw * (180 / Math.PI)
# 說出角度!
hero.say(degree)
調劑團(練習)
# 在Omarn 10米范圍內任何地方為他投擲藥水。
# 在它降落之前,站立靠近它,抓住藥水。
# 別讓藥水掉在地上!
#########################################################################
#################################程序思路################################
# 整體思路: 讓omarn扔藥,如果有藥,則去撿藥;如果沒有,就返回到omarn身邊。
# 有藥下:
# (1)找到藥到英雄的位置向量,歸一化向量,到達藥的位置。
# (2)發現途中的地雷,并且避開地雷(關鍵)。
# 無藥下:
# (1)找到Omarn到英雄的位置向量,歸一化向量,到達Omarn的位置。
# (2)發現途中的地雷,并且避開地雷(關鍵)。
###########################################################################
def safePath(para1,para2):
goal = Vector.subtract(para1, hero.pos)
goal = Vector.normalize(goal)
goal = Vector.multiply(goal, 10)
distance = hero.distanceTo(para2)
if distance < 5:
fire_vector = Vector.subtract(hero.pos, para2.pos)
fire_vector = Vector.normalize(fire_vector)
fire_vector = Vector.multiply(fire_vector, 10)
goal = Vector.add(fire_vector, goal)
moveToPos = Vector.add(hero.pos, goal)
return moveToPos
while True:
potion = hero.findFriendlyMissiles()[0]
firetraps = hero.findHazards()
# 請記住,如果您移動距離超過3米,則會觸發火災陷阱!
omarn = hero.findByType("potion-master")[0]
if potion:
dest = potion.targetPos;
# 去拿藥水。
firetrap = hero.findNearest(firetraps)
destination = safePath(dest, firetrap)
hero.move(destination)
pass
else:
if omarn and hero.distanceTo(omarn) > 10:
# 移回Omarn
firetrap = hero.findNearest(firetraps)
destination = safePath(omarn.pos, firetrap)
hero.move(destination)
# 警告:isPathClear不適用于危險!
pass
else:
hero.say("Hup, hup!")
冰上的雪花
# 對于這個關卡,我們需要對地面進行線分形和由6條線分形組成的六角形雪花。 檢查指南以獲取所需輸出的圖像。
def degreesToRadians(degrees):
# 所有向量運算都需要用弧度來工作,而不是度。
return Math.PI / 180 * degrees
# 這個函數創建了一個線分形。閱讀它,讓你了解遞歸概念。
def line(start, end):
# 首先,我們需要得到完整的向量和它的大小來確定我們是否低于我們的最小閾值。
full = Vector.subtract(end, start)
distance = full.magnitude()
if distance < 4:
# 如果在我們的閾值距離下,我們將簡單地沿著向量畫一條線并完成(返回告訴我們退出函數)。
hero.toggleFlowers(False)
hero.moveXY(start.x, start.y)
hero.toggleFlowers(True)
hero.moveXY(end.x, end.y)
return
# 否則,我們將創建一個向量的半個數量級的分形。
half = Vector.divide(full, 2)
# 我們將創建4行分形(開始-> A,A -B,B -> A和->結束),因此我們需要計算中間位置A和B。
A = Vector.add(half, start)
# 為了得到B,我們需要將向量旋轉90度,乘以2/3(因此它是原始大小的1/3),然后將其添加到A。
rotate = Vector.rotate(half, degreesToRadians(90))
rotate = Vector.multiply(rotate, 2 / 3)
B = Vector.add(rotate, A)
# 現在使用直線函數繪制4行。
line(start, A)
line(A, B)
line(B, A)
line(A, end)
def flake(start, end):
# 要創建六角片,我們需要創建每次旋轉60度的6條線分形。
side = Vector.subtract(end, start)
a = start
b = end
for i in range(6):
line(a, b)
# 為了得到下一條邊,我們需要旋轉60度的邊。
margin = Vector.subtract(b, a)
a = Vector.add(margin, a)
rotate = Vector.rotate(margin, degreesToRadians(60))
# 現在需要重新設置A和B的開始和結束點的新的一面。
b = Vector.add(rotate, b)
whiteXs = [Vector(12, 10), Vector(60, 10)]
redXs = [Vector(64, 52), Vector(52, 52)]
# 您需要用每個函數的起始和結束向量來調用函數。
line(whiteXs[0], whiteXs[1])
# 一定要使用實際的向量對象——簡單對象不能工作。
flake(redXs[0], redXs[1])
# Refresh often to avoid a memory leak and crash (working on it)
智能推薦
巔峰極客snote
巔峰極客snote 鏈接:https://pan.baidu.com/s/1aAb4JjplV-MBj8iQCauI2Q 提取碼:977o 復制這段內容后打開百度網盤手機App,操作更方便哦 查看保護&大致邏輯 是一個選單程序,且保護全開 功能分析 1.add 2.show dword_202014為1,只能泄露一次 3.delete dword_202010只能free一次,且free之...
CodeMonkey之Python關卡詳解(二)
第 6 關 第6關:這一關,上來就有提示,讓我們做個選擇,但是不要被他迷惑。蟒蛇已經能夠幫我們解決問題了。不需要再借助長頸鹿了。 直接設置蟒蛇的長度就可以: 第 7 關 第7關:提示提示我們要先設置長頸鹿的高度,再調整蟒蛇的長度。 順序是很重的哦!一點沒錯: 將長頸鹿的高度降低,讓蟒蛇邊長: 具體設置長頸鹿的高度是多少呢?只要比蟒蛇的高度低就行了。 第 8 關 第8關:從右邊的展示區可以看出,這次...
freemarker + ItextRender 根據模板生成PDF文件
1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
猜你喜歡
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...
Linux C系統編程-線程互斥鎖(四)
互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...
統計學習方法 - 樸素貝葉斯
引入問題:一機器在良好狀態生產合格產品幾率是 90%,在故障狀態生產合格產品幾率是 30%,機器良好的概率是 75%。若一日第一件產品是合格品,那么此日機器良好的概率是多少。 貝葉斯模型 生成模型與判別模型 判別模型,即要判斷這個東西到底是哪一類,也就是要求y,那就用給定的x去預測。 生成模型,是要生成一個模型,那就是誰根據什么生成了模型,誰就是類別y,根據的內容就是x 以上述例子,判斷一個生產出...
styled-components —— React 中的 CSS 最佳實踐
https://zhuanlan.zhihu.com/p/29344146 Styled-components 是目前 React 樣式方案中最受關注的一種,它既具備了 css-in-js 的模塊化與參數化優點,又完全使用CSS的書寫習慣,不會引起額外的學習成本。本文是 styled-components 作者之一 Max Stoiber 所寫,首先總結了前端組件化樣式中的最佳實踐原則,然后在此基...