コンテンツにスキップ

10個落とすとゲームオーバー

10個落とすとゲームオーバー

「終わり」がないとゲームになりません。この節ではまず簡単な終了条件として、 ボールを 10 個落としたらゲームオーバー画面へ移るようにします。

(この「個数で終わる」ルールは仮のものです。次の次の節で「線を超えたら」に置き換えます。 まずはゲームオーバーへの流れを作るのが目的です。)

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

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

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

GameScene に「落とした数」を持たせます。create() の box のすぐ後で上限と初期値を用意します。

main.js
39
39
bottom: 540,
40
40
wall: 12
41
41
};
42
const maxDrops = 10;
42
43
44
this.dropCount = 0;
45
43
46
const g = this.add.graphics();
44
47
g.fillStyle(0xe74c3c, 1);
45
48
g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);
main.js
73
76
},
74
77
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
75
78
});
79
80
this.dropCount += 1;
81
if (this.dropCount >= maxDrops) {
82
this.scene.start('GameOver');
83
}
76
84
});
77
85
}
78
86
}
79
87
88
class GameOverScene extends Phaser.Scene {
89
constructor() {
90
super('GameOver');
91
}
92
93
create() {
94
this.add
95
.text(200, 240, 'ゲームオーバー', {
96
fontSize: '32px',
97
color: '#c0392b',
98
fontStyle: 'bold',
99
})
100
.setOrigin(0.5);
101
102
this.add
103
.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
80
113
new Phaser.Game({
81
114
type: Phaser.AUTO,
82
115
width: 400,
main.js
86
119
default: 'matter',
87
120
matter: { gravity: { y: 1 } },
88
121
},
89
scene: [StartScene, GameScene],
122
scene: [StartScene, GameScene, GameOverScene],
90
123
});

this.dropCount のように this. を付けると、create の外(別のイベントや update)からも 読み書きできます。落とすたびに増やしたいので、シーンに持たせています。

落とす処理の最後にカウントを増やし、上限に達したら GameOver 画面へ移します。

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