打气球

打气球

<!DOCTYPE html>
<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>
        div {
            width: 231.25px;
            height: 300.7px;
            background: url(star/ball.png);
            position: absolute;
        }
    </style>
</head>
<body>
    <h1 id="info"></h1>
    <h2 id="e"></h2>
    <script src="https://cdn.bootcss.com/underscore.js/1.9.1/underscore-min.js"></script>
    <script>
        var allTime = 20;
        var allScore = 0;
        function Ballon() {
            this.dom = null;
            this.x = _.random(0,document.documentElement.clientWidth - 236);
            this.y = document.documentElement.clientHeight;
            this.score = _.random(1,12);
            this.init();
            this.bindEvent();
            array.push(this);
        }

        Ballon.prototype.init = function() {
            this.dom = document.createElement("div");
            document.body.appendChild(this.dom);
            // 设置dom元素的位置
            this.dom.style.left = this.x + "px";
            this.dom.style.top = this.y + "px";
            // 设置div的backgroundPosition
            this.dom.style.backgroundPosition = -((this.score-1)%4)*231.25 + "px " +(-parseInt((this.score-1)/4)*300.7) +"px";

            //    -231.25 0     
            // 1   2    3   4       (this.score-1)%4*231.25
            // 5   6    7   8       -300.7*1  parseInt((this.score-1)/4)
            // 9   10   11  12      -300.7*2 
        }

        Ballon.prototype.move = function() {
            this.y -= this.score;
            this.dom.style.top = this.y + "px";
        }

        Ballon.prototype.bindEvent = function() {
            var _this = this;
            this.dom.onmousedown = function() {
                allScore += _this.score; 
                _this.goDie();
            }
        }

        Ballon.prototype.goDie = function() {
            document.body.removeChild(this.dom);
            for(var i=0; i<array.length; i++) {
                if (array[i] === this) {
                    array.splice(i,1);
                }
            }
        }

        var array = [];
        var frame = 0;
        var timer = setInterval(function() {
            frame++;
            if (frame%100==0) {
                allTime--;
            }
            if (allTime==0) {
                alert("游戏结束");
                clearInterval(timer);
            }
            if(frame%10==0) {
                new Ballon();
            } 
            document.getElementById("info").innerHTML = allScore;  
            _.each(array,function(item) {
                item.move();
            })
        },10);
    </script>
</body>
</html>

返回目录

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值