codecombat计算机科学三,Codecombat 游戏攻略——JavaScript编辑语言——关卡(计算机科学四)Ⅰ...

第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;

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值