飞翔的小鸟

<!DOCTYPE html>

<html>

<head>

    <title>Flappy Bird</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script type="text/javascript">

        // Edit by xingoo

        // Fork on my github:https://github.com/xinghalo/CodeJS/tree/master/HTML5

        var ctx;

        var cwidth = 400;

        var cheight = 600;

        var objects = [];

        var birdIndex = 0;

        var ver1 = 10;

        var ver2;

        var gravity = 2;

        var pipe_height = 200;

        var velocity = 10;

        var tid;

        var score = 0;

        var isScore = false;

        var birds = ["./images/0.gif","./images/1.gif","./images/2.gif"];

        var back = new Background(0,0,400,600,"./images/bg.png");

        var up_pipe = new UpPipe(0,0,100,200,"./images/pipe.png");

        var down_pipe = new DownPipe(0,400,100,200,"./images/pipe.png");

        var ground = new Background(0,550,400,200,"./images/ground.png");

        var bird = new Bird(80,300,40,40,birds);

        objects.push(back);

        objects.push(up_pipe);

        objects.push(down_pipe);

        objects.push(ground);

        objects.push(bird);

        function UpPipe(x,y,width,height,img_src){

            this.px = x;

            this.py = y;

            this.pwidth = width;

            this.pheight = height;

            this.img_src = img_src;

            this.draw = drawUpPipe;

        }

        function DownPipe(x,y,width,height,img_src){

            this.px = x;

            this.py = y;

            this.pwidth = width;

            this.pheight = height;

            this.img_src = img_src;

            this.draw = drawDownPipe;

        }

        function drawUpPipe(){

            var image = new Image();

            image.src = this.img_src;

            ctx.drawImage(image,150,500,150,800,this.px,this.py,this.pwidth,this.pheight);

        }

        function drawDownPipe(){

            var image = new Image();

            image.src = this.img_src;

            ctx.drawImage(image,0,500,150,500,this.px,this.py,this.pwidth,this.pheight);

        }

        function Background(x,y,width,height,img_src){

            this.bgx = x;

            this.bgy = y;

            this.bgwidth = width;

            this.bgheight = height;

            var image = new Image();

            image.src = img_src;

            this.img = image;

            this.draw = drawbg;

        }

        function drawbg(){

            ctx.drawImage(this.img,this.bgx,this.bgy,this.bgwidth,this.bgheight);

        }

        function Bird(x,y,width,height,img_srcs){

            this.bx = x;

            this.by = y;

            this.bwidth = width;

            this.bheight = height;

            this.imgs = img_srcs;

            this.draw = drawbird;

        }

        function drawbird(){

            birdIndex++;

            var image = new Image();

            image.src = this.imgs[birdIndex%3];

            ctx.drawImage(image,this.bx,this.by,this.bwidth,this.bheight);

        }

        function calculator(){

            if(bird.by+bird.bheight>ground.bgy ||

                ((bird.bx+bird.bwidth>up_pipe.px)&&(bird.by>up_pipe.py)&&(bird.bx+bird.bwidth<up_pipe.px+up_pipe.pwidth)&&(    bird.by<up_pipe.py+up_pipe.pheight))||

                ((bird.bx+bird.bwidth>up_pipe.px)&&(bird.by>up_pipe.py)&&(bird.bx+bird.bwidth<up_pipe.px+up_pipe.pwidth)&&(    bird.by<up_pipe.py+up_pipe.pheight))||

                ((bird.bx>down_pipe.px)&&(bird.by>down_pipe.py)&&(bird.bx<down_pipe.px+down_pipe.pwidth)&&(bird.by<down_pipe.py+down_pipe.pheight))||

                ((bird.bx>down_pipe.px)&&(bird.by+bird.bheight>down_pipe.py)&&(bird.bx<down_pipe.px+down_pipe.pwidth)&&(bird.by+bird.bheight<down_pipe.py+down_pipe.pheight))){

                clearInterval(tid);

                ctx.fillStyle = "rgb(255,255,255)";

                ctx.font = "30px Accent";

                ctx.fillText("You got "+score+"!",110,100)

                return;

            }

            ver2 = ver1+gravity;

            bird.by += (ver2+ver1)*0.5;

            if(up_pipe.px+up_pipe.pwidth>0){

                up_pipe.px -= velocity;

                down_pipe.px -= velocity;

            }else{

                up_pipe.px = 400;

                down_pipe.px = 400;

                up_pipe.pheight = 100+Math.random()*200;

                down_pipe.py = up_pipe.pheight+pipe_height;

                down_pipe.pheight = 600-down_pipe.py;

                isScore = true;

            }

            if(isScore && bird.bx>up_pipe.px+up_pipe.pwidth){

                score += 1;

                isScore = false;

                if(score>0 && score%10 === 0){

                    velocity++;

                }

            }

            ctx.fillStyle = "rgb(255,255,255)";

            ctx.font = "30px Accent";

            if(score>0){

                score%10!==0?ctx.fillText(score,180,100):ctx.fillText("Great!"+score,120,100);

            }

        }

        function drawall(){

            ctx.clearRect(0,0,cwidth,cheight);

            var i;

            for(i=0;i<objects.length;i++){

                objects[i].draw();

            }

            calculator();

        }

        function keyup(e){

            var e = e||event;

               var currKey = e.keyCode||e.which||e.charCode;

               switch (currKey){

                case 32:

                    bird.by -= 80;

                    break;

            }

        }    

        function init(){

            ctx = document.getElementById('canvas').getContext('2d');

            document.onkeyup = keyup;

            drawall();

            tid = setInterval(drawall,80);

        }

    </script>

</head>

<body onLoad="init();">

<canvas id="canvas" width="400" height="600" style="margin-left:200px;">

    Your browser is not support canvas! 

</canvas>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值