bird:
(function( w ) {
/*
* constructor { Bird } 鳥構造函數(shù)
* param { ctx: Context } 繪圖環(huán)境
* param { img: Image } 鳥圖
* param { widthFrame: number } 一排的幀數(shù)
* param { heightFrame: number } 一列的幀數(shù)
* param { x: number } 鳥的起始x軸坐標
* param { y: number } 鳥的起始y軸坐標
* */
function Bird( ctx, img, widthFrame, heightFrame, x, y ) {
this.ctx = ctx;
this.img = img;
this.widthFrame = widthFrame;
this.heightFrame = heightFrame;
this.x = x;
this.y = y;
// 一個小鳥的寬和高
this.width = this.img.width / this.widthFrame;
this.height = this.img.height / this.heightFrame;
// 當前小鳥渲染的幀數(shù)
this.currentFrame = 0;
// 小鳥的下落速度
this.speed = 2;
// 加速度
this.speedPlus = 0.05;
// 綁定事件
this._bind();
}
// 給原型擴展方法
Bird.prototype = {
constructor: Bird,
// 繪制鳥
draw: function() {
// 當下落速度為1的時候,旋轉10度
var baseRadian = Math.PI / 180 * 10;
var maxRadian = Math.PI / 180 * 45;
// 根據速度計算旋轉的弧度
var rotateRadian = baseRadian * this.speed;
// 限制最大旋轉角度為70度
rotateRadian = rotateRadian >= maxRadian? maxRadian : rotateRadian;
this.ctx.save();
/*
* 1烈掠、平移到小鳥的中心點
* 2羞秤、然后根據下落的速度旋轉坐標系
* 3、繪制小鳥左敌,但是繪制的x和y坐標變?yōu)樨摰膶捀咭话搿? * */
this.ctx.translate( this.x + this.width / 2, this.y + this.height / 2 );
this.ctx.rotate( rotateRadian );
this.ctx.drawImage( this.img,
this.width * this.currentFrame, 0, this.width, this.height,
-this.width / 2, -this.height / 2, this.width, this.height );
this.ctx.restore();
},
// 計算下一幀繪制時的數(shù)據
update: function() {
// 繪制下一幀
this.currentFrame = ++this.currentFrame >= this.widthFrame? 0 : this.currentFrame;
// 讓小鳥不斷下落
this.y += this.speed;
// 刷新下落數(shù)度
this.speed += this.speedPlus;
},
// 綁定事件
_bind: function() {
var self = this;
this.ctx.canvas.addEventListener( 'click', function() {
self.speed = -1.5;
} );
}
}
// 用來存儲已經創(chuàng)建好的鳥實例對象
var bird = null;
// 工廠模式
w.getBird = function( ctx, img, widthFrame, heightFrame, x, y ) {
// 單利模式,整個游戲只要一個bird就夠了
if ( !bird ) {
bird = new Bird( ctx, img, widthFrame, heightFrame, x, y );
}
return bird;
};
}( window ));
land:
(function( w ) {
/*
* constrcutor { Land } 大地
* param { ctx: Context } 繪圖環(huán)境
* param { img: Image } 繪制的圖像資源
* param { speed: number } 速度
* */
function Land( ctx, img, speed ) {
this.ctx = ctx;
this.img = img;
this.speed = speed || 2;
Land.len++;
this.x = this.img.width * ( Land.len - 1 );
this.y = this.ctx.canvas.height - this.img.height;
}
// 大地實例的數(shù)量
Land.len = 0;
util.extend( Land.prototype, {
draw: function() {
this.ctx.drawImage( this.img, this.x, this.y );
},
update: function() {
this.x -= this.speed;
this.x += this.x <= -this.img.width? this.img.width * Land.len: 0;
}
} );
// 工廠模式
w.getLand = function( ctx, img, speed ) {
return new Land( ctx, img, speed );
};
}( window ));
pipe:
(function( w ) {
/*
* 管道的特點:
* 1瘾蛋、成對出現(xiàn),所以x軸可以共享矫限,但是y軸不共享
* 2哺哼、上下管道之間的路徑固定,可以由用戶指定
* 3叼风、管道的高度是隨機生成的取董,隨機生成上管道的高度,下管道就可以計算了
* 4无宿、當管道走出畫布茵汰,從右邊出來時,高度需要重新隨機生成
* */
/*
* constructor { Pipe } 管道
* param { ctx: Context } 繪圖環(huán)境
* param { imgDown:Image } 口朝下的管道孽鸡,在畫布的上面
* param { imgUp:Image } 口朝上的管道蹂午,在畫布的下面
* param { space:number } 上下管道的間距
* param { landHeight:number } 大地的高度
* param { speed:number } 速度
* */
function Pipe( ctx, imgDown, imgUp, space, landHeight, speed ) {
this.ctx = ctx;
this.imgDown = imgDown;
this.imgUp = imgUp;
this.space = space;
this.landHeight = landHeight;
this.speed = speed;
// 管道最小高度
this.minHeight = 100;
// 管道默認的寬高
this.width = this.imgDown.width;
this.height = this.imgDown.height;
Pipe.len++;
this.x = 300 + this.width * 3 * ( Pipe.len - 1 );
this.y = 0;
// 初始化管道的坐標
this._init();
}
// 管道實例的數(shù)量
Pipe.len = 0;
// 擴展原型方法
util.extend( Pipe.prototype, {
// 初始化管道的坐標
_init: function() {
// 單個管道的最大高度
var maxHeight = this.ctx.canvas.height - this.landHeight - this.space - this.minHeight;
// 隨機生成上管道的高度在 50 到 maxHeight 之間
var randomHeight = Math.random() * maxHeight;
randomHeight = randomHeight < this.minHeight? this.minHeight : randomHeight;
// 上面管道的y軸坐標 = 隨機生成的高度 - 管道默認的高度
this.downY = randomHeight - this.height;
// 下面管道的y軸坐標 = 隨機生成的高度 + 上下管道的間隔
this.upY = randomHeight + this.space;
},
// 繪制管道
draw: function() {
this.ctx.drawImage( this.imgDown, this.x, this.downY );
this.ctx.drawImage( this.imgUp, this.x, this.upY );
this._drawPath();
},
// 根據管道的寬高和坐標繪制對應的路徑
_drawPath: function() {
this.ctx.rect( this.x, this.downY, this.width, this.height );
this.ctx.rect( this.x, this.upY, this.width, this.height );
},
// 更新下一幀的數(shù)據
update: function() {
this.x -= this.speed;
// 管道走出畫布,向右拼接梭灿,同時重新生成高度
if ( this.x <= -this.width ) {
this._init();
this.x += this.width * 3 * Pipe.len;
}
}
} );
// 工廠模式
w.getPipe = function( ctx, imgDown, imgUp, space, landHeight, speed ) {
return new Pipe( ctx, imgDown, imgUp, space, landHeight, speed );
};
}( window ));
sky:
(function( w ) {
var cvs = document.getElementById('cvs');
var ctx = cvs.getContext('2d');
/*
* 繪制背景
* construcotor { Sky } 背景構造函數(shù)
* parasm { ctx: Context } 繪制環(huán)境
* parasm { img: Image } 背景圖像
* parasm { speed: number } 背景速度
* */
function Sky( ctx, img, speed ) {
this.ctx = ctx;
this.img = img;
this.width = this.img.width;
this.height = this.img.height;
this.speed = speed || 2;
// 每創(chuàng)建一個實例画侣,len自增
Sky.len++;
this.x = this.width * ( Sky.len - 1 );
this.y = 0;
}
// 背景實例的個數(shù)
Sky.len = 0;
// 給原型擴充方法
Sky.prototype = {
constructor: Sky,
// 繪制背景
draw: function() {
this.ctx.drawImage( this.img, this.x, this.y );
},
update: function() {
this.x -= this.speed;
if ( this.x <= -this.width ) {
this.x += this.width * Sky.len;
}
}
};
w.getSky = function( ctx, img, speed ) {
return new Sky( ctx, img, speed );
};
}( window ));