美文网首页
飞翔的小鸟 三(JS封装小鸟,大地,天空等)

飞翔的小鸟 三(JS封装小鸟,大地,天空等)

作者: 霁雨轩阁 | 来源:发表于2018-09-26 23:49 被阅读0次

bird:
(function( w ) {
/*
* constructor { Bird } 鸟构造函数
* param { ctx: Context } 绘图环境
* param { img: Image } 鸟图
* param { widthFrame: number } 一排的帧数
* param { heightFrame: number } 一列的帧数
* 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;

    // 当前小鸟渲染的帧数
    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坐标变为负的宽高一半。
         * */

        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();
    },

    // 计算下一帧绘制时的数据
    update: function() {

        // 绘制下一帧
        this.currentFrame = ++this.currentFrame >= this.widthFrame? 0 : this.currentFrame;

        // 让小鸟不断下落
        this.y += this.speed;

        // 刷新下落数度
        this.speed += this.speedPlus;

    },

    // 绑定事件
    _bind: function() {

        var self = this;

        this.ctx.canvas.addEventListener( 'click', function() {

            self.speed = -1.5;

        } );
    }
}

// 用来存储已经创建好的鸟实例对象
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 } 绘图环境
* 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;
}

// 大地实例的数量
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、成对出现,所以x轴可以共享,但是y轴不共享
* 2、上下管道之间的路径固定,可以由用户指定
* 3、管道的高度是随机生成的,随机生成上管道的高度,下管道就可以计算了
* 4、当管道走出画布,从右边出来时,高度需要重新随机生成
* */

/*
 * constructor { Pipe } 管道
 * param { ctx: Context } 绘图环境
 * 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();
}

// 管道实例的数量
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 );
    },

    // 更新下一帧的数据
    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 } 背景构造函数
 * parasm { ctx: Context } 绘制环境
 * 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;

    // 每创建一个实例,len自增
    Sky.len++;

    this.x = this.width * ( Sky.len - 1 );
    this.y = 0;
}

// 背景实例的个数
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 ));

相关文章

  • 飞翔的小鸟 三(JS封装小鸟,大地,天空等)

    bird:(function( w ) {/** constructor { Bird } 鸟构造函数* para...

  • 【喜阅说写(感谢)】(九)7.6 吴文静 单菁婧 李彧杰

    树木要感谢大地,因为大地让树木更茂盛 ;小鸟要感谢天 空,因为天空让小鸟自由飞翔 ;鱼要感谢小河,因为河水...

  • 一诗:大家不同,大家都好

    我和小鸟还有铃铛 我张开双臂 却不能像小鸟一样飞翔 飞翔的小鸟也不能像我一样 在大地上奔忙 我摇动身躯 却不能像铃...

  • 小鸟、飞翔、飞翔

    《小鸟、飞翔、飞翔》 从厨房走向回家路上 偏折地倒影 仿佛在与时间逆行 夜已是沉默的荒原 它用黑代表着寂静与孤独 ...

  • 小鸟飞翔

    小鸟学习飞,飞向天空。 小鸟的羽毛已经长全,妈妈决定让它学习飞。 这天,妈妈将小鸟叫到跟前,告诉小鸟,说今天要教小...

  • 小鸟飞翔

    太阳暖,花儿香 欢乐的节日歌声嘹亮 蒲公英的种子无暇的梦 小鸟向蓝天飞翔飞翔

  • 飞翔的小鸟

    走在小区内,听到树上小鸟清翠的鸣叫声。向树上一看,有两只小鸟在互相鸣叫着,两只鸟头上尖尖的羽毛。我挥下手,一只飞到...

  • 飞翔的小鸟

    今天是周六。小红还有他的哥哥还有爸爸妈妈带她去公园玩儿。公园里有蓝色的小鸟,有粉色的小鸟,公园里有各种各样的颜色小...

  • 飞翔的小鸟

    我想分享又不知道分享给谁这是一种空落落的感觉像飞翔的鸟儿路过大海,无处落脚只能哀伤地鸣叫几声算是和天空说了话和白云...

  • 飞翔的小鸟二 (JS封装的场景和加载器)

    JS封装的对象:(1)util:var util = {extend: function ( o1, o2 ) {...

网友评论

      本文标题:飞翔的小鸟 三(JS封装小鸟,大地,天空等)

      本文链接:https://www.haomeiwen.com/subject/bvrsoftx.html