大鱼吃小鱼游戏-baby.js




var babyObj = function()
{
    this.x;
    this.y;

    this.babyAngle;

    this.babyEye = new Image();
    this.babyBody = new Image();
    this.babyTail = new Image();

    this.babyTailTimer = 0;
    this.babyTailCount = 0;

    this.babyEyeTimer = 0;
    this.babyEyeCount = 0;
    this.babyEyeInterval = 1000;

    this.babyBodyTimer = 0;
    this.babyBodyCount = 0;


}

babyObj.prototype.init = function()
{
    this.x = canWidth * 0.5 - 50;
    this.y = canHeight * 0.5 + 50;
    this.babyAngle = 0;
    //this.babyEye.src  = "./src/bigEye0.png";
    this.babyBody.src = "./src/babyFade0.png";
    //this.babyTail.src = "./src/babyTail0.png";
}

babyObj.prototype.draw = function()
{
    //lerp x, y
    this.x = lerpDistance(mom.x ,this.x, 0.98);
    this.y = lerpDistance(mom.y, this.y, 0.99);
    //lerp angel
    var deltaY = mom.y - this.y;
    var deltaX = mom.x - this.x;
    var beta = Math.atan2(deltaY, deltaX) + Math.PI;//-PI , PI

    //lerp angle
    this.babyAngle = lerpAngle(beta, this.babyAngle, 0.6);  //计算出鱼与鼠标角度

    //baby tail count
    //this.babyTailTimer += deltaTime;
    this.babyTailTimer += 17;
    if(this.babyTailTimer > 50)
    {
        this.babyTailCount = (this.babyTailCount + 1) % 8;
        this.babyTailTimer %= 50;
    }

    //baby eye
    this.babyEyeTimer += 17 ;
    if(this.babyEyeTimer > this.babyEyeInterval)
    {
        this.babyEyeCount = (this.babyEyeCount + 1) % 2;
        this.babyEyeTimer %= this.babyEyeInterval;
        if(this.babyEyeCount == 0)
        {
            this.babyEyeInterval = Math.random() * 1500 + 2000;//[]
        }
        else
        {
            this.babyEyeInterval = 200;
        }
    }

    //baby body
    this.babyBodyTimer += 17;
    if(this.babyBodyTimer > 300)
    {
        this.babyBodyCount = this.babyBodyCount +1 ;
        this.babyBodyTimer %= 300;  //让小鱼匀速地变白,防止小鱼快速变白
        if(this.babyBodyCount > 19 )
        {
            this.babyBodyCount = 19;
            data.gameOver = true;
            // game over
        }
    }

    //console.log(deltaTime);
    //console.log(this.babyTailCount);

    //ctx1
    ctx1.save();
    //translate()
    ctx1.translate(this.x, this.y);     //把鼠标原点移到x,y位置
    ctx1.rotate(this.babyAngle);        //鱼跟随大鱼转动
    var babyTailCount = this.babyTailCount;
    ctx1.drawImage(babyTail[babyTailCount], - babyTail[babyTailCount].width * 0.5 +23, - babyTail[babyTailCount].height * 0.5);
    var babyBodyCount =this.babyBodyCount;
    ctx1.drawImage(babyBody[babyBodyCount], - babyBody[babyBodyCount].width * 0.5, - babyBody[babyBodyCount].height * 0.5);
    var babyEyeCount =this.babyEyeCount;
    ctx1.drawImage(babyEye[babyEyeCount], - babyEye[babyEyeCount].width * 0.5, - babyEye[babyEyeCount].height * 0.5);

    ctx1.restore();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值