JavaScript 小游戏:FlappyBall


demo.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

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

        #game .pipeD {
            background: url(images/pipe1.png) top center;
            position: absolute;
        }

        #game .pipeU {
            background: url(images/pipe2.png) bottom center;
            position: absolute;
        }

        #bird {
            width: 34px;
            height: 26px;
            /*border-radius: 10px;*/
            /*background-color: red;*/
            position: absolute;
            top: 100px;
            left: 100px;
            background: url(images/birds.png) -8px -10px no-repeat;
        }

    </style>
</head>
<body>
<div id="game">
    <div id="bird"></div>
</div>
</body>
</html>
<script>
    var game = document.getElementById("game");
    var birdEle = document.getElementById("bird");
    var gameover = false;
    var g = 1;  //重力加速度

    var sky = {
        position: 0
    }

    var bird = {
        entity: birdEle,
        speedX: 5,
        speedY: 5,
        x: birdEle.offsetLeft,
        y: birdEle.offsetTop
    }

    function Pipe(position) {
        this.x = position;  //管道div的left属性值
        this.width = 52;
		this.upPipeY = 0;   //上管道div的top属性值
        this.upPipeH = parseInt(Math.random() * 175) + 100;  //上面管道div的高度
        this.downPipeY = this.upPipeH + 200;   //下管道div的top属性值
        this.downPipeH = 600 - this.downPipeY;  //下面管道div的高度。 600是div.game的高度

        var divUp = document.createElement("div");
		
        divUp.className = "pipeU";
        divUp.style.left = this.x + "px";
        divUp.style.top = this.upPipeY + "px";
        divUp.style.width = this.width + "px";
        divUp.style.height = this.upPipeH + "px";
        var divDown = document.createElement("div");
        divDown.className = "pipeD";
        divDown.style.left = this.x + "px";
        divDown.style.top = this.downPipeY + "px";
        divDown.style.width = this.width + "px";
        divDown.style.height = this.downPipeH + "px";
		
        game.appendChild(divUp);
        game.appendChild(divDown);

        var _this = this;
        setInterval(function () {
            _this.x -= 1;
            if (_this.x < -52) {
                _this.x = 800;
            }
			if (!gameover) {
                divUp.style.left = _this.x + "px";
                divDown.style.left = _this.x + "px";
            }
            var clsUp = (bird.x + 34 > _this.x) && (bird.x < _this.x + 52) && (bird.y < _this.upPipeH);
            var clsDown = (bird.x + 34 > _this.x) && (bird.x < _this.x + 52) && (bird.y + 26 > _this.downPipeY);
            if (clsUp || clsDown) {
                gameover = true;
            }
        }, 10)
    }


    setInterval(function () {
        if (!gameover) {
            bird.speedY = bird.speedY + g;
            bird.y = bird.y + bird.speedY;
            if (bird.y > 574) {
                bird.y = 574;
                gameover = true;
            }
            if (bird.y < 0) {
                bird.y = 0;
                gameover = true;
            }
            bird.entity.style.top = bird.y + "px";
            sky.position -= bird.speedX;
            game.style.backgroundPositionX = sky.position + "px";
        }
    }, 25)

    document.onmousedown = function () {
        bird.speedY = -10;
    }

    for (var i = 0; i < 4; i++) {
        new Pipe(400 + 800 / 4 * i);
    }
</script>


images/birds.png:



images/pipe1.png:



images/pipe2.png:



images/sky.png:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值