---
# System prepended metadata

title: enchant.js 宿題2-4
tags: [enchant.js 宿題]

---

# enchant.js 宿題2-4
## 課題4: 魚の動きと衝突判定の設定
### タスク
・魚のスプライトに対し、上下左右のキーが押されたときの動きを設定します。
・魚と鮫のスプライトが重なったとき（衝突したとき）にゲームオーバーシーンに遷移するよう設定します。

### ヒント
・スプライトの位置はxとyプロパティを直接変更することで制御します。
・衝突判定はwithin関数を使用して行います。

### 開始コード:
    enchant();

    window.onload = function () {
        const FISH = "res/fish.png";
        const HEART = "res/heart.png";
        const SHARK = "res/shark.png";
        const SEA = "res/Sea.png";
        const START = "res/start.png";
        const DISP_SIZE = 640;

        var core = new Core(DISP_SIZE, DISP_SIZE);
        core.preload(FISH, HEART, SHARK, SEA, START);
        core.fps = 30;
        core.keybind(32, "Space");
        core.onload = function () {
        
            var background = new Sprite(640, 481);
            background.x = 0;
            background.y = 0;
            background.scaleX = 1.5;
            background.scaleY = 1.5;
            background.image = core.assets[SEA];
            core.rootScene.addChild(background);

            var start = new Sprite(320, 142);
            start.x = 150;
            start.y = 150;
            start.scaleX = 1.0;
            start.scaleY = 1.0;
            start.image = core.assets[START];
            core.rootScene.addChild(start);

            core.rootScene.on("enterframe", function (){
    
                if (core.input.Space) {
                    core.rootScene.removeChild(start);
                    core.pushScene(playScene);
                }
            });
            
            var playScene = new Scene();
    
            var background = new Sprite(640, 481);
            background.x = 0;
            background.y = 0;
            background.scaleX = 1.5;
            background.scaleY = 1.5;
            background.image = core.assets[SEA];
            playScene.addChild(background);

            var fish = new Sprite(320, 211);
            fish.x = 100;
            fish.y = 100;
            fish.scaleX = 0.5;
            fish.scaleY = 0.5;
            fish.image = core.assets[FISH];
            playScene.addChild(fish);
            // ここにコードを追加
            
        }
        core.start();
   
    }
    function rand(n) {
    return Math.floor(Math.random() * (n + 1));
    }

<details>
<summary>解答</summary>
    
    enchant();

    window.onload = function () {
        const FISH = "res/fish.png";
        const HEART = "res/heart.png";
        const SHARK = "res/shark.png";
        const SEA = "res/Sea.png";
        const START = "res/start.png";
        const DISP_SIZE = 640;

        var core = new Core(DISP_SIZE, DISP_SIZE);
        core.preload(FISH, HEART, SHARK, SEA, START);
        core.fps = 30;
        core.keybind(32, "Space");
        core.onload = function () {
        
            var background = new Sprite(640, 481);
            background.x = 0;
            background.y = 0;
            background.scaleX = 1.5;
            background.scaleY = 1.5;
            background.image = core.assets[SEA];
            core.rootScene.addChild(background);

            var start = new Sprite(320, 142);
            start.x = 150;
            start.y = 150;
            start.scaleX = 1.0;
            start.scaleY = 1.0;
            start.image = core.assets[START];
            core.rootScene.addChild(start);

            core.rootScene.on("enterframe", function (){
    
                if (core.input.Space) {
                    core.rootScene.removeChild(start);
                    core.pushScene(playScene);
                }
            });
            
            var playScene = new Scene();
    
            var background = new Sprite(640, 481);
            background.x = 0;
            background.y = 0;
            background.scaleX = 1.5;
            background.scaleY = 1.5;
            background.image = core.assets[SEA];
            playScene.addChild(background);

            var fish = new Sprite(320, 211);
            fish.x = 100;
            fish.y = 100;
            fish.scaleX = 0.5;
            fish.scaleY = 0.5;
            fish.image = core.assets[FISH];
            playScene.addChild(fish);
            fish.on('enterframe', function () {
                if (core.input.up) {
                    this.y -= 10;
                }
                if (core.input.down) {
                    this.y += 10;
                }
                if (core.input.right) {
                    this.x += 10;
                    fish.scaleX = 0.5;
                }
                if (core.input.left) {
                    this.x -= 10;
                    fish.scaleX = -0.5;
                }
                if (this.within(shark, 70)) {
                    core.pushScene(gameOverScene);
                }
            });

            
        }
        core.start();
    }
    function rand(n) {
    return Math.floor(Math.random() * (n + 1));
    }

</details>