像素鸟小游戏

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #game {
            width: 800px;
            height: 600px;
            background: url('./images/sky.png');
            position: relative;
            overflow: hidden;
        }

        #bird {
            width: 34px;
            height: 25px;
            background: url('./images/birds.png') -8px -10px no-repeat;
            position: absolute;
            top: 100px;
            left: 100px;
        }
    </style>
</head>

<body>
    <div id="game">
        <div id="bird"></div>
    </div>
    <script>
        // 让小鸟飞起来
        // 移动的背景
        // top
        // 定时器
        // 动画原理 leader = leader + step
        // 获取相应的元素
        var game = document.getElementById('game');
        var birdEle = document.getElementById('bird');
        // 初始化背景图的值

        var sky = {
            x: 0
        }
        // 初始bird 的值
        var bird = {
            speedX: 5,
            speedY: 0,
            x: birdEle.offsetLeft,
            y: birdEle.offsetTop,
            sum : 0
        }
        // 游戏的状态
        var running = true;
        setInterval(function () {
            if (running) {
                bird.sum += bird.x ;
                // 移动背景让小鸟实现水平运动
                sky.x -= 5;
                game.style.backgroundPositionX = sky.x + 'px';
                // 实现小鸟的上下运动
                bird.speedY += 1;  // 下落
                bird.y += bird.speedY;
                if (bird.y < 0) { // 不能飞出上边界
                    running = false;
                    bird.y = 0;
                }
                if (bird.y + birdEle.offsetHeight > 600) {   // 不能飞出下边界
                    running = false;
                    bird.y = 600 - birdEle.offsetHeight;
                    console.log(bird.y)
                }

                birdEle.style.top = bird.y + 'px';
            }

        }, 30)
       

        // 点击文档的时候实现小鸟向上运动
        document.onclick = function () {
            bird.speedY = -10;
        }
        document.onkeypress = function() {
            bird.speedY = -8;
        }
        // 创建管道
        function createPipe(position) {
            var pipe = {};
            pipe.x = position;
            pipe.uHeight = 200 + parseInt(Math.random() * 100);
            pipe.dHeight = 600 - pipe.uHeight - 200;
            pipe.dTop = pipe.uHeight + 200;
            var uPipe = document.createElement('div');
            uPipe.style.width = '52px';
            uPipe.style.height = pipe.uHeight + 'px';
            uPipe.style.background = 'url("./images/pipe2.png") no-repeat center bottom';
            uPipe.style.position = 'absolute';
            uPipe.style.top = '0px';
            uPipe.style.left = pipe.x + 'px';
            game.appendChild(uPipe);

            var dPipe = document.createElement('div');
            dPipe.style.width = '52px';
            dPipe.style.height = pipe.dHeight + 'px';
            dPipe.style.background = 'url("./images/pipe1.png") no-repeat center  top';
            dPipe.style.position = 'absolute';
            dPipe.style.top = pipe.dTop + 'px';
            dPipe.style.left = pipe.x + 'px';
            game.appendChild(dPipe);
            
            // 让管道运动起来
            setInterval( pipMove, 30)

            function pipMove() {
                if (running) {
                    pipe.x -= 2;
                    uPipe.style.left = pipe.x + 'px';
                    dPipe.style.left = pipe.x + 'px';
                    if (pipe.x < -52) {
                        pipe.x = 800;
                    }
                    var uCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y < pipe.uHeight;  // 鸟飞入管道
                    var dCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y > pipe.uHeight + 200;
                    if (uCheck || dCheck) {
                        running = false;
                        console.log("得分" , bird.sum );
                    }
                }
                
            }

        }

        // 初始界面的4个管道
        createPipe(400);
        createPipe(600);
        createPipe(800);
        createPipe(1000);

    </script>
</body>

</html>

//图片各存一个小文件夹


image.png
image.png
image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    余生动听阅读 13,606评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 9,800评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 128,293评论 2 7

友情链接更多精彩内容