第1關:塵埃
// 使用循環直到你擊殺10個Munchkins var attacks = 0; while (attacks < 10) { // 攻擊最近的敵人! var enemy = hero.findNearestEnemy(); if (enemy) { hero.attack(enemy); } // 增量意味着增加1。 // “attacks" 變量加1 attacks += 1; } // 當你完成后,撤退到伏擊點。 hero.say("I should retreat!"); //∆ 別站在那兒瞎扯!趕緊撤回伏擊點 hero.moveXY(79, 33);
第2關:復查
// 第一點,打敗6位ogres~ // 然后收集硬幣,直到你有30金。 // 變量用來對ogres計數 var defeatedOgres = 0; // 沒打敗6位ogres,就繼續打 while (defeatedOgres < 6) { var enemy = hero.findNearestEnemy(); if (enemy) { hero.attack(enemy); defeatedOgres += 1; } else { hero.say("食人魔!"); } } // 移到地圖的右側。 hero.moveXY(49, 36); // 錢沒攢夠30塊,就繼續撿 while (hero.gold < 30) { // 尋找並收集金幣 var item = hero.findNearestItem(); if (item) { hero.moveXY(item.pos.x, item.pos.y); } // 去掉這行 say()。 } // 移動到出口。 hero.moveXY(76, 32);
第3關:山谷的風與牛
// 沙漠風暴就要來了! // 沙氂牛能夠預測到沙漠風暴來臨 // 這個變量做為循環的判斷條件 var yak = hero.findNearestEnemy(); // 檢查是否還有沙氂牛在場 while (yak) { var item = hero.findNearestItem(); if (item) { hero.moveXY(item.pos.x, item.pos.y); } // 更新變量`yak`的值 // 使用findNearestEnemy() var yak = hero.findNearestEnemy(); } // 牛沒了! // 快去撤離點:紅X hero.moveXY(38, 58);
第4關:先有付出,才有回報
// 改變while循環的條件。 // 在英雄生命值大於200滴血時,一直跑。 while (hero.health > 200) { // Δ 這行要改一下! hero.moveXY(48, 24); hero.moveXY(16, 24); } // 移動到Okar那里。 hero.moveXY(32, 40);
第5關:沙漠戰役
// while循環重復直到條件為否。 var ordersGiven = 0; while (ordersGiven < 5) { // 向下移動10米 var x = hero.pos.x; var y = hero.pos.y; y = y - 10; hero.moveXY(x, y); // 用hero.say命令你的盟友“進攻”! // 你只有站在X標志上,他們才能聽你的 hero.say("Attack!"); // 確保 ordersGiven 要加1 ordersGiven += 1; } while (true) { var enemy = hero.findNearestEnemy(); // 當你下達完命令,立即加入戰斗! if (enemy) { hero.attack(enemy); } }
第6關:誘餌和開關
// 使用誘餌引誘食人魔誘到陷阱中。 // 這個函數讓英雄持續收集金幣,直到擁有enoughGold 數量的金幣。 function collectUntil(enoughGold) { // 當While hero.gold 小於 enoughGold: while (hero.gold < enoughGold) { // 找到一個硬幣,並帶走它: var coin = hero.findNearestItem(); if (coin) { hero.moveXY(coin.pos.x, coin.pos.y); } } } // 收集25個金幣,然后在紅色標記上建造一個誘餌 collectUntil(25); hero.buildXY("decoy", 40, 52); // 最好躲起來。 hero.moveXY(20, 52); // 使用collectUntil函數收集50個金幣: collectUntil(50); // 在骨骼標記上建立一個 "decoy" : hero.buildXY("decoy", 68, 22); // 在木質標記上建立一個 "decoy": hero.buildXY("decoy", 30, 20);
第7關:海市蜃樓
// 誘使食人魔陷入伏擊! // 當你的黃金小於25時,收集金幣。 while (hero.gold < 25) { var coin = hero.findNearestItem(); hero.moveXY(coin.pos.x, coin.pos.y); } // 在while循環后,於白色X處建造一個"decoy"。 hero.buildXY("decoy", 72, 68); // 使用while 循環,當你的health等於maxHealth, 使用 `say` 來挑釁食人魔. while (hero.health == hero.maxHealth) { hero.say('還在愣着干什么,快點打我啊!'); } // 然后退回到紅色X處。 hero.moveXY(22, 16);
第8關:菠菜粉
// 收集7份菠菜葯水。 // 然后你會強大到足以擊敗食人魔。 var potionCount = 0; // 在while循環中編寫葯水收集代碼 // 檢查葯水的數量potionCount while (true) { var item = hero.findNearestItem(); if (item) { hero.moveXY(item.pos.x, item.pos.y); potionCount += 1; } if (potionCount == 7) { break; } } // 當while循環結束時, // 去戰斗吧! while (true) { var enemy = hero.findNearestEnemy(); if (enemy) { hero.attack(enemy); } }
第9關:團隊合作
// 寶石很快就會消失。 你需要幫助! // findItems()返回一個項目數組。 var items = hero.findItems(); // 從陣列中獲取第一顆寶石。 // 不要忘記第一個索引是0。 var gem0 = items[0]; // # 告訴 Bruno 拿到 gem0 hero.say("Bruno " + gem0); // 您可以引用沒有變量的寶石。 hero.say("Matilda " + items[1]); // 為最后一個寶石創建一個變量[2]: hero.say("Lisa" + items[2]); // 使用moveXY()移至該寶石的位置 hero.moveXY(items[2].pos.x, items[2].pos.y);
第10關:協助防御
// 保護農民免受食人魔的侵害。 while (true) { // 得到一個敵人的數組。 var enemies = hero.findEnemies(); // 如果數組不為空。 if (enemies.length > 0) { // 從 "enemies"數組中攻擊第一個敵人。 hero.attack(enemies[0]); // 返回到起始位置。 hero.moveXY(40, 20); } }
第11關:招募隊伍
// 一個接一個呼叫農民。 // 中立單位被檢測為敵人。 var neutrals = hero.findEnemies(); while (true) { if (neutrals.length) { // 說出say 中立數組中的第一個元素 hero.say(neutrals[0]); } else { hero.say("沒有人在這兒"); } // 使用findEnemies()重新更新中立數組 var neutrals = hero.findEnemies(); }
第12關:第二寶石
// 一顆寶石是安全的,另一顆是炸彈。 // 但你知道答案:總是選擇第二個。 while (true) { var items = hero.findItems(); // 如果物品數組元素個數大於或等於2: if (items.length >= 2) { // 移動到物品數組中的第二項 hero.moveXY(items[1].pos.x, items[1].pos.y); } // 否則: else { // 移動到中心標記。 hero.moveXY(40, 34); } }
第13關:Sarven救世主
// 一個數組(Array)就是物品的數列。 // 這個數組是一個朋友名字的數列。 var friendNames = [ 'Joan', 'Ronan', 'Nikita', 'Augustus' ]; // 數組從零開始計數,不是1! var friendIndex = 0; // 循環該數組中的每一個名字 // 使用.lenght 屬性來得到數組的長度。 while (friendIndex < friendNames.length) { // 使用方括號來獲得數組中的名字。 var friendName = friendNames[friendIndex]; // 告訴你的朋友回家。 // 使用+來連接兩個字符串。 hero.say(friendName + ', go home!'); // 增加索引來獲取數組中的下一個名字 friendIndex += 1; } // 回到綠洲,在X處建造“fence” hero.moveXY(15, 30); hero.buildXY("fence", 30, 30);
第14關:銀行突襲
// 等待食人魔,擊敗他們並收集黃金。 while (true) { var enemies = hero.findEnemies(); // enemyIndex 用於迭代數組。 var enemyIndex = 0; // 雖然enemyIndex小於enemies.length while (enemyIndex < enemies.length) { // 在enemyIndex攻擊敵人 var enemy = enemies[enemyIndex]; hero.attack(enemy); // 給 enemyIndex 加上 1。 enemyIndex++; } var coins = hero.findItems(); // coinIndex用於迭代硬幣數組。 var coinIndex = 0; while (coinIndex < coins.length) { // 用 coinIndex 從 coins 數組得到一個金幣。 var coin = coins[coinIndex]; // 收集那個金幣。 hero.moveXY(coin.pos.x, coin.pos.y); // 給 coinIndex 的值增加 1。 coinIndex++; } }
第15關:游魂
// 攻擊骷髏撿走寶石 while (true) { var enemies = hero.findEnemies(); var enemyIndex = 0; while (enemyIndex < enemies.length) { var enemy = enemies[enemyIndex]; // 敵人有血量時進攻。 while (enemy.health > 0) { hero.attack(enemy); } enemyIndex += 1; } var items = hero.findItems(); var itemIndex = 0; // 遍歷所有的物品。 while (itemIndex < items.length) { var item = items[itemIndex]; // 而物品距離大於2: while (hero.distanceTo(item) > 2) { // 試着拿到物品 hero.moveXY(item.pos.x, item.pos.y); // 別忘了讓itemIndex變大 (itemIndex+=1)或(itemIndex=itemIndex+1) itemIndex += 1; } } }