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);
}

CodeCombat|ゲーム開発|9. PERSISTENCE PAYSの解答

CodeCombatでプログラミングの勉強をしています。

CodeCombat – PythonとJavaScriptを学ぶインデックスゲーム | コードコンバット

https://codecombat.com/

まずはJavascriptのコースを進めています。

基本的にはヒントを見れば解けるのですがたまに引っかかる問題があるので、そういった箇所の解答をメモしておこうと思います。

今回は以下の問題。

ゲーム開発キャンペーン|9. PERSISTENCE PAYS

// You can use a database to store persistent data.
// Persistent data stays the same between plays of your game!
var generator = game.spawnXY("generator", 60, 40);
generator.spawnType = "munchkin";
generator.spawnDelay = 1;
var player = game.spawnPlayerXY("raider", 36, 30);
player.maxHealth = 70;
player.attackDamage = 10;
game.addSurviveGoal(8);

// db stands for database
// db.add(key, value) increments a value stored in the database.
// This adds 1 to the "plays" key in the database.
db.add("plays", 1);

// Show the value of the "plays" and other keys in the db
ui.track(db, "plays");
ui.track(db, "wins");
ui.track(db, "total defeated");
ui.track(game, "time");

// Show the value of the "defeated" property of the game object
ui.track(game, "defeated");

// The code below will run when the player wins the game.
function onVictory(event) {
    db.add("wins", 1);

    // Use db.add(key, value) to add the value of
    // game.defeated to the database with the key "total defeated"
    db.set("defeated", game.defeated);
}
game.on("victory", onVictory);

どうしても解けない方は参考にしてみてください。

[プログラミング学習]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();
}

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