О чем этот пример
Этот финальный шаг в создании первой игры на Phaser превращает простой прототип в полноценный аркадный опыт. Мы добавим систему сбора предметов, счёт, опасных врагов и логику завершения игры. Понимание этих принципов — ключ к созданию увлекательных игровых циклов, где у игрока есть чёткая цель, растущая сложность и последствия от ошибок.
Версия Phaser: код и демо в этой статье рассчитаны на Phaser 3.90.0.
Живой запуск
Ниже встроен рабочий билд примера. Оригинальный источник: GitHub.
Исходный код
class Example extends Phaser.Scene
{
scoreText;
gameOver = false;
score = 0;
cursors;
platforms;
bombs;
stars;
player;
preload ()
{
// this.load.setBaseURL('https://cdn.phaserfiles.com/v385');
this.load.image('sky', 'src/games/firstgame/assets/sky.png');
this.load.image('ground', 'src/games/firstgame/assets/platform.png');
this.load.image('star', 'src/games/firstgame/assets/star.png');
this.load.image('bomb', 'src/games/firstgame/assets/bomb.png');
this.load.spritesheet('dude', 'src/games/firstgame/assets/dude.png', { frameWidth: 32, frameHeight: 48 });
}
create ()
{
// A simple background for our game
this.add.image(400, 300, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
this.platforms = this.physics.add.staticGroup();
// Here we create the ground.
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();
// Now let's create some ledges
this.platforms.create(600, 400, 'ground');
this.platforms.create(50, 250, 'ground');
this.platforms.create(750, 220, 'ground');
// The player and its settings
this.player = this.physics.add.sprite(100, 450, 'dude');
// Player physics properties. Give the little guy a slight bounce.
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
// Our player animations, turning, walking left and walking right.
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'dude', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
// Input Events
this.cursors = this.input.keyboard.createCursorKeys();
// Some stars to collect, 12 in total, evenly spaced 70 pixels apart along the x axis
this.stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 }
});
this.stars.children.forEach(child =>
{
// Give each star a slightly different bounce
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.bombs = this.physics.add.group();
// The score
this.scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });
// Collide the player and the stars with the platforms
this.physics.add.collider(this.player, this.platforms);
this.physics.add.collider(this.stars, this.platforms);
this.physics.add.collider(this.bombs, this.platforms);
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this);
this.physics.add.collider(this.player, this.bombs, this.hitBomb, null, this);
}
update ()
{
if (this.gameOver)
{
return;
}
if (this.cursors.left.isDown)
{
this.player.setVelocityX(-160);
this.player.anims.play('left', true);
}
else if (this.cursors.right.isDown)
{
this.player.setVelocityX(160);
this.player.anims.play('right', true);
}
else
{
this.player.setVelocityX(0);
this.player.anims.play('turn');
}
if (this.cursors.up.isDown && this.player.body.touching.down)
{
this.player.setVelocityY(-330);
}
}
collectStar (player, star)
{
star.disableBody(true, true);
// Add and update the score
this.score += 10;
this.scoreText.setText(`Score: ${this.score}`);
if (this.stars.countActive(true) === 0)
{
// A new batch of stars to collect
this.stars.children.forEach(child =>
{
child.enableBody(true, child.x, 0, true, true);
});
const x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
const bomb = this.bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
}
}
hitBomb (player, bomb)
{
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('turn');
this.gameOver = true;
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: Example
};
const game = new Phaser.Game(config);
Инициализация игрового мира и персонажа
В методе create() загружается фон, создаются статические платформы с помощью this.physics.add.staticGroup() и размещаются в сцене. Персонаж (player) добавляется как физический спрайт с настройками отскока и ограничениями по границам мира.
Важным этапом является создание анимаций из спрайтшита. Phaser позволяет определить анимации для движения влево, вправо и состояние покоя, которые будут проигрываться в зависимости от действий игрока.
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
Создание собираемых предметов и врагов
Звёзды, которые нужно собирать, создаются группой this.physics.add.group. Использование метода setXY с параметром stepX — это удобный способ равномерно распределить 12 объектов по горизонтали.
Каждой звезде задаётся случайный отскок по оси Y, чтобы движение выглядело более живым и разнообразным. Группа бомб (bombs) создаётся пустой — враги будут появляться динамически в процессе игры.
this.stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 }
});
this.stars.children.forEach(child => {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.bombs = this.physics.add.group();
Настройка физических взаимодействий (Коллизии и оверлапы)
Phaser Arcade Physics различает два основных типа взаимодействий: collider и overlap. Коллайдер останавливает объекты при столкновении, что используется для персонажа, платформ, звёзд и бомб. Оверлап регистрирует факт пересечения объектов, не влияя на их физику, что идеально для сбора предметов.
В этом коде коллайдеры предотвращают падение сквозь платформы. Оверлапы между игроком и звёздами, а также игроком и бомбами, запускают callback-функции collectStar и hitBomb.
this.physics.add.collider(this.player, this.platforms);
this.physics.add.collider(this.stars, this.platforms);
this.physics.add.collider(this.bombs, this.platforms);
this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this);
this.physics.add.collider(this.player, this.bombs, this.hitBomb, null, this);
Логика управления и обновления состояния в update()
Метод update() вызывается каждый кадр. Здесь обрабатывается ввод с клавиатуры (this.cursors), управляющий движением и прыжком персонажа. Ключевая проверка this.player.body.touching.down гарантирует, что прыжок возможен только когда персонаж стоит на поверхности.
Анимации переключаются в зависимости от направления скорости по оси X. Если клавиши не нажаты, скорость сбрасывается, и проигрывается анимация покоя.
if (this.cursors.left.isDown) {
this.player.setVelocityX(-160);
this.player.anims.play('left', true);
}
else if (this.cursors.right.isDown) {
this.player.setVelocityX(160);
this.player.anims.play('right', true);
}
if (this.cursors.up.isDown && this.player.body.touching.down) {
this.player.setVelocityY(-330);
}
Механика сбора и увеличения сложности (collectStar)
Функция collectStar вызывается при сборе звезды. Она "отключает" звезду (star.disableBody), увеличивает счёт и обновляет текстовое поле. Основная игровая механика реализована в условии: когда активных звёзд не осталось (this.stars.countActive(true) === 0), игра реанимирует все звёзды и создаёт новую бомбу.
Бомбе задаётся случайная начальная позиция (в противоположной от игрока половине экрана), отскок и случайная скорость. Это создаёт элемент неожиданности и постепенно увеличивает сложность игрового пространства.
if (this.stars.countActive(true) === 0) {
this.stars.children.forEach(child => {
child.enableBody(true, child.x, 0, true, true);
});
const x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
const bomb = this.bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
}
Обработка поражения (hitBomb)
Столкновение с бомбой приводит к завершению игры. Функция hitBomb приостанавливает всю физику сцены через this.physics.pause(), визуально сигнализирует о поражении, окрашивая персонажа в красный оттенок (setTint), и устанавливает флаг gameOver = true. Этот флаг в update() прерывает дальнейшую обработку управления, останавливая игру.
hitBomb (player, bomb) {
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('turn');
this.gameOver = true;
}
Что попробовать дальше
Вы создали базовую, но полноценную аркадную игру с циклом "сбор-награда-опасность". Для экспериментов попробуйте: изменить физические свойства бомб (гравитацию, трение); добавить разные типы собираемых предметов с уникальными эффектами; реализовать систему жизней вместо мгновенного поражения; или создать несколько волн противников с увеличивающейся сложностью.
