Commit 5a6b1cd8 authored by 谌继荃's avatar 谌继荃

管道增加随机高度,分数层级放置第一层

parent 6c0ccd04
......@@ -47,7 +47,7 @@ export default class Background {
score++;
// console.log("scoreClass", scoreClass.change );
scoreClass.change(score);
console.log("score", score);
// console.log("score", score);
}
if (bg.x > width) {
bg.x = bg2.x - width;
......
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
class BirdItem extends Movable {
constructor(stage) {
super();
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/0bcb2c26a85addb1714b4c63f2a873aafe210749.png"
);
this.position.set(300, 0);
this.velocity = new Vector2(0, 0);
this.acceleration = new Vector2(0, 1);
}
getCanRemove() {
return false;
}
}
export default class Bird {
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
var bird = new BirdItem(stage);
movableManager.add(bird);
stage.addChild(bird);
stage.addEventListener(FYGE.Event.ENTER_FRAME, () => {});
}
}
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
export default class Enemy extends Movable {
movableManager: MovableManager;
constructor(movableManager: MovableManager) {
super();
this.movableManager = movableManager;
movableManager.add(this);
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/9fe8d613157bddffdb2bf5ca9a748e8fbb7e9471.png"
);
this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {});
}
}
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
export default class Pipe extends Movable {
movableManager: MovableManager;
constructor(movableManager: MovableManager) {
super();
this.movableManager = movableManager;
movableManager.add(this);
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/9fe8d613157bddffdb2bf5ca9a748e8fbb7e9471.png"
);
this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {});
}
}
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import Enemy from "./Enemy";
export default class EnemyFactory {
private _timer;
private _stage: FYGE.Stage;
movableManager: MovableManager;
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
this.movableManager = movableManager;
this._stage = stage;
this._timer = setInterval(this.onTimer, 1000);
}
onTimer = () => {
const enemy1 = this._stage.addChild(new Enemy(this.movableManager));
const enemy2 = this._stage.addChild(new Enemy(this.movableManager));
enemy1.position.set(750, 0);
enemy2.position.set(750, 1060);
enemy1.velocity = new Vector2(-10, 0);
enemy2.velocity = new Vector2(-10, 0);
};
destroy() {
clearInterval(this._timer);
}
}
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import pipe from "./Pipe";
export default class PipeFactory {
private _timer;
private _stage: FYGE.Stage;
movableManager: MovableManager;
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
this.movableManager = movableManager;
this._stage = stage;
this._timer = setInterval(this.onTimer, 1000);
}
onTimer = () => {
const pipe1 = this._stage.addChildAt(
new pipe(this.movableManager),
this._stage.children.length - 2
);
const pipe2 = this._stage.addChildAt(
new pipe(this.movableManager),
this._stage.children.length - 2
);
// 管道高度420
pipe1.height = Math.random() * 100 + 400;
pipe2.height = Math.random() * 100 + 400;
pipe1.position.set(750, 0);
pipe2.position.set(750, this._stage.stageHeight - pipe2.height);
pipe1.velocity = new Vector2(-10, 0);
pipe2.velocity = new Vector2(-10, 0);
console.log(pipe1.height);
};
destroy() {
clearInterval(this._timer);
}
}
......@@ -3,25 +3,24 @@ import Vector2 from "../lib/Vector2";
import Background from "./Background";
import Bird from "./Bird";
import Score from "../tools/Score";
import EnemyFactory from "./EnemyFactory";
import EnemyFactory from "./PipeFactory";
export function addGame(stage: FYGE.Stage) {
const movableManager = new MovableManager(stage); //创建管理器
const background = new Background(stage, movableManager);
const bird = stage.addChild(new Bird()); //有加速度
// const bird = new Bird(stage, movableManager); //有加速度
bird.position.set(300, 500);
bird.velocity = new Vector2(0, 0);
bird.acceleration = new Vector2(0, 1);
const enemyFactory = new EnemyFactory(stage, movableManager);
movableManager.add(bird);
// 当前分数
const score = new Score(stage);
function onGamOver() {
console.log("onGamOver");
// alert(`gameOver 您获得的分数为${movableManager.Score}`);
......
import Enemy from "../bird/Enemy";
import Pipe from "../bird/Pipe";
import Bird from "../bird/Bird";
import Movable from "./Movable";
import ScoreClass from "../tools/Score";
let scoreClass;
let birdClass;
export default class MovableManager extends FYGE.EventDispatcher {
private _stage: FYGE.Stage;
public Score: 0;
bird: Bird;
private _movableList: Movable[] = [];
constructor(stage: FYGE.Stage) {
super();
this._stage = stage;
this.Score = 0;
scoreClass = new ScoreClass(stage);
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
}
onEnterFrame = () => {
this.step();
this.checkBirdHeight();
this.checkEnemy();
this.checkPipe();
this.checkHitByBird();
};
......@@ -33,38 +27,37 @@ export default class MovableManager extends FYGE.EventDispatcher {
window.dispatchEvent(new Event("gameover"));
}
// 小鸟碰到顶部和底部 死亡
checkBirdHeight() {
const BirdList = this._movableList.filter((i) => i instanceof Bird);
const bird = BirdList[0];
console.log("移动对象bird:", bird.y);
// console.log("移动对象bird:", bird.y);
if (bird.y > this._stage.stageHeight - bird.height || bird.y <= 0) {
console.log("死亡");
this.gameOver();
}
}
// 检查管道 回收对象
checkEnemy() {
const EnemyList = this._movableList.filter((i) => i instanceof Enemy);
console.log("移动对象EnemyList:", EnemyList);
EnemyList.forEach((item) => {
// 检查管道 移除屏幕就回收对象
checkPipe() {
const PipeList = this._movableList.filter((i) => i instanceof Pipe);
// console.log("移动对象PipeList:", PipeList);
PipeList.forEach((item) => {
if (item.x <= 0) {
this.remove(item);
}
});
console.log("this._movableList", this._movableList);
// console.log("this._movableList", this._movableList);
}
// 小鸟和管道的碰撞
checkHitByBird() {
const enemyList = this._movableList.filter((i) => i instanceof Enemy);
const PipeList = this._movableList.filter((i) => i instanceof Pipe);
const BirdList = this._movableList.filter((i) => i instanceof Bird);
const bird = BirdList[0];
enemyList.forEach((enemy) => {
if (this.checkHit(bird, enemy)) {
PipeList.forEach((Pipe) => {
if (this.checkHit(bird, Pipe)) {
console.log("被碰撞了,游戏结束了");
this.gameOver();
return;
......@@ -83,7 +76,6 @@ export default class MovableManager extends FYGE.EventDispatcher {
return x0 < x1 && y0 < y1;
}
private step() {
this._movableList.forEach((item) => item.step());
}
......
......@@ -8,7 +8,7 @@ export default class Score {
scoreText.x = 250;
scoreText.y = 30;
scoreText.size = 45;
stage.addChild(scoreText);
stage.addChildAt(scoreText, stage.children.length - 1);
stage.addEventListener("score", this.change);
window.addEventListener("gameover", this.onGamOver);
......@@ -17,7 +17,7 @@ export default class Score {
change(changeScore) {
score = changeScore;
scoreText.text = `当前分数:${score}`;
console.log("change---scoreText.text", scoreText.text);
// console.log("change---scoreText.text", scoreText.text);
}
onGamOver() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment