zoukankan      html  css  js  c++  java
  • js 面向对象 打气球小游戏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
        <style>
            * {
                margin:0;
                padding:0;
            }
            
            html,body {
                height:100%;
                background-color:#ccc;
            } 
            
            div {
                width:250px;
                height:333px;
                position:absolute;
                background: url(img/ballons.png) no-repeat 0px 0;
            }
        </style>
    </head>
        
    <body>
        <!--<div></div>-->
        <h2></h2>
        <h2></h2>
        <script>
            var balloons = [];
            var allScore = 0;
            /*面向对象创建气球的构造函数*/
            function Balloon(){
                this.dom = null;
                this.x = 0;
                this.y = 0;        
                this.score = 0;
                this.init();
                balloons.push(this);
                this.bindEvent();
            }
            
            Balloon.prototype.init = function(){
                this.dom = document.createElement('div');
                document.body.appendChild(this.dom);
                this.x = parseInt(Math.random()*(document.documentElement.clientWidth-250));
                this.y = document.documentElement.clientHeight;
                this.score = parseInt(Math.random()*12)+1; //[1~13);
                this.dom.style.left = this.x+"px";
                this.dom.style.top = this.y+"px";
                var curX = -((this.score-1)%4)*250;
                var curY = -parseInt(((this.score-1)/4))*333;
                this.dom.style.backgroundPosition = curX+"px "+curY+"px";
                
                /*
                    1   2   3    4        (0 -250 -250*2 250*3) 0
                    5   6   7    8        (0 -250 -250*2 250*3) -333
                    9  10  11   12         (0 -250 -250*2 250*3) -333*2
                
                */
            };
            
            Balloon.prototype.bindEvent = function(){
                var _this = this;
                this.dom.onclick = function(){
                    /*每次点击计算分数,下树,从数组中下线*/
                    allScore += _this.score;
                    _this.goDied();
                };
            }
            
            Balloon.prototype.update = function(){
                this.y -= this.score*3;
                if(this.y < -333){
                    this.goDied();
                }
                this.dom.style.top = this.y+"px";
            };
            
            Balloon.prototype.goDied = function(){
                document.body.removeChild(this.dom);
                for(var i=0;i<balloons.length;i++){
                    if(balloons[i] == this){
                        balloons.splice(i,1);
                    }
                }
            };
            
            var allTime = 20;
            var iframe = 0;
            /*给new 出来的每一个this对象添加对应的属性
                每秒创建4个气球 */
            var timer = setInterval(function(){
                iframe++;
                if(iframe %10 == 0){
                    allTime--;
                    for(var i=0;i<4;i++){
                        new Balloon();
                    }
                }
                
                for(var i=0;i<balloons.length;i++){
                    balloons[i]&&balloons[i].update();
                }
                document.getElementsByTagName('h2')[0].innerHTML = "你剩余的时间为:"+allTime+"";
                document.getElementsByTagName('h2')[1].innerHTML = "你目前的分数:"+allScore+"";
                if(!allTime){
                    alert("Game over ,你的总分数为:"+allScore+"");
                    clearInterval(timer);
                }
            },100);
            
            
            
        </script>
    </body>
    </html>

    图片见文件中
  • 相关阅读:
    autocomplete自动完成搜索提示仿google提示效果
    实现子元素相对于父元素左右居中
    javascript 事件知识集锦
    让 IE9 以下的浏览器支持 Media Queries
    「2013124」Cadence ic5141 installation on CentOS 5.5 x86_64 (limited to personal use)
    「2013420」SciPy, Numerical Python, matplotlib, Enthought Canopy Express
    「2013324」ClipSync, Youdao Note, GNote
    「2013124」XDMCP Configuration for Remote Access to Linux Desktop
    「2013115」Pomodoro, Convert Multiple CD ISO to One DVD ISO HowTo.
    「2013123」CentOS 5.5 x86_64 Installation and Configuration (for Univ. Labs)
  • 原文地址:https://www.cnblogs.com/moon-yyl/p/9844950.html
Copyright © 2011-2022 走看看