1 Star 0 Fork 0

余赟昊 / Flappy-polimin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Game.js 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
余赟昊 提交于 2020-11-17 09:57 . 基本全部完成,修复若干bug
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;
}
}
1
https://gitee.com/yu_yunhao/flappy-polimin.git
git@gitee.com:yu_yunhao/flappy-polimin.git
yu_yunhao
flappy-polimin
Flappy-polimin
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891