コンテンツにスキップ

鳥・ブタ・箱を画像にする

鳥・ブタ・箱を画像にする

ここまでゲームの中身は完成しています。ここからは見た目と音で仕上げます。この節では、鳥・ブタ・箱を 丸や四角から絵文字の画像に置き換えます。画像は game/ の中に assets/ フォルダを作って入れます。

画像は Twemoji(CC-BY 4.0)の絵文字です。下のボタンからダウンロードして、解凍してできた assets/ を game/ の中に置いてください(この節を終えたときのファイルの ZIP にも同梱してあります)。

鳥・ブタ・箱の画像をダウンロード

中身(5 ファイル): 1f426.png、1f437.png、1f4e6.png、CREDITS.txt、LICENSE.txt
解凍してできる assets/ を game/ の中に置いてください。

今回さわる game/: scenes/game-scene.js を書き換え、assets/ に画像を追加

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

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

preload(ゲーム開始前に画像を読み込む場所)を足します。 箱・ブタ・鳥を画像に置き換える変更(下の「丸・四角を画像に置き換える」)も、下の差分に入っています。

scenes/game-scene.js
3
3
super('Game');
4
4
}
5
5
6
preload() {
7
// 素材は twemoji の絵文字画像。
8
this.load.image('bird', 'assets/1f426.png');
9
this.load.image('pig', 'assets/1f437.png');
10
this.load.image('block', 'assets/1f4e6.png');
11
}
12
6
13
create() {
7
14
const groundX = 360;
8
15
const groundY = 440;
scenes/game-scene.js
24
31
const towerX = 560;
25
32
for (let i = 0; i < 3; i++) {
26
33
const boxY = groundTop - boxSize / 2 - i * boxSize;
27
const box = this.add.rectangle(towerX, boxY, boxSize, boxSize, 0xdddddd);
28
box.setStrokeStyle(3, 0x333333);
29
this.matter.add.gameObject(box, { restitution: 0.1 });
34
const box = this.add
35
.image(towerX, boxY, 'block')
36
.setDisplaySize(boxSize, boxSize);
37
this.matter.add.gameObject(box, {
38
shape: { type: 'rectangle', width: boxSize, height: boxSize },
39
restitution: 0.1,
40
});
30
41
}
31
42
32
43
const pigRadius = 16;
scenes/game-scene.js
36
47
{ x: 660, y: groundTop - pigRadius }, // 奥のブタ
37
48
];
38
49
for (const pos of pigPositions) {
39
const pig = this.add.circle(pos.x, pos.y, pigRadius, 0xaaaaaa);
40
pig.setStrokeStyle(3, 0x333333);
50
const pig = this.add
51
.image(pos.x, pos.y, 'pig')
52
.setDisplaySize(pigRadius * 2, pigRadius * 2);
41
53
this.matter.add.gameObject(pig, {
42
54
shape: { type: 'circle', radius: pigRadius },
43
55
restitution: 0.2,
scenes/game-scene.js
95
107
const spawnBird = () => {
96
108
if (birdsLeft <= 0) return;
97
bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
98
bird.setStrokeStyle(3, 0x333333);
109
bird = this.add
110
.image(anchor.x, anchor.y, 'bird')
111
.setDisplaySize(birdRadius * 2, birdRadius * 2);
99
112
this.matter.add.gameObject(bird, {
100
113
shape: { type: 'circle', radius: birdRadius },
101
114
restitution: 0.2,
  • preload() … create の前に呼ばれ、画像や音を読み込むための場所です。
  • this.load.image('bird', 'assets/1f426.png') … 'bird' という名前で画像を読み込みます。以降この名前で使えます。

this.add.circle(...) を this.add.image(...) に置き換えます。当たり判定(shape)は今までどおり 円や四角のままにして、見た目だけ画像にします。

差分では、箱を積む for の中、ブタを置く for の中、spawnBird の中の 3 か所を書き換えています。

  • this.add.image(x, y, 'bird') … 画像を置きます。名前は preload でつけた 'bird' などです。
  • setDisplaySize(w, h) … 画像の表示サイズを、これまでの丸・四角と同じ大きさに合わせます。
  • 当たり判定は shape で今までどおり指定するので、見た目が画像でも動きは変わりません。
  • 3か所とも、丸・四角に付けていた setStrokeStyle(...) の行が消えます。輪郭線は図形だけの 機能で画像にはないので、残すと setStrokeStyle is not a function で止まります。

使う絵文字画像は Twemoji(グラフィックは CC-BY 4.0)です。 assets/ に CREDITS.txt と LICENSE.txt を置いて、出どころとライセンスを明記しています。

丸や四角だったものが、鳥・ブタ・箱の絵になりました。ぐっとゲームらしくなります。 次の節で、効果音をつけます。

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

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

Section titled “この節を終えたときのディレクトリ”
この節を終えたときのファイル