JavaScript codecombat.odt

(15 KB) Pobierz

// Keep moving right, but adjust up and down as you go.

while(true) {
    var enemy = hero.findNearestEnemy();
    var xPos = hero.pos.x + 5;
    var yPos = 17;
    if(enemy) {
        // Adjust y up or down to get away from yaks.
        if(enemy.pos.y > hero.pos.y) {
            // If the Yak is above you, subtract 3 from yPos.
            y = y - 3;
        } else if (enemy.pos.y < hero.pos.y) {
            // If the Yak is below you, add 3 to yPos.
            y = y + 3;
        }
    }
    hero.moveXY(xPos, yPos);
}

 

hero.buildXY(„fence”, enemy.pos.x – 20, enemy.pos.y);

 

// This function checks the hero's health
// and returns a Boolean value.
function shouldRun() {
    if (hero.health < hero.maxHealth / 3) {
        return true;
    } else {
        return false;
    }
}

while (true) {
    // Move to the X only if shouldRun() returns true
    if(shouldRun(true)){
         hero.moveXY(75, 37);
    // Else, attack!
    }else{
        var enemy = hero.findNearestEnemy();
        hero.attack(enemy);
    }
}
------------------------------

// Figure out which direction the ogres are coming from.

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Left: enemy.pos.x is less than hero.pos.x
        var isLeft = hero.pos.x  > enemy.pos.x;
        // Above: enemy.pos.y is greater than hero.pos.y
        var isAbove = hero.pos.y < enemy.pos.y;
        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x < enemy.pos.x;
        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y > enemy.pos.y;
        // If enemy isAbove and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isLeft && isAbove) {
            hero.buildXY("fire-trap", 40 - 20, 34 + 17);
        }
        // If enemy isAbove and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isAbove && isRight){
            hero.buildXY("fire-trap", 60, 51);
        }
        // If enemy isBelow and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft){
            hero.buildXY("fire-trap", 20, 17);
        }
        // If enemy isBelow and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isRight){
            hero.buildXY("fire-trap", 60, 17);
        }
        hero.moveXY(40, 34);
    } else {
        hero.moveXY(40, 34);
    }
}
 

Zgłoś jeśli naruszono regulamin