# enchant.js 宿題2-8 ## 課題8: ゲームオーバーシーンとゲームクリアシーンの作成 ### タスク ・ゲームオーバーシーンとゲームクリアシーンを作成し、それぞれのシーンにテキストラベルを追加します。 ### ヒント ・ラベルはnew Label();で作成し、textプロパティでテキストを設定します。 ・シーンにラベルを追加するにはaddChild(label);を使用します。 ### 開始コード: 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 score = 0; 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); } }); 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); } }); var shark = new Sprite(320, 220); shark.x = 600; shark.y = rand(DISP_SIZE / 2); shark.scaleX = -0.7; shark.scaleY = 0.7; shark.image = core.assets[SHARK]; shark.on('enterframe', function () { this.x -= 5; if (this.x < 0) { this.y = rand(DISP_SIZE / 2); this.x = 600 } }); playScene.addChild(shark); var Heart = Class.create(Sprite, { initialize: function () { Sprite.call(this, 320, 211); this.x = rand(500); this.y = rand(DISP_SIZE / 2); this.scaleX = 0.25; this.scaleY = 0.25; this.image = core.assets[HEART]; this.on('enterframe', function () { if (this.within(fish, 50)) { playScene.removeChild(this); score++; if (score == 10) { core.pushScene(gameClearScene); } } }); playScene.addChild(this); } }); playScene.on('enter', function () { for (i = 0; i < 10; i++) { new Heart(); } }); //ここにコードを作成する。 } 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); } }); 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); } }); var shark = new Sprite(320, 220); shark.x = 600; shark.y = rand(DISP_SIZE / 2); shark.scaleX = -0.7; shark.scaleY = 0.7; shark.image = core.assets[SHARK]; shark.on('enterframe', function () { this.x -= 5; if (this.x < 0) { this.y = rand(DISP_SIZE / 2); this.x = 600 } }); playScene.addChild(shark); var Heart = Class.create(Sprite, { initialize: function () { Sprite.call(this, 320, 211); this.x = rand(500); this.y = rand(DISP_SIZE / 2); this.scaleX = 0.25; this.scaleY = 0.25; this.image = core.assets[HEART]; this.on('enterframe', function () { if (this.within(fish, 50)) { playScene.removeChild(this); score++; if (score == 10) { core.pushScene(gameClearScene); } } }); playScene.addChild(this); } }); playScene.on('enter', function () { for (i = 0; i < 10; i++) { new Heart(); } }); var gameOverScene = new Scene(); gameOverScene.backgroundColor = 'black'; var GOlabel = new Label(); GOlabel.x = 180; GOlabel.y = 200; GOlabel.font = '40px Hiragino'; GOlabel.text = "GAME OVER"; GOlabel.color = 'white'; gameOverScene.addChild(GOlabel); var gameClearScene = new Scene(); var GClabel = new Label(); GClabel.x = 180; GClabel.y = 200; GClabel.font = '40px Hiragino'; GClabel.text = "GAME CLEAR"; gameClearScene.addChild(GClabel); } core.start(); } function rand(n) { return Math.floor(Math.random() * (n + 1)); } </details>