落とすたびに数を表示する
画面に数値(HUD)を出す練習として、まず落とした回数を表示します。次の 2 つの節で、これを 「スコア」に育てていきます。
今回さわる
game/:scenes/game-scene.jsを書き換え
前の節を終えていない人は、ここから始められます。
数を持って、画面に文字を置く
Section titled “数を持って、画面に文字を置く”create() の状態に数を用意し、画面左上に文字を置きます。
scenes/game-scene.js を変更する
Section titled “scenes/game-scene.js を変更する”3030this.pendingRemoval = new Set();3131this.pendingMerge = [];3232this.isOver = false;33this.count = 0;3334
3435const g = this.add.graphics();3536g.fillStyle(0xe74c3c, 1);5455const floorW = box.right - box.left + box.wall * 2;5556this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });5657
58this.countText = this.add.text(16, 16, '0', {59 fontSize: '28px',60 color: '#333333',61 fontStyle: 'bold',62});63
5764this.input.on('pointerdown', (pointer) => {5865 if (this.isOver) return;6471 const ball = this.spawnBall(x, dropY, 0);6572 this.balls.push(ball);73
74 this.count += 1;75 this.countText.setText(String(this.count));6676});6777
6878this.matter.world.on('collisionstart', (event) => {this.add.text(x, y, 初期文字, スタイル) で文字を置けます。左上(16, 16)に「0」を出しておき、
あとで中身だけ書き換えます。書き換えたいので、this.countText としてシーンに覚えておきます。
落とすたびに増やして表示を更新する
Section titled “落とすたびに増やして表示を更新する”落とす処理の最後で数を増やし、文字を更新します。
setText(...) は、すでに置いてある文字の中身だけを差し替えます(毎回新しい文字を作らない)。
String(this.count) で数値を文字にしています。
ボールを落とすたびに、左上の数字が 1 ずつ増えます。数値を画面に出して更新する、という HUD の基本ができました。次の節で、これを「スコア」と呼び、結果画面にも引き継ぎます。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”game
// ゲーム本編
const COLORS = [ 0xd7263d, 0xff7096, 0x8e44ad, 0xe67e22, 0x2e86de, 0x27ae60, 0xf1c40f,];
class GameScene extends Phaser.Scene { constructor() { super('Game'); }
create() { const box = { left: 60, right: 340, top: 150, bottom: 540, wall: 12 }; const baseRadius = 20;
this.lineY = 190; this.balls = []; this.pendingRemoval = new Set(); this.pendingMerge = []; this.isOver = false; this.count = 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);
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.countText = this.add.text(16, 16, '0', { fontSize: '28px', color: '#333333', fontStyle: 'bold', });
this.input.on('pointerdown', (pointer) => { if (this.isOver) return;
const dropY = 100;
// 箱の内側からはみ出さないよう X を制限する const x = Phaser.Math.Clamp(pointer.x, box.left + baseRadius, box.right - baseRadius);
const ball = this.spawnBall(x, dropY, 0); this.balls.push(ball);
this.count += 1; this.countText.setText(String(this.count)); });
this.matter.world.on('collisionstart', (event) => { for (const pair of event.pairs) { const ballA = pair.bodyA.gameObject; const ballB = pair.bodyB.gameObject; if (!ballA || !ballB) continue; if (this.pendingRemoval.has(ballA) || this.pendingRemoval.has(ballB)) continue; // 同じ段階どうしだけ合体する(違う大きさはぶつかるだけ) if (ballA.grade !== ballB.grade) continue;
this.pendingRemoval.add(ballA); this.pendingRemoval.add(ballB); this.pendingMerge.push({ x: (ballA.x + ballB.x) / 2, y: (ballA.y + ballB.y) / 2, grade: ballA.grade + 1, }); } }); }
spawnBall(x, y, grade) { const radius = 20 + grade * 10; const color = COLORS[Phaser.Math.Clamp(grade, 0, COLORS.length - 1)];
const ball = this.add.circle(x, y, radius, color); ball.grade = grade; ball.radius = radius; // 線の判定などで使うので半径を持たせておく this.matter.add.gameObject(ball, { shape: { type: 'circle', radius: radius }, restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる) }); return ball; }
update(time, delta) { if (this.pendingRemoval.size > 0) { for (const ball of this.pendingRemoval) { Phaser.Utils.Array.Remove(this.balls, ball); ball.destroy(); } this.pendingRemoval.clear(); }
if (this.pendingMerge.length > 0) { for (const m of this.pendingMerge) { const ball = this.spawnBall(m.x, m.y, m.grade); this.balls.push(ball); } this.pendingMerge.length = 0; }
if (this.isOver) return;
// 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ) for (const ball of this.balls) { if (!ball.body) continue; const isSlow = ball.body.speed < 1; const top = ball.y - ball.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')); }}// スタート画面
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')); }}<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>15 ボールを落とすたびに数値が増える</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="scenes/start-scene.js" defer></script> <script src="scenes/game-scene.js" defer></script> <script src="scenes/gameover-scene.js" defer></script> <script src="main.js" defer></script> </head> <body></body></html>// ボールを落とすたびに数値が増えて表示される。
new Phaser.Game({ type: Phaser.AUTO, width: 400, height: 600, backgroundColor: '#fdf6e3', physics: { default: 'matter', matter: { gravity: { y: 1 } }, }, scene: [StartScene, GameScene, GameOverScene],});