コンテンツにスキップ

大きさで色を変える

大きさで色を変える

大きさを「半径の数値」で持つと、色分けや「同じ大きさ同士だけ合体」といった判定がやりにくく なります。この節では、大きさを 段階(grade) という整数で表すように切り替え、段階ごとに 色を変えます。

  • grade = 0, 1, 2, … と段階が上がるほど大きく、色が変わる
  • 半径は 20 + grade * 10 で計算する
  • 色は段階に対応した表 COLORS から選ぶ

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

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

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

scenes/game-scene.js の先頭(クラスの外)に、段階ごとの色を並べた表を置きます。この表は spawnBall から参照します。

scenes/game-scene.js
1
1
// ゲーム本編
2
2
3
const COLORS = [
4
0xd7263d,
5
0xff7096,
6
0x8e44ad,
7
0xe67e22,
8
0x2e86de,
9
0x27ae60,
10
0xf1c40f,
11
];
12
3
13
class GameScene extends Phaser.Scene {
4
14
constructor() {
5
15
super('Game');
scenes/game-scene.js
13
23
bottom: 540,
14
24
wall: 12
15
25
};
16
const ballRadius = 20;
17
const grow = 10;
26
const baseRadius = 20;
18
27
19
28
this.lineY = 190;
20
29
this.balls = [];
scenes/game-scene.js
51
60
const dropY = 100;
52
61
53
62
// 箱の内側からはみ出さないよう X を制限する
54
const x = Phaser.Math.Clamp(pointer.x, box.left + ballRadius, box.right - ballRadius);
63
const x = Phaser.Math.Clamp(pointer.x, box.left + baseRadius, box.right - baseRadius);
55
64
56
const ball = this.spawnBall(x, dropY, ballRadius);
65
const ball = this.spawnBall(x, dropY, 0);
57
66
this.balls.push(ball);
58
67
});
scenes/game-scene.js
69
78
this.pendingMerge.push({
70
79
x: (ballA.x + ballB.x) / 2,
71
80
y: (ballA.y + ballB.y) / 2,
72
radius: ballA.radius + grow,
81
grade: ballA.grade + 1,
73
82
});
74
83
}
75
84
});
76
85
}
77
86
78
spawnBall(x, y, radius) {
79
const ball = this.add.circle(x, y, radius, 0x333333);
87
spawnBall(x, y, grade) {
88
const radius = 20 + grade * 10;
89
const color = COLORS[Phaser.Math.Clamp(grade, 0, COLORS.length - 1)];
90
91
const ball = this.add.circle(x, y, radius, color);
92
ball.grade = grade;
80
93
ball.radius = radius; // 線の判定などで使うので半径を持たせておく
81
94
this.matter.add.gameObject(ball, {
82
95
shape: {
scenes/game-scene.js
99
112
if (this.pendingMerge.length > 0) {
100
113
for (const m of this.pendingMerge) {
101
const ball = this.spawnBall(m.x, m.y, m.radius);
114
const ball = this.spawnBall(m.x, m.y, m.grade);
102
115
this.balls.push(ball);
103
116
}
104
117
this.pendingMerge.length = 0;

spawnBall を「半径」から「段階」に変える

Section titled “spawnBall を「半径」から「段階」に変える”

spawnBall の引数を radius から grade に変え、半径と色をその場で計算します。ボールには grade を持たせます。

Clamp(grade, 0, COLORS.length - 1) は、段階が表の数を超えても最後の色で止めるための保険です。

落とすときは一番下の段階 0 を渡します(ballRadius は使わなくなり、基準半径は baseRadius としてクランプ用にだけ残します)。

合体予定も、半径ではなく段階を 1 つ上げた値で積みます。

update() の合体処理も this.spawnBall(m.x, m.y, m.grade) に変えます。

落としたボールは一番小さい色、合体するたびに段階が上がって大きく・別の色になります。 「段階」で持てるようになったので、次の節で「同じ段階同士だけ合体」を実装できます。

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

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

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;
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 + baseRadius, box.right - baseRadius);
const ball = this.spawnBall(x, dropY, 0);
this.balls.push(ball);
});
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;
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>13 大きさを変えるときに色を変える</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],
});
この節を終えたときのファイル