flappy bird js 源码

素材

注入灵魂

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #canvas{
            background-image: url(res/sky.png);
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="800" height="400"></canvas>
    <div id="res"></div>
</body>
    <script>

        var canvas = document.getElementById('canvas')
        var context = canvas.getContext('2d')

        // 创建一个图片
        var img = new Image();
        img.src = 'res/birds.png'

        var birdX = 200;
        var birdY = 100;
        var birdTime = null;
        var stop = 0
        img.onload = function () {
            if(birdTime === null && stop === 0) {
                birdTime = setInterval(function(){

                    if (birdY <= 350) {
                        birdY++
                    }

                    context.clearRect(0,0,800,400)
                    drawColumn ()
                    context.drawImage(img,0,0,52,45,birdX,birdY,52,45);
                },10)
            }
        }
        var click = null
        document.onmouseup = function () {
            if (stop === 0) {
                context.drawImage(img,0,0,52,45,birdX,birdY,52,45);
                clearInterval(click)
            }
            
        }
        document.onmousedown = function () {
            if (stop === 0) {
                click  = setInterval(function() { 
                    context.drawImage(img,104,0,52,45,birdX,birdY,52,45);
                    birdY = birdY-2
                },10)
                
            }
        }


        // 柱子 
        var columnArr = []
        var columnTiner = null

        function createColumn () {


            columnTiner = setInterval(function() {
                var column = {}
                column.postionX = 800
                column.postionY = -Math.round(Math.random()*100 + 150);
                column.imgA = new Image()
                column.imgB = new Image()
                column.imgA.src = 'res/pipe2.png'
                column.imgB.src = 'res/pipe1.png'
                column.id = new Date().getTime()

                columnArr.push(column)
            },1500)
            
        }
        createColumn()

        var same = null;
        var mark = 0
        function drawColumn () {
            for(var i = 0 ; i < columnArr.length; i++) {
                columnArr[i].postionX--
                context.drawImage(columnArr[i].imgA,columnArr[i].postionX,columnArr[i].postionY - 100);

                context.drawImage(columnArr[i].imgB,columnArr[i].postionX,columnArr[i].postionY + 450);
            
                if (birdX + 40 >= columnArr[i].postionX && birdX - 70 <= columnArr[i].postionX) {

                    if (columnArr[i].id != same) {
                        mark++
                        same = columnArr[i].id 

                        document.getElementById('res').innerHTML = "得分" + mark
                    }

                    // 判定碰撞
                    if(birdY < columnArr[i].postionY + 300 || birdY + 35 > columnArr[i].postionY + 450) {

                        clearInterval(columnTiner)
                        clearInterval(birdTime)
                        stop = 1
                        document.getElementById('res').innerHTML = "最后得分" + mark + "刷新页面重新开始"
                    }
                }

            }
        }
    </script>
</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值