10個落とすとゲームオーバー
「終わり」がないとゲームになりません。この節ではまず簡単な終了条件として、 ボールを 10 個落としたらゲームオーバー画面へ移るようにします。
(この「個数で終わる」ルールは仮のものです。次の次の節で「線を超えたら」に置き換えます。 まずはゲームオーバーへの流れを作るのが目的です。)
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
落とした数を数える
Section titled “落とした数を数える”GameScene に「落とした数」を持たせます。create() の box のすぐ後で上限と初期値を用意します。
main.js を変更する
Section titled “main.js を変更する”3939 bottom: 540,4040 wall: 124141};42const maxDrops = 10;4243
44this.dropCount = 0;45
4346const g = this.add.graphics();4447g.fillStyle(0xe74c3c, 1);4548g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);7376 },7477 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)7578 });79
80 this.dropCount += 1;81 if (this.dropCount >= maxDrops) {82 this.scene.start('GameOver');83 }7684 });7785 }7886}7987
88class GameOverScene extends Phaser.Scene {89 constructor() {90 super('GameOver');91 }92
93 create() {94 this.add95 .text(200, 240, 'ゲームオーバー', {96 fontSize: '32px',97 color: '#c0392b',98 fontStyle: 'bold',99 })100 .setOrigin(0.5);101
102 this.add103 .text(200, 320, 'クリックでスタートに戻る', {104 fontSize: '18px',105 color: '#555555',106 })107 .setOrigin(0.5);108
109 this.input.once('pointerdown', () => this.scene.start('Start'));110 }111}112
80113new Phaser.Game({81114 type: Phaser.AUTO,82115 width: 400,86119 default: 'matter',87120 matter: { gravity: { y: 1 } },88121 },89 scene: [StartScene, GameScene],122 scene: [StartScene, GameScene, GameOverScene],90123});this.dropCount のように this. を付けると、create の外(別のイベントや update)からも
読み書きできます。落とすたびに増やしたいので、シーンに持たせています。
落とす処理の最後にカウントを増やし、上限に達したら GameOver 画面へ移します。
ゲームオーバー画面を足す
Section titled “ゲームオーバー画面を足す”3 つ目のシーン GameOverScene を作ります。メッセージを出し、クリックでスタート画面に戻します。
this.input.once(...) は on と似ていますが、1 回だけ反応します。戻る操作は 1 回で十分なので
once を使います。
最後に、このシーンも Phaser.Game の一覧に足します。
スタート → ボールを 10 個落とす → ゲームオーバー → クリックでスタートに戻る、という一周が できました。次の節で、ゲームオーバーの本命ルール「線を超えたら」に向けて、まず線を描きます。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”game
<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>08 10個落とすとゲームオーバー</title> <style> body { margin: 0; display: grid; place-items: center; min-height: 100vh; background: #222; } canvas { display: block; } </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.90.0/phaser.min.js"></script> <script src="main.js" defer></script> </head> <body></body></html>class StartScene extends Phaser.Scene { constructor() { super('Start'); }
create() { this.add .text(200, 220, 'スイカゲーム風', { fontSize: '32px', color: '#8b5a2b', fontStyle: 'bold', }) .setOrigin(0.5);
const startText = this.add .text(200, 340, '▶ スタート', { fontSize: '28px', color: '#ffffff', backgroundColor: '#e08e3c', padding: { x: 24, y: 12 }, }) .setOrigin(0.5) .setInteractive({ useHandCursor: true });
startText.on('pointerdown', () => this.scene.start('Game')); }}
class GameScene extends Phaser.Scene { constructor() { super('Game'); }
create() { const box = { left: 60, right: 340, top: 150, bottom: 540, wall: 12 }; const maxDrops = 10;
this.dropCount = 0;
const g = this.add.graphics(); g.fillStyle(0xe74c3c, 1); g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x2ecc71, 1); g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x3498db, 1); g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);
const wallH = box.bottom - box.top; const wallCY = (box.top + box.bottom) / 2; this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true }); this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true }); const floorW = box.right - box.left + box.wall * 2; this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });
this.input.on('pointerdown', (pointer) => { const radius = 25; const dropY = 100;
// 箱の内側からはみ出さないよう X を制限する const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);
const ball = this.add.circle(x, dropY, radius, 0x333333);
this.matter.add.gameObject(ball, { shape: { type: 'circle', radius: radius }, restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる) });
this.dropCount += 1; if (this.dropCount >= maxDrops) { this.scene.start('GameOver'); } }); }}
class GameOverScene extends Phaser.Scene { constructor() { super('GameOver'); }
create() { this.add .text(200, 240, 'ゲームオーバー', { fontSize: '32px', color: '#c0392b', fontStyle: 'bold', }) .setOrigin(0.5);
this.add .text(200, 320, 'クリックでスタートに戻る', { fontSize: '18px', color: '#555555', }) .setOrigin(0.5);
this.input.once('pointerdown', () => this.scene.start('Start')); }}
new Phaser.Game({ type: Phaser.AUTO, width: 400, height: 600, backgroundColor: '#fdf6e3', physics: { default: 'matter', matter: { gravity: { y: 1 } }, }, scene: [StartScene, GameScene, GameOverScene],});