代码拉取完成,页面将自动刷新
class Game {
constructor(paramsJSON) {
//FPS
this.fps = paramsJSON.fps || 60;
//定时器
this.timer = null;
//我的帧工具
this.frameUtil = new FrameUtil();
//得到canvas
/** @type {HTMLCanvasElement} */
this.canvas = document.getElementById(paramsJSON.canvasId);
this.canvas.width = document.documentElement.clientWidth
this.canvas.height = document.documentElement.clientHeight
//得到画笔
this.pen = this.canvas.getContext("2d");
//所有图片
this.images = null;
//音频
this.audio = document.getElementsByTagName("audio")[0];
//静态资源
this.staticResource = new StaticResoursesUtil();
this.staticResource.loadImage("r.json", (alreadyLoadNum, allNum, imgsObj) => {
//清屏
// console.log(alreadyLoadNum);
this.pen.clearRect(0, 0, this.canvas.width, this.canvas.height);
//打印已加载图片个数
this.pen.font = "20px Arial"
this.pen.fillText(`正在加载 ${alreadyLoadNum} of ${allNum}`, 30, 50);
if (alreadyLoadNum === allNum) {
this.images = imgsObj;
this.run();
}
})
}
//开始游戏
run() {
//定时器
this.timer = setInterval(() => {
this.mainloop();
}, 1000 / this.fps);
//自己的一些游戏要素
//自己的地板
// console.log(this.images);
this.floor = new Background({
image: this.images.floor,
width: 192,
height: 78,
speed: 2,
y: this.canvas.height - 78
});
//实例化一个鸟
this.bird = new Bird();
//实例化一个管子数组
this.pipeArr = [];
//分数出来
this.scoreManager = new ScoreManager();
}
//主循环
mainloop() {
this.frameUtil.update();
// console.log(this.frameUtil.FPS);
//清屏
this.pen.clearRect(0, 0, this.canvas.width, this.canvas.height);
//打印FPS
this.pen.font = "12px 微软雅黑"
this.pen.fillText(`FPS / ${this.frameUtil.FPS}`, 10, 20)
//打印针编号
this.pen.fillText(`FNO / ${this.frameUtil.currentFrame}`, 10, 40)
//渲染地板
this.floor.update();
this.floor.render();
//渲染鸟
this.bird.update();
this.bird.render();
//渲染更新管子
if (this.frameUtil.currentFrame % 70 === 0) {
if (this.pipeArr.length === 0) {
this.pipeArr.push(new Pipe());
} else {
let nextType = +!this.pipeArr[this.pipeArr.length - 1].type
this.pipeArr.push(new Pipe(nextType));
}
}
this.pipeArr.forEach(element => {
element.update();
element.render();
});
//渲染分数
this.scoreManager.update();
this.scoreManager.render();
}
//暂停
suspend() {
clearInterval(this.timer);
}
//重新开始
restart() {
new Restart;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。