Code Combat|AIRDROP(java script)の解答

Code Combat|AIRDROP(java script) solution

自力で解きたい方は見ないようにしてください。

// すべての剣を集め、村を守れ!
function onSpawn(event) {
    while (true) {
        var item = hero.findNearestItem();
        //  The pet should fetch the item if it exists:
        if (item) {
            pet.fetch(item);
        }
    }
}
// Assign onSpawn function for the pet's "spawn".
pet.on("spawn", onSpawn);
while (true) {
    // Guard the left passage: 
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }
}

if (item) の箇所、テンプレートだと

if (item.type == “potion”)

のようになってるので “potion” を “sword” とか “arms” に変えたりしたのですがそれではうまく動かず、if (item) のみにしたら正解でした。

しかし、codecombatの解答を検索してもPython版ばかり出てくるのでjava scriptを学んでいることに躊躇してしまいます。

Code Combat|SHORT-SIGHTED BURL(java script)の解答

結構悩んだので共有します。自力で解きたい方は見ないようにしてください。

Code Combat, SHORT-SIGHTED BURL (java script) stage solution.

画面全体
code
// Collect coins and run, or else the burl will find you.

// This function allows your hero take an item.
function takeItem(item) {
    hero.moveXY(item.pos.x, item.pos.y);
}

// Write the function "checkTakeRun" with one parameter.
// If the item exists, use "takeItem" function to take it.
// Go back to the start (green mark), with or without the item.
function checkTakeRun(target) {
if (target) {
    hero.moveXY(target.pos.x, target.pos.y);
}
    hero.moveXY(40, 12);
}

// Don't change this code.
while (true) {
    hero.moveXY(16, 56);
    var coin = hero.findNearestItem();
    checkTakeRun(coin);
    
    hero.moveXY(64, 56);
    coin = hero.findNearestItem();
    checkTakeRun(coin);
}

[プログラミング学習]code.org/Sprite Movement #9-a の解き方

無料のプログラミング学習サイト「code.org」でプログラミングの勉強をしています。

今日やったのはこの問題

Sprite Movement #9 | Self Paced Introduction to Game Lab – Code.org

魚を増やす!
カウンターパターンを学ぶ前に、スプライトのプロパティ(回転など)をランダムな値に設定してアニメーションさせることを学びましたね。魚の回転をランダムな値に設定することで、魚が微妙に動いているように見せることができます。これで、よりリアルな動きのアニメーションが実現できます!

こうしてください:
それぞれの魚について、描画ループの中で回転をランダムに設定します。
できるだけリアルに見えるように、マイナスからプラスまでの小さな範囲を選びます。

https://studio.code.org/s/csd3-virtual/lessons/9/levels/9/sublevel/1

右から左に移動する魚の群れに、rotationをrandomNumberで入れて震えさせます。

rotationを入れる位置試行錯誤しましたが、以下の場所にいれたらうまく動きました。

var orangeFish = createSprite(400, randomNumber(0, 100));
orangeFish.setAnimation("orange_fish");
var blueFish = createSprite(250, randomNumber(0, 200));
blueFish.setAnimation("blue_fish");
var greenFish = createSprite(300, randomNumber(200, 300));
greenFish.setAnimation("green_fish");

function draw() {
  // Draw Background
  background("navy");
  
  // Update Values
  orangeFish.x = orangeFish.x - 2;
  orangeFish.rotation = randomNumber(1, 10);
  blueFish.x = blueFish.x - 3;
  blueFish.rotation = randomNumber(1, 10);
  greenFish.x = greenFish.x - 1;
  greenFish.rotation = randomNumber(1, 10);
  // Draw Animations
  drawSprites();
}

悩んでいる方は参考にしてみてください。