codecombat安息之云山峰22-31关代码分享

来源:互联网 发布:知行结合爱国 编辑:程序博客网 时间:2024/04/19 23:12
codecombat中国游戏网址:http://www.codecombat.cn/
所有代码为javascript代码分享

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

22、躁动的死亡

// 这个关卡应该是非常难的!你也许需要一个很棒的战略与或装置去完成它!
// 找到然后杀死雪人,为了仪式去收集他的血液。
// 你也许想收集雪人遗留下的金币,你需要他们去召唤一只军队。
// 站在召唤石旁(红色X),开始召唤。
// 
this.flags = function(){
    var flagg = this.findFlag("green");
    var flagb = this.findFlag("black");
    var flagv = this.findFlag("violet");
    if (flagg) {
        this.pickUpFlag(flagg);
    }
    if (flagb) {
        this.buildXY("fire-trap", flagb.pos.x,flagb.pos.y);
        this.pickUpFlag(flagb);
    }
    if (flagv) {
        this.pickUpFlag(flagv);
        this.summ();
    }
};
this.coins = function(){
    var item = this.findNearest(this.findItems());
    if (item) {
        this.move(item.pos);
    }
};
this.summ = function(){
    while(this.gold > this.costOf("soldier")) {
        this.summon("soldier");
    }
};
this.comm = function(){
    var soldiers = this.findFriends();
    if (soldiers) {
        for(var i = 0; i < soldiers.length; i++){
            var friend = soldiers[i];
            var enemy = friend.findNearestEnemy();
            if (enemy) {
            this.command(friend, "attack", enemy);    
            }
            
        }   
    }
    
};
loop {
    this.flags();
    this.coins();
    this.comm();
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
        if (this.isReady("bash")) {
            this.bash(enemy);
        }
        else {
            this.shield();            
        }
    }
}

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

23、The Geometry of Flowers

// You now have the Ring of Flowers! You can do:
// toggleFlowers(true/false) - turns flowers on or off.
// setFlowerColor("random") - can also be "pink", "red", "blue", "purple", "yellow", or "white".
// Here are some functions for drawing shapes:
// x, y - center of the shape
// size - size of the shape (radius, side length)
this.drawCircle = function(x, y, size) {
    var angle = 0;
    this.toggleFlowers(false);
    while (angle <= Math.PI * 2) {
        var newX = x + (size * Math.cos(angle));
        var newY = y + (size * Math.sin(angle));
        this.moveXY(newX, newY);
        this.toggleFlowers(true);
        angle += 0.2;
    }
};
this.drawSquare = function(x, y, size) {
    this.toggleFlowers(false);
    cornerOffset = size / 2;
    this.moveXY(x - cornerOffset, y - cornerOffset);
    this.toggleFlowers(true);
    this.moveXY(x + cornerOffset, y - cornerOffset);
    this.moveXY(x + cornerOffset, y + cornerOffset);
    this.moveXY(x - cornerOffset, y + cornerOffset);
    this.moveXY(x - cornerOffset, y - cornerOffset);
};

var redX = {x: 28, y: 36};
var whiteX = {x: 44, y: 36};

// Pick a color.
this.setFlowerColor("red");
// Draw a size 10 circle at the redX.
this.drawCircle(redX.x,redX.y,10);
// Change the color!
this.setFlowerColor("white");
// Draw a size 10 square at the whiteX.
this.drawSquare(whiteX.x,whiteX.y,10);
// Now experiment with drawing whatever you want!

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

24、Mountain Flower Grove

// This level is a place for making flower art.
// The real goal is to experiment and have fun!
// If you draw something with at least 1000 flowers, you will "succeed" at the level.
this.drawCircle = function(x, y, size) {
    var angle = 0;
    this.toggleFlowers(false);
    while (angle <= Math.PI * 2) {
        var newX = x + (size * Math.cos(angle));
        var newY = y + (size * Math.sin(angle));
        this.moveXY(newX, newY);
        this.toggleFlowers(true);
        angle += 0.2;
    }
};
this.drawSquare = function(x, y, size) {
    this.toggleFlowers(false);
    cornerOffset = size / 2;
    this.moveXY(x - cornerOffset, y - cornerOffset);
    this.toggleFlowers(true);
    this.moveXY(x + cornerOffset, y - cornerOffset);
    this.moveXY(x + cornerOffset, y + cornerOffset);
    this.moveXY(x - cornerOffset, y + cornerOffset);
    this.moveXY(x - cornerOffset, y - cornerOffset);
};
var r = 10;
var X = [];
X[1] = {x: 30, y: 110};
X[2] = {x: 70, y: 110};
X[3] = {x: 100, y: 110};
X[4] = {x: 130, y: 110};
X[5] = {x: 30, y: 70};
X[6] = {x: 70, y: 70};
X[7] = {x: 100, y: 70};
X[8] = {x: 130, y: 70};
X[9] = {x: 30, y: 30};
X[10] = {x: 70, y: 30};
X[10] = {x: 100, y: 30};
X[10] = {x: 130, y: 30};
for(var i = 1; i <= X.length; i++){
    var m = X[i];
    this.drawCircle(m.x,m.y,r);
    this.drawSquare(m.x,m.y,r);
}

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

25、What in Carnation

var twoPi = 2 * Math.PI;
// Here are some functions for drawing shapes:
this.degreesToRadians = function(degrees) {
    return (degrees/360)*twoPi;
};
this.drawPolyStars = function(center, radius, sides, skips, startAngle) {
    this.toggleFlowers(false);
    var angle = startAngle;
    var x = center.x;
    var y = center.y;
    var hoops = skips + 1;
    var stepAngle = hoops * (twoPi / sides);
    if(skips !== 0 && (sides % hoops) === 0) {
        hoops = skips;
    }
    var endAngle = (twoPi * hoops) + startAngle;
    while(angle <= endAngle) {
        var newX = x + radius * Math.cos(angle);
        var newY = y + radius * Math.sin(angle);
        this.moveXY(newX, newY);
        this.toggleFlowers(true);
        angle += stepAngle;
    }
};
this.drawStar = function(center, radius, sides, skips, startAngle) {
    var skipsPlusOne = skips + 1;
    if ((sides/skipsPlusOne) != 1 && (sides%skipsPlusOne) === 0) {
        var index = skips;
        while(index >= 0) {
            var angle = startAngle + index * (twoPi / sides);
            this.drawPolyStars(center, radius, sides, skips, angle);
            index -= 1;
        }
    } else {
        this.drawPolyStars(center, radius, sides, skips, startAngle);
    }
};
this.drawPolygon = function(center, radius, sides, startAngle) {
    this.drawPolyStars(center, radius, sides, 0, startAngle);
};
this.drawSpokes = function(center, radius, sides, startAngle) {
    var x = center.x;
    var y = center.y;
    var endAngle = twoPi + startAngle;
    var stepAngle = twoPi / sides;
    var angle = startAngle;
    while(angle < endAngle) {
        var newX = x + radius * Math.cos(angle);
        var newY = y + radius * Math.sin(angle);
        if((this.pos.x|0) == (x|0) && (this.pos.y|0) == (y|0)) {
            this.toggleFlowers(true);
            this.moveXY(newX, newY);
        } else {
            this.toggleFlowers(false);
            this.moveXY(newX, newY);
            this.toggleFlowers(true);
            this.moveXY(x, y);
        }
        this.toggleFlowers(false);
        angle += stepAngle;
    }
};
this.drawSpiral = function(center, size, loopNum, startAngle) {
    var newX, x = center.x;
    var newY, y = center.y;
    this.toggleFlowers(false);
    this.moveXY(x, y);
    this.toggleFlowers(true);
    var steps = size * 2;
    var direction = Math.sign(loopNum);
    var stepAngle = twoPi / steps / direction;
    var loops = direction * loopNum;
    var stepSize = size / steps / loops;
    var curSize = 0;
    var angle = startAngle;
    var endAngle = (twoPi * loopNum) + startAngle;
    while(loopNum<0 ? (angle>=endAngle) : (angle<=endAngle)) {
        newX = x + curSize * Math.cos(angle);
        newY = y + curSize * Math.sin(angle);
        this.moveXY(newX, newY);
        angle += stepAngle;
        curSize += stepSize;
    }
    newX = x + size * Math.cos(endAngle);
    newY = y + size * Math.sin(endAngle);
    this.moveXY(newX, newY);
};
redX = {x: 28, y: 36};
whiteX = {x: 60, y: 36};
cent ={x: 60,y: 42};
//setFlowerColor
this.setFlowerColor("white");
// Draw a "3D style" box, using the drawPolygon and drawSpokes functions, centered on the red X and with a size of 10.
// The simplest startAngles to calculate would be either "up" or "down".
// The drawing functions deal with angles in terms of radians. If you think in terms of degrees then please use the "degreesToRadians(degrees)" function so they can understand you.
this.drawPolygon(redX, 10, 6, this.degreesToRadians(30));
this.drawSpokes(redX, 10, 3, this.degreesToRadians(30));
// Draw the star bib, using the drawStar and drawSpiral functions. (See the Guide for an image of the shapes.)
// The star is centered on the white X and has a size of 6.
// The spirals have a size of 15. To get a spiral to go the other direction give it a negative number of loops.
//setFlowerColor
this.setFlowerColor("yellow");
this.drawStar(whiteX, 6, 7, 2, this.degreesToRadians(90));
//setFlowerColor
this.setFlowerColor("purple");
this.drawSpiral(cent, 15, -1, this.degreesToRadians(270));
this.drawSpiral(cent, 15, 1, this.degreesToRadians(270));
//drawSpiral(center, size,  number of loops, start angle)
this.toggleFlowers(false);
this.moveXY(42, 37);


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

26、猎手和猎物

// Ogres are trying to kill your reindeer!
// Keep your archers back while summoning soldiers to attack.
this.pickUpCoin = function() {
    // Collect coins.
    var item = this.findNearest(this.findItems());
    if (item) {
        this.move(item.pos);    
    }   
};
this.summonTroops = function() {
    // Summon soldiers if you have the gold.
    if (this.gold > this.costOf("soldier")) {
        this.summon("soldier");
    }
};

// This function has an argument named soldier.
// Arguments are like variables.
// The value of an argument is determined when the function is called.
this.commandSoldier = function(soldier) {
    // Soldiers should attack enemies.
        var friend = soldier;
        var enemy = friend.findNearestEnemy();
        if (friend.type == "soldier") {
            if (enemy) {
                this.command(friend, "attack", enemy);
            }
            if (friend.health < friend.maxHealth/3) {
                if (this.canCast("regen")) {
                    this.cast("regen", friend);
                }
            }
        }
};

// Write a commandArcher function to tell your archers what to do!
// It should take one argument that will represent the archer passed to the function when it's called.
// Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
this.commandArcher = function(archer) {
    var enemy = archer.findNearestEnemy();
    if (enemy) {
        if (archer.distanceTo(enemy) < 25) {
            this.command(archer, "attack", enemy);
        } 
    }
    if (friend.health < friend.maxHealth/3) {
        if (this.canCast("regen")) {
            this.cast("regen", friend);
        }
    }
};
loop {
    this.pickUpCoin();
    this.summonTroops();
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        if(friend.type == "soldier") {
            // This friend will be assigned to the variable soldier in commandSoldier
            this.commandSoldier(friend);
        } else if(friend.type == "archer") {
            this.commandArcher(friend);
        }
    }
}

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

27、Library Tactician

// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
// Soldiers spread out in a circle and defend.
this.commandSoldier = function(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {x: 41, y: 40};
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    this.command(soldier, "defend", defendPos);
};
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
this.findStrongestTarget = function() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = this.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i = 0; i < enemies.length; i++ ){
        var enemy = enemies[i];
        if (enemy.health > mostHealth) {
            mostHealth = enemy.health;
            bestTarget = enemy;
        }
    }
    // Only focus archers' fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
};
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
this.commandArcher = function(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        this.command(archer, "attack", archerTarget);
    } else if(nearest) {
        this.command(archer, "attack", nearest);
    }
};
var archerTarget = null;
loop {
    // If archerTarget is dead or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = this.findStrongestTarget();
    }
    var friends = this.findFriends();
    var soldiers = this.findByType("soldier");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        this.commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    var archers = this.findByType("archer");
    for(var j = 0; j < archers.length; j++){
        var archer = archers[j];
        this.commandArcher(archer);
    }
}

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

28、Misty Island Mine

// Your goal is to collect gold efficiently.
// Your peasants should build decoys to defend themselves.
// Find the best item to collect, make sure two friends aren't going after the same item.
// friend is a peasant
// excludeItems is an array of items that friend should ignore
this.findBestItem = function(friend, excludeItems) {
    var items = friend.findItems();
    var bestItem = null;
    var bestItemValue = 999999;
    for(var i=0; i < items.length; i++) {
        var item = items[i];
        // indexOf() returns the index of the item in the array, or -1 if item isn't in the array
        var idx = excludeItems.indexOf(item.id);
        // Another peasant is already targeting this item
        // The continue statement skips to the beginning of the next iteration of the for-loop
        if(idx > -1) { continue; }
        else{
            if (friend.distanceTo(item)/item.value < bestItemValue ) {
                bestItemValue = friend.distanceTo(item)/item.value;
                bestItem = item;
            }
        }
        // Find the best item: value divided by distance
    }
    // Make sure to return the bestItem!
    if (bestItem) {
        return bestItem;
    }
    else {
        return null;
    }
};
loop {
    var peasants = this.findByType("peasant");
    var claimedItems = [];
    for(var i=0; i < peasants.length; i++) {
        var peasant = peasants[i];
        var nearest = peasant.findNearestEnemy();
        if(nearest && peasant.distanceTo(nearest) <= 10){
            if (peasant.gold > peasant.costOf("decoy")) {                this.command(peasant, "buildXY", "decoy",peasant.pos.x - 2,peasant.pos.y);
            }
        }
        else {
         var item = peasant.findItems();
         claimedItems.push(item.id);
         var coin = this.findBestItem(peasant,claimedItems);
         if (coin) {
         this.command(peasant, "move", {x:coin.pos.x,y:coin.pos.y});    
         }   
        }

    }
}        
        // Peasants have gold and costOf() properties, and obey the "buildXY" command.
        // If there's an enemy within 10 meters targeting this peasant, build a "decoy" if we can afford one.
        // You can get the enemy's target with nearest.target
        // Otherwise, use findBestItem to target the best coin to collect
        // Use claimedItems.push(item.id) to add it to the claimedItems array.

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

29、Steelclaw Gap

// This level introduces the % operator, also known as the modulo operator.
// a % b returns the remainder of a divided by b
// This can be used to wrap around to the beginning of an array when an index might be greater than the length
var defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}];
var summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"];
// You start with 360 gold to build a mixture of soldiers and archers.
// this.built is an array of the troops you have built, alive or dead
// Here we use "this.built.length % summonTypes.length" to wrap around the summonTypes array
this.summonTroops = function() {
    var type = summonTypes[this.built.length % summonTypes.length];
    if(this.gold >= this.costOf(type)) {
        this.summon(type);
    }
};

this.commandTroops = function() {
    var friends = this.findFriends();
    for(var friendIndex=0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        // Use % to wrap around defendPoints based on friendIndex
        var dis = friendIndex % defendPoints.length;
        // Command your minion to defend the defendPoint
        this.command(friend, "move", defendPoints[dis]);
        var enemy = friend.findNearestEnemy();
        if (enemy) {
            this.command(friend, "attack", enemy);
        }
    }
};

loop {
    this.summonTroops();
    this.commandTroops();
}

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

30、讨厌的牦牛

// 一大群牦牛
// 如果生存,你需要过滤掉牦牛
this.removeByType = function(array, excludedType) {
    var tempArray = [];
    for(var i = 0; i < array.length; i++) {
        // 经过并检查每一个敌人是否excludedtype
        // 如果不是,把它“放入”数组
        var enemye = array[i];
         if (enemye.type != excludedType) {
                tempArray.push(enemye);
         }
    }
    return tempArray;
};

loop {
    // 找到敌人
    var enemies = this.findEnemies();
    // 清除那些讨厌的牦牛
    enemies = this.removeByType(enemies, "sand-yak");
    var enemy = this.findNearest(enemies);
    if (this.gold > this.costOf("soldier")) {
        this.summon("soldier");
    }
    // 现在...“清除”那些敌人
    var  friends = this.findFriends();
    if(enemy) {
        if (friends) {
          for(var i = 0; i < friends.length; i++){
            var friend = friends[i];
            this.command(friend, "attack", enemy);
          }
        }     
        this.attack(enemy);
    }
}

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

31、Mixed Unit Tactics

// Practice using modulo to loop over an array
// Choose the mix and order of units you want to summon by populating this array:
var summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"];
this.summonTroops = function() {
    // Use % to wrap around the summonTypes array based on this.built.length
    var type = summonTypes[this.built.length % summonTypes.length];
    if (this.gold > this.costOf(type)) {
        this.summon(type);
    }
};
this.commandTroops = function() {
    var friends = this.findFriends();
    for(var friendIndex=0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        var enemy = friend.findNearestEnemy();
        if (enemy) {
            this.command(friend, "attack", enemy);
        }
    }
};
loop {
    var item = this.findNearest(this.findItems());
    this.move(item.pos);
    this.summonTroops();
    this.commandTroops();
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 0
原创粉丝点击