码迷,mamicode.com
首页 > 其他好文 > 详细

codecombat之Sarven沙漠13-24关及森林45/46关代码分享

时间:2015-08-30 10:04:25      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

codecombat中国游戏网址: http://www.codecombat.cn/
所有代码为javascript代码分享

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

13、诱饵钻

// 我们在测试一个新的战斗单位:诱饵。
// 创建4个诱饵,然后汇报给 Naria
var decoysBuilt = 0;
loop {
    var item = this.findNearest(this.findItems());
    // 掠夺金币!
    var x = item.pos.x ;
    var y = item.pos.y ;
    this.moveXY(x, y);
    // 每个诱饵消费25个金币,使用石英灵石。
    // 让它知道当你有超过25个金币的时候 this.gold.
    if (this.gold > 25) {
        this.buildXY("decoy", x, y);
        decoysBuilt ++ ;
    }
    // 当你一直走的时候,保持统计你创建的诱饵的数量。
    if (decoysBuilt == 4) {
        break;
    }
    // 当你创建了4个诱饵时跳出循环
    
}
this.moveXY(14, 36);
this.say("完成创建诱饵!");
// 去找 Naria 并告诉她你创建了多少个诱饵。
this.say("4");

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

14、牦牛牵引

// 保护 brandy 避免那些冲来的口渴的耗牛!
// 收集金币来建造诱饵干扰耗牛。
// 使用旗子来决定什么时候在哪里建造诱饵。
loop {
    var flag = this.findFlag();
    var item = this.findNearest(this.findItems());
    if (flag) {
        if (this.gold >= 25) {
            var x = flag.pos.x ;
            var y = flag.pos.y ;
            this.buildXY("decoy", x, y);
        }
        this.pickUpFlag(flag);
    }
    if (item) {
        var goldx = item.pos.x ;
        var goldy = item.pos.y ;
        this.moveXY(goldx, goldy);
    }
    
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

15、沙漠战役

// while循环重复直到条件为假。
// 在while 循环内部执行动作,否认则它永远不能执行。
var ordersGiven = 0;
while (ordersGiven < 5) {
    // 在站场上移动和排列你的盟友。 (如果你是直接在他们面前,他们只能听到你的。
    var x = this.pos.x;
    var y = this.pos.y - 9;
    this.moveXY(x, y);
    this.say("Attack!");
    ordersGiven ++ ;
}
// 当你找到规律,加入攻击。
loop {
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
         if (this.isReady("bash")) {
            this.bash(enemy);
        }
        else {
            this.attack(enemy);
        }       
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

16、尘埃

// 总是采取行动在一个while循环,否则会无限!
// 使用循环直到你有足够的击杀10个芒奇金人 
var hits = 0;
this.moveXY(58, 30);
while (hits < 10) {
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
        this.attack(enemy);
        hits ++ ;
    }
}
// 当你完成后,撤退到伏击点。
this.moveXY(79, 33);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

17、Sarven 拯救

// 从强盗中救出农民并送她回到村。
// 选择适合你的路线,避开巡逻或直面迎接他们。
// 药水会有随机效果–有好的,也有不好的。
// 觉得自己很勇敢?如果你可以得到食人魔的财宝的话,就会得到奖励。
loop {
    var flag = this.findFlag();
    var enemy = this.findNearest(this.findEnemies());
    if (flag) {
        this.pickUpFlag(flag);
    }
    if (enemy) {
        if (this.isReady("bash")) {
            this.bash(enemy);
        }
        if (this.isReady("cleave")) {
            this.cleave(enemy);
        }
        else {
            this.attack(enemy);
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

18、神圣的雕像
同17关Sarven 拯救代码

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

19、蜃景制造者

// 当你的金币小于25个的时候,收集金币。
// 然后建造诱饵来引诱食人魔离开路线。
// 当你的生命值满了,冲着小食人魔喊叫侮辱他们,引诱他们。
// 然后退回到你的基地伏击他们。
    while (this.gold < 25) {
        var mygold = this.findNearest(this.findItems()).pos;
        var x = mygold.x ;
        var y = mygold.y ;
        this.moveXY(x, y);
    }
    this.buildXY("decoy", 72, 69);
loop {
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
    }
    if (this.health == this.maxHealth) {
        this.say("Come on baby !");
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

20、Sarven 救世主

// 一个数组就是物品的数列。
// 这个数组是一个朋友名字的数列。
var friendNames = [‘Joan‘, ‘Ronan‘, ‘Nikita‘, ‘Augustus‘];
// 数组从零开始计数,不是1!
var friendIndex = 0;
// 循环该数组中的每一个名字
// 使用.lenght 属性来得到数组的长度。
while (friendIndex < friendNames.length) {
    // 使用方括号来获得数组中的名字。
    var friendName = friendNames[friendIndex];
    // 告诉你的朋友回家。
    // 使用+来连接两个字符串。
    this.say(friendName + ‘, go home!‘);
    friendIndex ++ ;
    // 增加索引来获取数组中的下一个名字
}
this.moveXY(22, 31);
this.buildXY("fence", 29, 30);
// 回去建造栅栏让食人魔远离。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

21、奇数沙尘暴

// 这个数组包含朋友和食人魔。
// 偶数元素是食人魔,奇数元素是伙伴。
var everybody = [‘Yetu‘, ‘Tabitha‘, ‘Rasha‘, ‘Max‘, ‘Yazul‘,  ‘Todd‘];
var enemyIndex = 0;
while (enemyIndex < everybody.length) {
    // 使用方括号把食人魔的名字从数组中获取出来
    var enemyname = everybody[enemyIndex] ;
    // 使用变量传入食人魔的名字,攻击它们。
    this.attack(enemyname);
    // 每次递增2,来跳过朋友。
    enemyIndex += 2;
}
this.moveXY(36, 53);
// 在击败食人魔之后,向绿洲移动。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

22、潜伏

// 用findEnemies把敌人存在数组enemies中
// 只攻击萨满巫师,不要攻击牦牛!

var enemies = this.findEnemies();
var enemyIndex = 0;
// 把这段代码用一个while loop 功能循环遍历所有的敌人
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if (enemy.type == ‘shaman‘) {
        while (enemy.health > 0) {
            if (this.isReady("bash")) {
                this.bash(enemy);
            }
            this.attack(enemy);
        }
    }
    enemyIndex ++ ;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

23、优待

// 首先,在所有的敌人中循环...
var enemies = this.findEnemies();
var enemyIndex = 0;
// ...但是仅攻击 ‘thrower‘ 类型的敌人。
// 别忘了使用你的特殊能力!
while (enemyIndex < enemies.length) {
    if (enemies[enemyIndex].type == "thrower") {
        if (this.isReady("cleave")) {
            this.cleave(enemies[enemyIndex]);
        }
        else {
            this.attack(enemies[enemyIndex]);
        }
    }
    enemyIndex ++ ;
}
// 然后再到所有的敌人中循环...
enemies = this.findEnemies();
enemyIndex = 0;
// ...干掉仍然活着的每个
while (enemyIndex < enemies.length) {
    if (this.isReady("cleave")) {
        this.cleave(enemies[enemyIndex]);
    }
    else {
        this.attack(enemies[enemyIndex]);
    }
    enemyIndex ++ ;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

24、Sarven 牧羊人

// 使用 while 循环来对付食人魔。

loop {
    var enemies = this.findEnemies();
    var enemyIndex = 0;
    while (enemyIndex < enemies.length) {
        // 将攻击逻辑放到 while 循环里来攻击所有的敌人。
        // Find the array‘s length with: enemies.length
        var enemy = enemies[enemyIndex];
        // "!=" 意思是 "不等于"
        if (enemy.type != "sand-yak") {
            // 当敌人的健康值大于0,攻击它!
            if (enemy.health > 0) {
                if (this.isReady("cleave")) {
                    this.cleave(enemy);
                }
                else if (this.isReady("bash")) {
                    this.bash(enemy);
                }
                else {
                    this.attack(enemy);
                }
            }
        }
        enemyIndex ++ ;
    }
    // 在两波敌人之间,移动回中央。
    this.moveXY(40, 32);
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

番外篇:森林45关-野马

loop {
    // 你怎么寻找最近的友好单位?
    // 马=?
    var horse = this.findNearest(this.findFriends());
    var flag = this.findFlag();
    if (horse ) {
        var x1 = horse.pos.x - 7;
        var x2 = horse.pos.x + 7;
        if (x1 >= 1) {
            // 移动到马的y坐标,但使用x1作为x坐标。
            this.moveXY(x1, horse.pos.y);
        } else if (x2 <= 79) {
            // 移动到马的y坐标,但使用x2作为x坐标。
            this.moveXY(x2, horse.pos.y);
        }
        var distance = this.distanceTo(horse);
        if (distance <= 10) {
            this.say("Whoa");
            // 移到到红色的x来使马返回农场。 
            // 移回牧场开始寻找下一匹马。
            if (flag) {
                this.pickUpFlag(flag);
            }
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

番外篇:森林46关-天启

//使用增加速度的装备
// 炮火的天启在接近我们!
// 在60秒内躲避炮弹。
// 提示:旗子可能派上用场,比如Coinucopia这关。
// 因为攻击是每次提交时随机的,所以你不能使用moveXY这个指令。
loop {
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
this.shield();
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

codecombat之Sarven沙漠13-24关及森林45/46关代码分享

标签:

原文地址:http://my.oschina.net/comA/blog/499145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!