コンテンツにスキップ

標的(ブタ)を置く

標的(ブタ)を置く

ここからはゲームのルールを作ります。アングリーバードの目的は「パチンコで飛ばして、標的を全部倒す」 ことです。まずはその標的(ブタ)を置きます。この章からは、変更するのはおもに scenes/game-scene.js です。

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

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

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

標的のブタを3匹置きます。鳥(白い丸)と区別できるよう、灰色の丸にします。

scenes/game-scene.js
29
29
this.matter.add.gameObject(box, { restitution: 0.1 });
30
30
}
31
31
32
const pigRadius = 16;
33
const pigPositions = [
34
{ x: 460, y: groundTop - pigRadius }, // 手前のブタ
35
{ x: towerX, y: groundTop - boxSize * 3 - pigRadius }, // タワーの上のブタ
36
{ x: 660, y: groundTop - pigRadius }, // 奥のブタ
37
];
38
for (const pos of pigPositions) {
39
const pig = this.add.circle(pos.x, pos.y, pigRadius, 0xaaaaaa);
40
pig.setStrokeStyle(3, 0x333333);
41
this.matter.add.gameObject(pig, {
42
shape: { type: 'circle', radius: pigRadius },
43
restitution: 0.2,
44
});
45
}
46
32
47
const anchor = { x: 140, y: 300 };
33
48
const power = 0.22; // 引っ張った長さを速さに変える倍率
34
49
const birdRadius = 18;
  • pigRadius = 16 … ブタの大きさ(鳥より少し小さめ)。
  • pigPositions … ブタを置く場所のリスト。箱タワーの手前の地面・箱タワーの上・箱タワーの奥の地面の3か所です。
  • for (const pos of pigPositions) … リストの場所ぶんだけ、くり返してブタを作ります。
  • this.add.circle(..., 0xaaaaaa) … 灰色の丸でブタを描きます。鳥(白)と見分けられます。
  • ブタも物理ボディにするので、鳥や箱がぶつかると押されて動きます。

3匹の y も、鳥や箱と同じ考え方で決めています。丸の絵は中心の位置を受け取るので、 地面の上に置くなら半径ぶん(16)、箱タワーの上に置くなら箱 3 段ぶん(120)とさらに半径ぶんだけ groundTop から引きます。

ブタは地面の上に 2 匹(y=384)と、箱タワーの上に 1 匹(y=264)

図: 横位置は 460・560・660。groundTop から引く量だけで、高さが決まる。

箱タワーの手前・上・奥に、灰色のブタが3匹置かれます。鳥をぶつけると押して動かせますが、 まだ消えません。次の節で、当たったブタが消えるようにします。

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

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

Section titled “この節を終えたときのディレクトリ”
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 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,
});
}
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'));
}
}
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>12 標的(ブタ)を置く</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="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],
});
この節を終えたときのファイル