線を超えるとゲームオーバー
いよいよ本命のルールにします。「10 個で終わり」をやめて、ボールが積み上がって線を超えたら
ゲームオーバーに変えます。ここで初めて update(毎フレーム呼ばれる処理)を使います。
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
落とした数のかわりに、ボールの一覧を持つ
Section titled “落とした数のかわりに、ボールの一覧を持つ”判定には「今ある全ボールの位置」を見たいので、create() の状態を作り直します。
dropCount / maxDrops は使わなくなり、代わりに次を持たせます。
main.js を変更する
Section titled “main.js を変更する”3939 bottom: 540,4040 wall: 124141};42const maxDrops = 10;43const lineY = 190;4442
45this.dropCount = 0;43this.radius = 25;44this.lineY = 190;45this.balls = [];46this.isOver = false;4647
4748const g = this.add.graphics();4849g.fillStyle(0xe74c3c, 1);5657g.lineStyle(2, 0xc0392b, 0.8);5758g.beginPath();58g.moveTo(box.left, lineY);59g.lineTo(box.right, lineY);59g.moveTo(box.left, this.lineY);60g.lineTo(box.right, this.lineY);6061g.strokePath();6162
6263const wallH = box.bottom - box.top;6869 this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });6970
7071 this.input.on('pointerdown', (pointer) => {71 const radius = 25;72 if (this.isOver) return;73
7274 const dropY = 100;7375
7476 // 箱の内側からはみ出さないよう X を制限する75 const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);77 const x = Phaser.Math.Clamp(pointer.x, box.left + this.radius, box.right - this.radius);7678
77 const ball = this.add.circle(x, dropY, radius, 0x333333);79 const ball = this.add.circle(x, dropY, this.radius, 0x333333);7880
7981 this.matter.add.gameObject(ball, {8082 shape: {8183 type: 'circle',82 radius: radius84 radius: this.radius8385 },8486 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)8587 });8688
87 this.dropCount += 1;88 if (this.dropCount >= maxDrops) {89 this.balls.push(ball);90 });91 }92
93 update(time, delta) {94 if (this.isOver) return;95
96 // 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ)97 for (const ball of this.balls) {98 if (!ball.body) continue;99 const isSlow = ball.body.speed < 1;100 const top = ball.y - this.radius;101 if (!isSlow || top >= this.lineY) {102 ball.overLineTime = 0;103 continue;104 }105 ball.overLineTime = (ball.overLineTime ?? 0) + delta;106 if (ball.overLineTime > 1000) {107 this.isOver = true;89108 this.scene.start('GameOver');109 return;90110 }91 });111 }92112 }93113}this.balls… 落としたボールをためておく配列(判定でひとつずつ見ます)。this.isOver… すでにゲームオーバーになったか。二重に遷移しないための旗。radius/lineYをthis.に載せたのは、updateからも参照するためです。
線を描く部分は前の節と同じですが、lineY を this.lineY に変えておきます。
落とす処理を調整する
Section titled “落とす処理を調整する”落とすたびに、作ったボールを一覧に加えます。ゲームオーバー後は落とせないようにもします。
毎フレーム、線を超えていないか調べる
Section titled “毎フレーム、線を超えていないか調べる”update(time, delta) はシーンが動いている間、毎フレーム自動で呼ばれます(delta は前フレームからの
経過ミリ秒)。ここで全ボールを調べます。
素朴に「線を超えたら即ゲームオーバー」にすると、落下中に一瞬跨いだだけで負けになってしまいます。
そこで 「ほぼ止まっている(speed < 1)ボールが、線を超えた状態を一定時間(1 秒)続けたら」
だけを負けと判定します。
読み方:
ball.body.speed… Matter が計算しているそのボールの速さ。< 1なら「ほぼ静止」。top = ball.y - this.radius… ボールの上端の Y。これがlineYより上(小さい)=線を超えている。- 条件を満たさないフレームは
overLineTimeを 0 に戻す(=連続していないとリセット)。 - 満たすフレームが続くと
overLineTimeにdeltaを足し込み、1000(ミリ秒)を超えたら負け。
?? 0 は「まだ値が無ければ 0 として扱う」書き方です。ボールごとに「線を超え続けた時間」を
持たせています。
わざとボールを片側に高く積んでみてください。線を超えて 1 秒ほど静止するとゲームオーバーになります。 落としている最中に一瞬跨いだだけでは負けにならないことも確認できます。
これで「遊べる骨組み」が完成しました。次の節では、増えてきたコードを整理するために、 ファイルを分割します。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”game
<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>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 };
this.radius = 25; this.lineY = 190; this.balls = []; this.isOver = false;
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);
g.lineStyle(2, 0xc0392b, 0.8); g.beginPath(); g.moveTo(box.left, this.lineY); g.lineTo(box.right, this.lineY); g.strokePath();
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) => { if (this.isOver) return;
const dropY = 100;
// 箱の内側からはみ出さないよう X を制限する const x = Phaser.Math.Clamp(pointer.x, box.left + this.radius, box.right - this.radius);
const ball = this.add.circle(x, dropY, this.radius, 0x333333);
this.matter.add.gameObject(ball, { shape: { type: 'circle', radius: this.radius }, restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる) });
this.balls.push(ball); }); }
update(time, delta) { if (this.isOver) return;
// 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ) for (const ball of this.balls) { if (!ball.body) continue; const isSlow = ball.body.speed < 1; const top = ball.y - this.radius; if (!isSlow || top >= this.lineY) { ball.overLineTime = 0; continue; } ball.overLineTime = (ball.overLineTime ?? 0) + delta; if (ball.overLineTime > 1000) { this.isOver = true; this.scene.start('GameOver'); return; } } }}
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],});