コンテンツにスキップ

結果にスコアを表示

結果にスコアを表示

前の 04 ブタを倒してスコアを足す で、スコアがつきました。この節では、 クリア画面とゲームオーバー画面にも最終スコアを表示します。ゲームの結果を伝える「結果画面」の 完成です。

今回さわる game/: scenes/game-scene.js・scenes/clear-scene.js・scenes/gameover-scene.js を書き換え

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

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

シーンを切り替えるとき、2つ目の引数で好きなデータを渡せます。ここでスコアを渡します。 GameScene の、クリアへ進む所とゲームオーバーへ進む所を書き換えます。

scenes/game-scene.js
144
144
if (birdsLeft > 0) {
145
145
spawnBird();
146
146
} else {
147
this.scene.start('GameOver');
147
this.scene.start('GameOver', { score: this.score });
148
148
}
149
149
});
150
150
});
scenes/game-scene.js
162
162
// 何度も遷移しないよう、クリアは1回だけ。
163
163
if (!this.cleared && this.pigsLeft <= 0) {
164
164
this.cleared = true;
165
this.scene.start('Clear');
165
this.scene.start('Clear', { score: this.score });
166
166
}
167
167
}
168
168
}
  • this.scene.start('Clear', { score: this.score }) … クリア画面へ進むとき、いまのスコアを一緒に渡します。

結果画面で受け取って表示する

Section titled “結果画面で受け取って表示する”

受け取り側の create は create(data) と書くと、渡されたデータを受け取れます。 ClearScene にスコアの表示を足します。

scenes/clear-scene.js
3
3
super('Clear');
4
4
}
5
5
6
create() {
6
create(data) {
7
const score = data.score || 0;
8
7
9
this.add
8
.text(360, 200, 'クリア!', {
10
.text(360, 180, 'クリア!', {
9
11
fontSize: '40px',
10
12
color: '#2e7d32',
11
13
fontStyle: 'bold',
scenes/clear-scene.js
13
15
.setOrigin(0.5);
14
16
15
17
this.add
16
.text(360, 280, 'クリックでスタートに戻る', {
18
.text(360, 250, 'スコア: ' + score, {
19
fontSize: '28px',
20
color: '#333333',
21
})
22
.setOrigin(0.5);
23
24
this.add
25
.text(360, 320, 'クリックでスタートに戻る', {
17
26
fontSize: '18px',
18
27
color: '#555555',
19
28
})
  • create(data) … シーンを始めるときに渡されたデータ({ score })を受け取ります。
  • data.score || 0 … スコアが渡されていればその値、無ければ 0 を使います。

GameOverScene にも同じようにスコアの表示を足します(見出しの文字が「ゲームオーバー」になるだけです)。

scenes/gameover-scene.js
3
3
super('GameOver');
4
4
}
5
5
6
create() {
6
create(data) {
7
const score = data.score || 0;
8
7
9
this.add
8
.text(360, 200, 'ゲームオーバー', {
10
.text(360, 180, 'ゲームオーバー', {
9
11
fontSize: '36px',
10
12
color: '#c0392b',
11
13
fontStyle: 'bold',
scenes/gameover-scene.js
13
15
.setOrigin(0.5);
14
16
15
17
this.add
16
.text(360, 280, 'クリックでスタートに戻る', {
18
.text(360, 250, 'スコア: ' + score, {
19
fontSize: '28px',
20
color: '#333333',
21
})
22
.setOrigin(0.5);
23
24
this.add
25
.text(360, 320, 'クリックでスタートに戻る', {
17
26
fontSize: '18px',
18
27
color: '#555555',
19
28
})

クリアしても、鳥を撃ち切って負けても、結果画面に最終スコアが出るようになりました。 これで「パチンコで飛ばして標的を倒し、スコアを競う」ゲームの中身が完成です。 次の章から、画像や音をつけて仕上げていきます。

↗ 新しいタブで開く
このステップの完成イメージ(実際に触って動かせます)

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
game
class ClearScene extends Phaser.Scene {
constructor() {
super('Clear');
}
create(data) {
const score = data.score || 0;
this.add
.text(360, 180, 'クリア!', {
fontSize: '40px',
color: '#2e7d32',
fontStyle: 'bold',
})
.setOrigin(0.5);
this.add
.text(360, 250, 'スコア: ' + score, {
fontSize: '28px',
color: '#333333',
})
.setOrigin(0.5);
this.add
.text(360, 320, 'クリックでスタートに戻る', {
fontSize: '18px',
color: '#555555',
})
.setOrigin(0.5);
this.input.once('pointerdown', () => this.scene.start('Start'));
}
}
class GameScene extends Phaser.Scene {
constructor() {
super('Game');
}
create() {
const groundX = 360;
const groundY = 440;
const groundWidth = 720;
const groundHeight = 80;
const groundTop = groundY - groundHeight / 2; // 地面の上面。物はこの高さに乗る
const ground = this.add.rectangle(
groundX,
groundY,
groundWidth,
groundHeight,
0x888888,
);
this.matter.add.gameObject(ground, { isStatic: true });
const boxSize = 40;
const towerX = 560;
for (let i = 0; i < 3; i++) {
const boxY = groundTop - boxSize / 2 - i * boxSize;
const box = this.add.rectangle(towerX, boxY, boxSize, boxSize, 0xdddddd);
box.setStrokeStyle(3, 0x333333);
this.matter.add.gameObject(box, { restitution: 0.1 });
}
const pigRadius = 16;
const pigPositions = [
{ x: 460, y: groundTop - pigRadius }, // 手前のブタ
{ x: towerX, y: groundTop - boxSize * 3 - pigRadius }, // タワーの上のブタ
{ x: 660, y: groundTop - pigRadius }, // 奥のブタ
];
for (const pos of pigPositions) {
const pig = this.add.circle(pos.x, pos.y, pigRadius, 0xaaaaaa);
pig.setStrokeStyle(3, 0x333333);
this.matter.add.gameObject(pig, {
shape: { type: 'circle', radius: pigRadius },
restitution: 0.2,
});
pig.isPig = true; // 衝突したときに見分けるための目印。
}
this.pigsLeft = pigPositions.length;
this.cleared = false;
this.score = 0;
this.scoreText = this.add
.text(700, 30, 'スコア: 0', { fontSize: '20px', color: '#333333' })
.setOrigin(1, 0.5);
// 衝突中に消すと不安定なので、ためて update でまとめて消す。
this.pendingRemoval = new Set();
this.matter.world.on('collisionstart', (event) => {
for (const pair of event.pairs) {
const gameObjectA = pair.bodyA.gameObject;
const gameObjectB = pair.bodyB.gameObject;
if (!gameObjectA || !gameObjectB) continue;
if (gameObjectA.isBird && gameObjectB.isPig)
this.pendingRemoval.add(gameObjectB);
if (gameObjectB.isBird && gameObjectA.isPig)
this.pendingRemoval.add(gameObjectA);
}
});
const anchor = { x: 140, y: 300 };
const power = 0.22; // 引っ張った長さを速さに変える倍率
const birdRadius = 18;
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
const aim = this.add.graphics();
let birdsLeft = 5;
const reserve = this.add.graphics();
const drawReserve = () => {
reserve.clear();
for (let i = 0; i < birdsLeft; i++) {
const x = 30 + i * 26;
reserve.fillStyle(0xffffff, 1);
reserve.fillCircle(x, 40, 9);
reserve.lineStyle(2, 0x333333, 1);
reserve.strokeCircle(x, 40, 9);
}
};
// いま操作できる鳥。発射中やリロード待ちのときは null。
let bird = null;
let dragging = false;
const spawnBird = () => {
if (birdsLeft <= 0) return;
bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
bird.setStrokeStyle(3, 0x333333);
this.matter.add.gameObject(bird, {
shape: { type: 'circle', radius: birdRadius },
restitution: 0.2,
});
// 待機中は動かないように静的にしておく。
bird.setStatic(true);
bird.isBird = true; // 同じく、鳥かどうかの目印。
};
drawReserve();
spawnBird();
this.input.on('pointerdown', () => {
if (!bird || dragging) return;
dragging = true;
});
this.input.on('pointermove', (pointer) => {
if (!dragging || !bird) return;
bird.setPosition(pointer.x, pointer.y);
const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;
const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;
aim.clear();
aim.lineStyle(2, 0x333333, 0.5);
aim.lineBetween(bird.x, bird.y, forwardX, forwardY);
});
this.input.on('pointerup', () => {
if (!dragging || !bird) return;
dragging = false;
aim.clear();
const vx = (anchor.x - bird.x) * power;
const vy = (anchor.y - bird.y) * power;
bird.setStatic(false);
bird.setVelocity(vx, vy);
bird = null;
birdsLeft -= 1;
drawReserve();
this.time.delayedCall(1200, () => {
if (birdsLeft > 0) {
spawnBird();
} else {
this.scene.start('GameOver', { score: this.score });
}
});
});
}
update() {
for (const pig of this.pendingRemoval) {
pig.destroy();
this.pigsLeft -= 1;
this.score += 1000;
this.scoreText.setText('スコア: ' + this.score);
}
this.pendingRemoval.clear();
// 何度も遷移しないよう、クリアは1回だけ。
if (!this.cleared && this.pigsLeft <= 0) {
this.cleared = true;
this.scene.start('Clear', { score: this.score });
}
}
}
class GameOverScene extends Phaser.Scene {
constructor() {
super('GameOver');
}
create(data) {
const score = data.score || 0;
this.add
.text(360, 180, 'ゲームオーバー', {
fontSize: '36px',
color: '#c0392b',
fontStyle: 'bold',
})
.setOrigin(0.5);
this.add
.text(360, 250, 'スコア: ' + score, {
fontSize: '28px',
color: '#333333',
})
.setOrigin(0.5);
this.add
.text(360, 320, 'クリックでスタートに戻る', {
fontSize: '18px',
color: '#555555',
})
.setOrigin(0.5);
this.input.once('pointerdown', () => this.scene.start('Start'));
}
}
class StartScene extends Phaser.Scene {
constructor() {
super('Start');
}
create() {
this.add
.text(360, 180, 'パチンコ物理ゲーム', {
fontSize: '36px',
color: '#5d4037',
fontStyle: 'bold',
})
.setOrigin(0.5);
const startText = this.add
.text(360, 300, '▶ スタート', {
fontSize: '28px',
color: '#ffffff',
backgroundColor: '#e08e3c',
padding: { x: 24, y: 12 },
})
.setOrigin(0.5)
.setInteractive({ useHandCursor: true });
startText.on('pointerdown', () => this.scene.start('Game'));
}
}
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>16 結果にスコアを表示</title>
<style>
body {
margin: 0;
overflow: hidden;
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="scenes/start-scene.js" defer></script>
<script src="scenes/game-scene.js" defer></script>
<script src="scenes/gameover-scene.js" defer></script>
<script src="scenes/clear-scene.js" defer></script>
<script src="main.js" defer></script>
</head>
<body></body>
</html>
new Phaser.Game({
type: Phaser.AUTO,
width: 720,
height: 480,
backgroundColor: '#fdf6e3',
physics: {
default: 'matter',
matter: { gravity: { y: 1 } },
},
scene: [StartScene, GameScene, GameOverScene, ClearScene],
});
この節を終えたときのファイル