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

codecombat安息之云山峰11-21关及沙漠38关代码分享

时间:2015-09-07 16:57:09      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

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

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

11、零和

// 在两分钟内击败敌方英雄。
loop {
    var enemies = this.findEnemies();
    var nearestEnemy = this.findNearest(enemies);
    var item = this.findNearest(this.findItems());
    // 你的英雄能收集金币,召唤部队。
    if (this.isReady("goldstorm")) {
        this.cast("goldstorm");
    }
    if (this.gold > this.costOf("soldier")) {
        this.summon("soldier");
    }
    // 在站场上,她也可以命令你的盟友。
    var friends = this.findFriends();
    for (var friendIndex = 0; friendIndex < friends.length; ++friendIndex) {
        var friend = friends[friendIndex];
        this.command(friend, "attack", friend.findNearest(enemies));
    }
    // 使用你的英雄的能力力挽狂澜。
    if (nearestEnemy && this.distanceTo(nearestEnemy) < 10) {
        if (this.isReady("mana-blast")) {
            this.manaBlast();
        }
        if (this.isReady("reset-cooldown")) {
            this.resetCooldown("mana-blast") ;
        }
        this.attack(nearestEnemy);
    }
    else {
        this.move(item.pos);
    }
}

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

12、动物园管理员

// 保护笼子。
// 放一个士兵在每一个 X 的位置
var 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金币。
loop {
    var item = this.findNearest(this.findItems());
    this.move(item.pos);
    if (this.gold > 80) {
        break ;
    }
}
// 2.建造4个士兵。
for(var i=0; i < 4; i++) {
    this.summon("soldier");
}
// 3.派你的士兵到特定的位置上。
loop {
    var friends = this.findFriends();
    for(var j=0; j < friends.length; j++) {
        var point = points[j];
        var friend = friends[j];
        var enemy = friend.findNearestEnemy();
        if(enemy && enemy.team == "ogres" && friend.distanceTo(enemy) < 5) {
            // 命令友方攻击。
        this.command(friend, "attack", enemy);    
        } else {
            // 命令的朋友移动到特定点上。
        this.command(friend, "move", point);    
        }
    }
}

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

13、木材的叛徒

//换个好法杖
loop {
    // 收集金子
    if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    var item = this.findNearest(this.findItems());
    if (item) {
       this.move(item.pos); 
    }
    // 如果你有足够的金币,召唤一个士兵。
    if (this.gold > this.costOf("soldier")) {
        this.summon("soldier");
    }
    // 使用 for 循环来命令每个士兵。
    var friends = this.findFriends();
    for(var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        if(friend.type == "soldier") {
            var enemy = friend.findNearestEnemy();
            // 如果这有一个敌人,命令她攻击。
            if (enemy && friend.health >= friend.maxHealth/2 && friend.team != "ogres"){
                this.command(friend, "attack", enemy);
            }
            else {
                if (friend.team != "ogres") {
                    this.command(friend, "move", {x:71,y:48});
                }
                  
            }
            // Careful! If your soldiers die, a warlock will appear!
            // 否则的话,移动她到地图的右边。
            if (friend.health < friend.maxHealth/2) {
                this.command(friend, "move", {x:61, y:48});
            }

        }
         if (this.canCast("regen")) {
                    this.cast("regen", friend);
                }
    }
}


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

14、崇高牺牲

// 收集80金币
loop {
    var item = this.findNearest(this.findItems());
    if (item) {
        this.move(item.pos);
    }
    if (this.gold > 80) {
        break ;
    }
}
// 建造4个士兵用来做诱饵
for (var i = 0; i < 4; i++) {
    this.summon("soldier");
}
// 派你的士兵到指定位置。
var 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 };
var friends = this.findFriends();
// 使用 for 循环来在0~4个士兵中循环
// 让伙伴们比赛到指定的点上,命令他们移动
for (var index = 0; index < friends.length; index ++) {
    var f = friends[index];
    this.command(f, "move", points[index]);
}


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

15、狩猎派对

// 命令你的部队向东移动,攻击任何看到的食人魔。
// 使用 for 循环和 findFriends方法。
// 你能在你的士兵上使用findNearestEnemy()来获取他们的而不是你的最近的敌人。
loop {
    var friends = this.findFriends();
    for (var index = 0 ; index < friends.length ; index ++ ) {
        var friend = friends[index] ;
        var enemy = friend.findNearestEnemy();
        if (enemy && friend.health > friend.maxHealth / 2) {
            this.command(friend, "attack", enemy);
        }
        else if (friend.health <= friend.maxHealth / 2) {
                this.command(friend, "move", {x:friend.pos.x - 1, y:friend.pos.y});         
        }
        else {
            this.command(friend, "move", {x:friend.pos.x + 1, y:friend.pos.y});
        }   
    }    
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

16、借刀

// 你的英雄不需要在本关参与战斗。
// 命令你的弓箭手集中在火攻敌人。
loop {
    var enemy = this.findNearest(this.findEnemies());
    var friends = this.findFriends();
    for(var index = 0; index < friends.length; index ++ ){
        var f = friends[index];
        if (enemy && this.now() > 5) {
            this.command(f, "attack", enemy);
        }
        else {
            this.command(f, "move", f.pos);
        }
    }        
}

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

17、重要的权利

// 这关会教你怎么定义你自己的函数。
// 放在函数内的代码并不会立刻执行, 而是先保存好, 以备后用.
// 这个函数会让你的英雄收集最近的金币。
this.pickUpNearestCoin = function() {
    var items = this.findItems();
    var nearestCoin = this.findNearest(items);
    if(nearestCoin) {
        this.move(nearestCoin.pos);
    }
};

// 这个函数会让你的英雄召唤一个士兵。
this.summonSoldier = function() {
    // 在这里写下代码:当你有足够金币时召唤士兵
    if (this.gold > this.costOf("soldier")) {
        this.summon("soldier");
    }
};

// 这个函数会命令你的士兵攻击最近的敌人
this.commandSoldiers = function() {
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        var enemy = friends[i].findNearestEnemy();
        if(enemy) {
            this.command(friends[i],"attack", enemy);
        }
    }
};

loop {
    // 在你的循环里,你可以"调用"你在上面定义的函数
    // 下面几行代码会让 "pickUpNearestCoin" 函数里的代码被执行。
    this.pickUpNearestCoin();
    // 在这里调用 summonSoldier
    // 在这里调用 commandSoldiers
    this.summonSoldier();
    this.commandSoldiers(); 
}

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

18、保护和服务

// Protect the workers and animals!
// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };
var soldiers = [];
var friends = this.findFriends();
for(var i=0; i < friends.length; i++) {
    var friend = friends[i];
    if(friend.type == "soldier") {
        soldiers.push(friend);
    } else {
        // Defend the workers:
        defend.push(friend);
    }
}

loop {
    // Use a for-loop to assign each soldier to a corresponding defend[] target
    // Use command(soldier, "defend", thang) or command(soldier, "defend", position)
    for(var y = 0; y < soldiers.length; y ++) {
        var f = soldiers[y];
        this.command(f, "defend", defend[y]);
    }

}

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

19、The Bane of Soldiers

// Robobombs explode when they die or touch an enemy.
// Split up your soldiers so that they don‘t all get exploded together.

loop {
    var friends = this.findFriends();
    for(var index = 0; index < friends.length; index++) {
        var friend = friends[index];
        // Use the index to decide where to command each soldier to move.
         if (friend && index == "0") {
            this.command(friend, "move", {x:58,y:46}); 
        }
         else {
        this.command(friend, "move", {x:friend.pos.x-1,y:friend.pos.y}); 
         }           

    }
}

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

20、Ring Bearer

// You must escort a powerful magical ring back to town to be studied.
// The goal is to escape, not fight. More ogres lurk in the surrounding mountains!
// Make a circle of soldiers around the peasant!
// We give you two functions to help with this:

// findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
// The first argument ‘soldiers‘ should be an array of your soldiers.
// The second argument ‘i‘ is the index of the soldier (in soldiers) you want to find the position for.
function findSoldierOffset(soldiers, i) {
    var soldier = soldiers[i];
    var angle = i * 360 / soldiers.length;
    return radialToCartesian(5, angle);
}
// This function does the math to determine the offset a soldier should stand at.
function radialToCartesian(radius, degrees) {
    var radians = Math.PI / 180 * degrees;
    var xOffset = radius * Math.cos(radians);
    var yOffset = radius * Math.sin(radians);
    return {x: xOffset, y: yOffset};
}
var peasant = this.findByType("peasant")[0];
// Use findByType to get an array of your soldiers.
loop {
    // Use a for-loop to iterate over your array of soldiers
    var f = this.findFriends();
    for(var index = 0; index < f.length; index ++){
        var friend = f[index];
        if (friend.type == "soldier") {
        var offset = findSoldierOffset(f,index);
        moveTo = {x: peasant.pos.x + offset.x,y:peasant.pos.y + offset.y};
            this.command(friend, "move", moveTo);           
        }
    }
    // Find the offset for a soldier.
    // Add the offset.x and offset.y to the peasant‘s pos.x and pos.y.
    // Command the soldier to move to the new offset position.

    // The hero should keep pace with the peasant!
    this.move({x: this.pos.x + 0.2, y: this.pos.y});
}

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

21、双生花

// 如果花匠受伤了,双生花会缩小!
this.summonSoldiers = function() {
    if (this.gold >= this.costOf("soldier")) {
        this.summon("soldier");
    }
};
// 定义函数:commandSoldiers
this.commandSoldiers = function() {
    var friends = this.findFriends();
    for(var index = 0; index < friends.length; index ++) {
         var friend = friends[index];
         var enemy = friend.findNearestEnemy();
         if (enemy && friend.type == "soldier") {          
            this.command(friend, "attack", enemy);
        }       
    }

};
// 定义函数:pickUpNearestCoin
this.pickUpNearsetCoin = function() {
    var item = this.findNearest(this.findItems());
    if (item) {
        this.move(item.pos);
    }
};
var peasant = this.findByType("peasant")[0];
loop {
    this.summonSoldiers();
    // this.commandSoldiers();
    // this.pickUpNearestCoin();
    this.commandSoldiers();
    this.pickUpNearsetCoin();
}

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

番外篇:沙漠38关-Goalkeeper

//多试几次就有好运气啦
// Command the peasants to prevent the ogres from scoring.
// The fireball is type "ball".
    var friends = this.findFriends();
    var i = 0;var j = -1;    
loop {
        if (i%10 === 0) {
          j = -j;
        }
        this.command(friends[1], "move", {x:17,y:friends[1].pos.y+j});
        this.command(friends[0], "move", {x:17,y:friends[0].pos.y-j});
        i++;
}

codecombat安息之云山峰11-21关及沙漠38关代码分享

标签:

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

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