---
# System prepended metadata

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

---

# enchant.js 宿題1-4
## 課題4: ゲームオーバーとクリアの設定
### タスク:
・ねこがリンゴに触れるとゲームクリアとなるよう設定してください。
・ねこがコウモリに触れるか、リンゴが画面外に出てしまったらゲームオーバーとなるよう設定してください。
### 開始コード:
    enchant();

    window.onload = function() {
        const BACKGROUND = "res/mori.jpeg"
        const CAT = "res/cat.png";
        const APPLE = "res/apple.png";
        const BAT = "res/bat.png";
        const DISP_SIZE = 640;

        var core = new Core(DISP_SIZE, DISP_SIZE);
        core.preload(BACKGROUND, CAT, APPLE, BAT);
        core.onload = function() {

            var background = new Sprite(600, 338);
            background.x = 0;
            background.y = 0;
            background.scaleX = 1.0;
            background.scaleY = 2.0;
            background.image = core.assets[BACKGROUND];
            core.rootScene.addChild(background);

            var cat = new Sprite(100, 100);
            cat.x = 5;
            cat.y = 400;
            cat.scaleX = 0.5;
            cat.scaleY = 0.5;
            cat.image = core.assets[CAT];
            cat.on('enterframe', function() {
                if (core.input.right) {
                    this.x += 10;
                }
                if (core.input.left) {
                    this.x -= 10;
                }
            });
            core.rootScene.addChild(cat);

            var apple = new Sprite(100, 100);
            apple.x = 400;
            apple.y = 0;
            apple.scaleX = 0.5;
            apple.scaleY = 0.5;
            apple.image = core.assets[APPLE];
            apple.on("enterframe", function(){
                this.y += 5;
            });
            core.rootScene.addChild(apple);

            var bat = new Sprite(400, 281);
            bat.x = 100;
            bat.y = 200;
            bat.scaleX = 0.5;
            bat.scaleY = 0.5;
            bat.image = core.assets[BAT];
            bat.on("enterframe", function(){
                if (core.frame % 40 >= 20) {
                    this.y += rand(25);
                }
                else {
                    this.y -= rand(20);
                }
            })
            core.rootScene.addChild(bat);

            // ここにコードを追加

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

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

    window.onload = function() {
        const BACKGROUND = "res/mori.jpeg"
        const CAT = "res/cat.png";
        const APPLE = "res/apple.png";
        const BAT = "res/bat.png";
        const DISP_SIZE = 640;

        var core = new Core(DISP_SIZE, DISP_SIZE);
        core.preload(BACKGROUND, CAT, APPLE, BAT);
        core.onload = function() {

        var background = new Sprite(600, 338);
        background.x = 0;
        background.y = 0;
        background.scaleX = 1.0;
        background.scaleY = 2.0;
        background.image = core.assets[BACKGROUND];
        core.rootScene.addChild(background);

        var cat = new Sprite(100, 100);
        cat.x = 5;
        cat.y = 400;
        cat.scaleX = 0.5;
        cat.scaleY = 0.5;
        cat.image = core.assets[CAT];
        cat.on('enterframe', function() {
            if (core.input.right) {
                this.x += 10;
            }
            if (core.input.left) {
                this.x -= 10;
            }
            if (this.within(apple, 30)) {
                core.pushScene(gameClear);
            }
            if (this.within(bat, 40)) {
                core.pushScene(gameOver);
            }
        });
        core.rootScene.addChild(cat);

        var apple = new Sprite(100, 100);
        apple.x = 400;
        apple.y = 0;
        apple.scaleX = 0.5;
        apple.scaleY = 0.5;
        apple.image = core.assets[APPLE];
        apple.on("enterframe", function(){
            this.y += 5;
            if(500 < this.y){
                core.pushScene(gameOver);
            }
        });
        core.rootScene.addChild(apple);

        var bat = new Sprite(400, 281);
        bat.x = 100;
        bat.y = 200;
        bat.scaleX = 0.5;
        bat.scaleY = 0.5;
        bat.image = core.assets[BAT];
        bat.on("enterframe", function(){
            if (core.frame % 40 >= 20) {
                this.y += rand(25);
            }
            else {
                this.y -= rand(20);
            }
        })
        core.rootScene.addChild(bat);

        var gameOver = new Scene();
        gameOver.backgroundColor = 'black';
        var GOlabel = new Label();
        GOlabel.x = 180;
        GOlabel.y = 200;
        GOlabel.font = '40px Hiragino';
        GOlabel.text = "GAME OVER";
        GOlabel.color = 'white';
        gameOver.addChild(GOlabel);

        var gameClear = new Scene();
        var GClabel = new Label();
        GClabel.x = 180;
        GClabel.y = 200;
        GClabel.font = '40px Hiragino';
        GClabel.text = "GAME CLAER";
        gameClear.addChild(GClabel);

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


</details>