コンテンツにスキップ

箱の上に線を引く

箱の上に線を引く

ゲームオーバーの本命ルールは「積み上がったボールが一定の高さ(線)を超えたら負け」です。 この節ではまずその線を描くだけにします。判定は次の節で入れます。

「線を描く」→「線で判定する」を分けるのは、まず見た目で線の位置を確かめてから、ロジックを 足すためです。

今回さわる game/: main.js を書き換え

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

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

GameScene の create() で線の高さ lineY を決めます。箱の上端 top(150)より少し下の 190 あたりにします。

main.js
40
40
wall: 12
41
41
};
42
42
const maxDrops = 10;
43
const lineY = 190;
43
44
44
45
this.dropCount = 0;
main.js
53
54
g.fillStyle(0x3498db, 1);
54
55
g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);
55
56
57
g.lineStyle(2, 0xc0392b, 0.8);
58
g.beginPath();
59
g.moveTo(box.left, lineY);
60
g.lineTo(box.right, lineY);
61
g.strokePath();
62
56
63
const wallH = box.bottom - box.top;
57
64
const wallCY = (box.top + box.bottom) / 2;
58
65
this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });

箱を描いている graphics(g)に、そのまま線を追加します。線は「色と太さを決めて(lineStyle)、 始点へ移動して(moveTo)、終点へ引く(lineTo)、確定する(strokePath)」の順です。

  • lineStyle(太さ, 色, 不透明度) … 線のスタイル。0.8 で少し透けさせています。
  • moveTo / lineTo … 箱の左端から右端まで、lineY の高さで水平に引きます。

箱の上のほうに赤い横線が表示されます。まだ超えても何も起きません(ゲームオーバー条件は 前の節のまま「10 個」です)。次の節で、この線を実際の判定に使います。

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

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

Section titled “この節を終えたときのディレクトリ”
game
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>09 箱の上に線を引く(表示するだけ)</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="main.js" defer></script>
</head>
<body></body>
</html>
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'));
}
}
class GameScene extends Phaser.Scene {
constructor() {
super('Game');
}
create() {
const box = {
left: 60,
right: 340,
top: 150,
bottom: 540,
wall: 12
};
const maxDrops = 10;
const lineY = 190;
this.dropCount = 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, lineY);
g.lineTo(box.right, 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) => {
const radius = 25;
const dropY = 100;
// 箱の内側からはみ出さないよう X を制限する
const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);
const ball = this.add.circle(x, dropY, radius, 0x333333);
this.matter.add.gameObject(ball, {
shape: {
type: 'circle',
radius: radius
},
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
});
this.dropCount += 1;
if (this.dropCount >= maxDrops) {
this.scene.start('GameOver');
}
});
}
}
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'));
}
}
new Phaser.Game({
type: Phaser.AUTO,
width: 400,
height: 600,
backgroundColor: '#fdf6e3',
physics: {
default: 'matter',
matter: { gravity: { y: 1 } },
},
scene: [StartScene, GameScene, GameOverScene],
});
この節を終えたときのファイル