コンテンツにスキップ

線を超えるとゲームオーバー

線を超えるとゲームオーバー

いよいよ本命のルールにします。「10 個で終わり」をやめて、ボールが積み上がって線を超えたら ゲームオーバーに変えます。ここで初めて update(毎フレーム呼ばれる処理)を使います。

今回さわる game/: main.js を書き換え

前の節を終えていない人は、ここから始められます。

この節を始めるときのファイル

落とした数のかわりに、ボールの一覧を持つ

Section titled “落とした数のかわりに、ボールの一覧を持つ”

判定には「今ある全ボールの位置」を見たいので、create() の状態を作り直します。 dropCount / maxDrops は使わなくなり、代わりに次を持たせます。

main.js
39
39
bottom: 540,
40
40
wall: 12
41
41
};
42
const maxDrops = 10;
43
const lineY = 190;
44
42
45
this.dropCount = 0;
43
this.radius = 25;
44
this.lineY = 190;
45
this.balls = [];
46
this.isOver = false;
46
47
47
48
const g = this.add.graphics();
48
49
g.fillStyle(0xe74c3c, 1);
main.js
56
57
g.lineStyle(2, 0xc0392b, 0.8);
57
58
g.beginPath();
58
g.moveTo(box.left, lineY);
59
g.lineTo(box.right, lineY);
59
g.moveTo(box.left, this.lineY);
60
g.lineTo(box.right, this.lineY);
60
61
g.strokePath();
61
62
62
63
const wallH = box.bottom - box.top;
main.js
68
69
this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });
69
70
70
71
this.input.on('pointerdown', (pointer) => {
71
const radius = 25;
72
if (this.isOver) return;
73
72
74
const dropY = 100;
73
75
74
76
// 箱の内側からはみ出さないよう 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);
76
78
77
const ball = this.add.circle(x, dropY, radius, 0x333333);
79
const ball = this.add.circle(x, dropY, this.radius, 0x333333);
78
80
79
81
this.matter.add.gameObject(ball, {
80
82
shape: {
81
83
type: 'circle',
82
radius: radius
84
radius: this.radius
83
85
},
84
86
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
85
87
});
86
88
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;
89
108
this.scene.start('GameOver');
109
return;
90
110
}
91
});
111
}
92
112
}
93
113
}
  • this.balls … 落としたボールをためておく配列(判定でひとつずつ見ます)。
  • this.isOver … すでにゲームオーバーになったか。二重に遷移しないための旗。
  • radius / lineY を this. に載せたのは、update からも参照するためです。

線を描く部分は前の節と同じですが、lineY を this.lineY に変えておきます。

落とすたびに、作ったボールを一覧に加えます。ゲームオーバー後は落とせないようにもします。

毎フレーム、線を超えていないか調べる

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],
});
この節を終えたときのファイル