You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.5 KiB
165 lines
4.5 KiB
import Phaser from "phaser";
|
|
import pipeImg from "@/game/game/assets/flipbird/pipe.png";
|
|
import birdImg from "@/game/game/assets/flipbird/clumsy.png";
|
|
import bg from "@/game/game/assets/flipbird/bg.png";
|
|
import ground from "@/game/game/assets/flipbird/ground.png";
|
|
/**
|
|
* 素材再找 https://github.com/ellisonleao/clumsy-bird/blob/master/data/img/new.png
|
|
* https://www.emanueleferonato.com/2019/05/02/flappy-bird-html5-prototype-updated-to-phaser-3-16-2/
|
|
*
|
|
*/
|
|
export default class PlayScene extends Phaser.Scene {
|
|
constructor() {
|
|
super({ key: "flipbird" });
|
|
this.bg = null;
|
|
this.pipeGroup = null;
|
|
this.gameOptions = {
|
|
// bird gravity, will make bird fall if you dont flap
|
|
birdGravity: 800,
|
|
|
|
// horizontal bird speed
|
|
birdSpeed: 120, //125
|
|
|
|
// flap thrust
|
|
birdFlapPower: 300,
|
|
|
|
// minimum pipe height, in pixels. Affects hole position
|
|
minPipeHeight: 50,
|
|
|
|
// distance range from next pipe, in pixels
|
|
pipeDistance: [220, 280],
|
|
|
|
// hole range between pipes, in pixels
|
|
pipeHole: [200, 250], //[100, 130],
|
|
|
|
// local storage object name
|
|
localStorageName: "bestFlappyScore",
|
|
};
|
|
}
|
|
preload() {
|
|
//this.load.image("bird", "bird.png");
|
|
this.load.image("bg", bg);
|
|
this.load.image("pipe", pipeImg);
|
|
this.load.image("ground", ground);
|
|
|
|
this.load.spritesheet("bird", birdImg, {
|
|
frameWidth: 85,
|
|
frameHeight: 60,
|
|
});
|
|
}
|
|
create() {
|
|
this.bg = this.add.image(0, 0, "bg");
|
|
this.bg.setScale(1);
|
|
this.pipeGroup = this.physics.add.group();
|
|
this.pipePool = [];
|
|
for (let i = 0; i < 4; i++) {
|
|
this.pipePool.push(this.pipeGroup.create(0, 0, "pipe"));
|
|
this.pipePool.push(this.pipeGroup.create(0, 0, "pipe"));
|
|
this.placePipes(false);
|
|
}
|
|
this.pipeGroup.setVelocityX(-this.gameOptions.birdSpeed);
|
|
this.bird = this.physics.add.sprite(
|
|
80,
|
|
this.game.config.height / 2,
|
|
"bird"
|
|
);
|
|
this.ground = this.add.tileSprite(
|
|
0,
|
|
this.game.config.height,
|
|
this.game.config.width,
|
|
0,
|
|
"ground"
|
|
);
|
|
this.ground.setOrigin(0);
|
|
|
|
this.bird.body.gravity.y = this.gameOptions.birdGravity;
|
|
|
|
this.anims.create({
|
|
key: "fly",
|
|
frames: this.anims.generateFrameNumbers("bird", { start: 0, end: 3 }),
|
|
frameRate: 10,
|
|
repeat: -1,
|
|
});
|
|
this.input.on("pointerdown", this.flap, this);
|
|
this.score = 0;
|
|
this.scoreText1 = this.add.text(20, 20, "", {
|
|
fontSize: 20,
|
|
});
|
|
this.updateScore(this.score);
|
|
}
|
|
updateScore(inc) {
|
|
this.score += inc;
|
|
this.scoreText1.text = "Game Score: " + this.score;
|
|
}
|
|
placePipes(addScore) {
|
|
let rightmost = this.getRightmostPipe();
|
|
let pipeHoleHeight = Phaser.Math.Between(
|
|
this.gameOptions.pipeHole[0],
|
|
this.gameOptions.pipeHole[1]
|
|
);
|
|
let pipeHolePosition = Phaser.Math.Between(
|
|
this.gameOptions.minPipeHeight + pipeHoleHeight / 2,
|
|
this.game.config.height -
|
|
this.gameOptions.minPipeHeight -
|
|
pipeHoleHeight / 2
|
|
);
|
|
this.pipePool[0].x =
|
|
rightmost +
|
|
this.pipePool[0].getBounds().width +
|
|
Phaser.Math.Between(
|
|
this.gameOptions.pipeDistance[0],
|
|
this.gameOptions.pipeDistance[1]
|
|
);
|
|
this.pipePool[0].y = pipeHolePosition - pipeHoleHeight / 2;
|
|
this.pipePool[0].setOrigin(0, 1);
|
|
this.pipePool[1].x = this.pipePool[0].x;
|
|
this.pipePool[1].y = pipeHolePosition + pipeHoleHeight / 2;
|
|
this.pipePool[1].setOrigin(0, 0);
|
|
this.pipePool[1].flipY = true;
|
|
this.pipePool = [];
|
|
if (addScore) {
|
|
this.updateScore(1);
|
|
}
|
|
}
|
|
flap() {
|
|
this.bird.body.velocity.y = -this.gameOptions.birdFlapPower;
|
|
this.bird.anims.play("fly", true);
|
|
}
|
|
getRightmostPipe() {
|
|
let rightmostPipe = 0;
|
|
this.pipeGroup.getChildren().forEach(function (pipe) {
|
|
rightmostPipe = Math.max(rightmostPipe, pipe.x);
|
|
});
|
|
return rightmostPipe;
|
|
}
|
|
update() {
|
|
this.physics.world.collide(
|
|
this.bird,
|
|
this.pipeGroup,
|
|
function () {
|
|
this.die();
|
|
},
|
|
null,
|
|
this
|
|
);
|
|
if (this.bird.y > this.game.config.height || this.bird.y < 0) {
|
|
this.die();
|
|
}
|
|
this.pipeGroup.getChildren().forEach(function (pipe) {
|
|
if (pipe.getBounds().right < 0) {
|
|
this.pipePool.push(pipe);
|
|
if (this.pipePool.length == 2) {
|
|
this.placePipes(true);
|
|
}
|
|
}
|
|
}, this);
|
|
this.ground.tilePositionX += 2;
|
|
// this.bg.tilePositionY += 0.5
|
|
}
|
|
die() {
|
|
if (this.game.onLose) {
|
|
this.game.onLose();
|
|
this.scene.pause();
|
|
}
|
|
}
|
|
}
|
|
|