鳥・ブタ・箱を画像にする
ここまでゲームの中身は完成しています。ここからは見た目と音で仕上げます。この節では、鳥・ブタ・箱を
丸や四角から絵文字の画像に置き換えます。画像は 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/に画像を追加
前の節を終えていない人は、ここから始められます。
画像を読み込む
Section titled “画像を読み込む”preload(ゲーム開始前に画像を読み込む場所)を足します。
箱・ブタ・鳥を画像に置き換える変更(下の「丸・四角を画像に置き換える」)も、下の差分に入っています。
scenes/game-scene.js を変更する
Section titled “scenes/game-scene.js を変更する”33 super('Game');44}55
6preload() {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
613create() {714 const groundX = 360;815 const groundY = 440;2431const towerX = 560;2532for (let i = 0; i < 3; i++) {2633 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.add35 .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 });3041}3142
3243const pigRadius = 16;3647 { x: 660, y: groundTop - pigRadius }, // 奥のブタ3748];3849for (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.add51 .image(pos.x, pos.y, 'pig')52 .setDisplaySize(pigRadius * 2, pigRadius * 2);4153 this.matter.add.gameObject(pig, {4254 shape: { type: 'circle', radius: pigRadius },4355 restitution: 0.2,95107const spawnBird = () => {96108 if (birdsLeft <= 0) return;97 bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);98 bird.setStrokeStyle(3, 0x333333);109 bird = this.add110 .image(anchor.x, anchor.y, 'bird')111 .setDisplaySize(birdRadius * 2, birdRadius * 2);99112 this.matter.add.gameObject(bird, {100113 shape: { type: 'circle', radius: birdRadius },101114 restitution: 0.2,preload()…createの前に呼ばれ、画像や音を読み込むための場所です。this.load.image('bird', 'assets/1f426.png')…'bird'という名前で画像を読み込みます。以降この名前で使えます。
丸・四角を画像に置き換える
Section titled “丸・四角を画像に置き換える”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で止まります。
素材のライセンス
Section titled “素材のライセンス”使う絵文字画像は Twemoji(グラフィックは CC-BY 4.0)です。
assets/ に CREDITS.txt と LICENSE.txt を置いて、出どころとライセンスを明記しています。
丸や四角だったものが、鳥・ブタ・箱の絵になりました。ぐっとゲームらしくなります。 次の節で、効果音をつけます。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”(画像などの、文字ではないファイルです。ZIP には入っています)(画像などの、文字ではないファイルです。ZIP には入っています)(画像などの、文字ではないファイルです。ZIP には入っています)画像素材のクレジット=====================
このフォルダの絵文字画像(1f426.png / 1f437.png / 1f4e6.png)はTwemoji を使用しています。
- 提供元: Twemoji (https://github.com/jdecked/twemoji) ※ もとは Twitter, Inc. とその貢献者による Twemoji- グラフィックのライセンス: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)- 使用した絵文字: - 1f426.png … 🐦 鳥(プレイヤーが飛ばす弾) - 1f437.png … 🐷 ブタ(標的) - 1f4e6.png … 📦 箱(構造物)
CC-BY 4.0 の条件により、上記の帰属表示(クレジット)を残しています。詳しいライセンス条文は LICENSE.txt を参照してください。Twemoji graphics================
Copyright Twitter, Inc and other contributors.Maintained fork: https://github.com/jdecked/twemoji
Graphics are licensed under CC-BY 4.0:https://creativecommons.org/licenses/by/4.0/
You are free to share and adapt these graphics for any purpose,provided that you give appropriate credit (see CREDITS.txt).class ClearScene extends Phaser.Scene { constructor() { super('Clear'); }
create(data) { const score = data.score || 0;
this.add .text(360, 180, 'クリア!', { fontSize: '40px', color: '#2e7d32', fontStyle: 'bold', }) .setOrigin(0.5);
this.add .text(360, 250, 'スコア: ' + score, { fontSize: '28px', color: '#333333', }) .setOrigin(0.5);
this.add .text(360, 320, 'クリックでスタートに戻る', { fontSize: '18px', color: '#555555', }) .setOrigin(0.5);
this.input.once('pointerdown', () => this.scene.start('Start')); }}class GameScene extends Phaser.Scene { constructor() { super('Game'); }
preload() { // 素材は twemoji の絵文字画像。 this.load.image('bird', 'assets/1f426.png'); this.load.image('pig', 'assets/1f437.png'); this.load.image('block', 'assets/1f4e6.png'); }
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 .image(towerX, boxY, 'block') .setDisplaySize(boxSize, boxSize); this.matter.add.gameObject(box, { shape: { type: 'rectangle', width: boxSize, height: boxSize }, 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 .image(pos.x, pos.y, 'pig') .setDisplaySize(pigRadius * 2, pigRadius * 2); this.matter.add.gameObject(pig, { shape: { type: 'circle', radius: pigRadius }, restitution: 0.2, }); pig.isPig = true; // 衝突したときに見分けるための目印。 }
this.pigsLeft = pigPositions.length; this.cleared = false;
this.score = 0; this.scoreText = this.add .text(700, 30, 'スコア: 0', { fontSize: '20px', color: '#333333' }) .setOrigin(1, 0.5);
// 衝突中に消すと不安定なので、ためて update でまとめて消す。 this.pendingRemoval = new Set();
this.matter.world.on('collisionstart', (event) => { for (const pair of event.pairs) { const gameObjectA = pair.bodyA.gameObject; const gameObjectB = pair.bodyB.gameObject; if (!gameObjectA || !gameObjectB) continue; if (gameObjectA.isBird && gameObjectB.isPig) this.pendingRemoval.add(gameObjectB); if (gameObjectB.isBird && gameObjectA.isPig) this.pendingRemoval.add(gameObjectA); } });
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 .image(anchor.x, anchor.y, 'bird') .setDisplaySize(birdRadius * 2, birdRadius * 2); this.matter.add.gameObject(bird, { shape: { type: 'circle', radius: birdRadius }, restitution: 0.2, }); // 待機中は動かないように静的にしておく。 bird.setStatic(true); bird.isBird = 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', { score: this.score }); } }); }); }
update() { for (const pig of this.pendingRemoval) { pig.destroy(); this.pigsLeft -= 1; this.score += 1000; this.scoreText.setText('スコア: ' + this.score); } this.pendingRemoval.clear();
// 何度も遷移しないよう、クリアは1回だけ。 if (!this.cleared && this.pigsLeft <= 0) { this.cleared = true; this.scene.start('Clear', { score: this.score }); } }}class GameOverScene extends Phaser.Scene { constructor() { super('GameOver'); }
create(data) { const score = data.score || 0;
this.add .text(360, 180, 'ゲームオーバー', { fontSize: '36px', color: '#c0392b', fontStyle: 'bold', }) .setOrigin(0.5);
this.add .text(360, 250, 'スコア: ' + score, { fontSize: '28px', color: '#333333', }) .setOrigin(0.5);
this.add .text(360, 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(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>17 鳥・ブタ・箱を画像にする</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="scenes/clear-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, ClearScene],});