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

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

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