コンテンツにスキップ

弾切れでゲームオーバー

弾切れでゲームオーバー

前の 09 発射したら次の鳥をセットする で、鳥の数が決まりました。 この節では、鳥を撃ち切ったら「ゲームオーバー画面」へ進むようにします。3つ目のシーンを足します。

今回さわる game/: main.js を書き換え(ゲームオーバーのシーンを追加)

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

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

ゲームオーバーの文字を出し、クリックでスタート画面に戻る GameOverScene を作ります。 あわせて、撃ち切ったらこのシーンへ移るようにし、scene の配列に登録します(下の見出しで説明します)。

main.js
128
128
birdsLeft -= 1;
129
129
drawReserve();
130
130
131
this.time.delayedCall(1200, () => spawnBird());
131
this.time.delayedCall(1200, () => {
132
if (birdsLeft > 0) {
133
spawnBird();
134
} else {
135
this.scene.start('GameOver');
136
}
137
});
132
138
});
133
139
}
134
140
}
135
141
142
class GameOverScene extends Phaser.Scene {
143
constructor() {
144
super('GameOver');
145
}
146
147
create() {
148
this.add
149
.text(360, 200, 'ゲームオーバー', {
150
fontSize: '36px',
151
color: '#c0392b',
152
fontStyle: 'bold',
153
})
154
.setOrigin(0.5);
155
156
this.add
157
.text(360, 280, 'クリックでスタートに戻る', {
158
fontSize: '18px',
159
color: '#555555',
160
})
161
.setOrigin(0.5);
162
163
this.input.once('pointerdown', () => this.scene.start('Start'));
164
}
165
}
166
136
167
new Phaser.Game({
137
168
type: Phaser.AUTO,
138
169
width: 720,
main.js
142
173
default: 'matter',
143
174
matter: { gravity: { y: 1 } },
144
175
},
145
scene: [StartScene, GameScene],
176
scene: [StartScene, GameScene, GameOverScene],
146
177
});
  • super('GameOver') … このシーンの名前は 'GameOver'。
  • this.input.once('pointerdown', ...) … once は「一度だけ」反応します。押したら 'Start' へ戻ります。

リロードのタイミングを見直します。次の鳥がまだあるならセット、無ければゲームオーバーへ進みます。

  • if (birdsLeft > 0) … まだ鳥が残っていれば、次の1羽をセットします。
  • else { this.scene.start('GameOver') } … 残りが 0 なら、ゲームオーバー画面へ切り替えます。

new Phaser.Game({ ... }) に渡す設定オブジェクトの scene の配列に、GameOverScene を足します。

鳥を5羽撃ち切ると、少し待ってゲームオーバー画面になります。クリックするとスタート画面に戻り、 また遊べます。これで「始まり」と「終わり」がそろいました。次の節では、増えてきたコードを シーンごとにファイルへ分けて整理します。

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

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

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;
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="main.js" defer></script>
</head>
<body></body>
</html>
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'));
}
}
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 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);
};
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');
}
});
});
}
}
class GameOverScene extends Phaser.Scene {
constructor() {
super('GameOver');
}
create() {
this.add
.text(360, 200, 'ゲームオーバー', {
fontSize: '36px',
color: '#c0392b',
fontStyle: 'bold',
})
.setOrigin(0.5);
this.add
.text(360, 280, 'クリックでスタートに戻る', {
fontSize: '18px',
color: '#555555',
})
.setOrigin(0.5);
this.input.once('pointerdown', () => this.scene.start('Start'));
}
}
new Phaser.Game({
type: Phaser.AUTO,
width: 720,
height: 480,
backgroundColor: '#fdf6e3',
physics: {
default: 'matter',
matter: { gravity: { y: 1 } },
},
scene: [StartScene, GameScene, GameOverScene],
});
この節を終えたときのファイル