JS版:不要踩白块

  别踩白块这个游戏,很适合用来练手,而且也不是那么难,废话不多说,先上游戏的思路

以下是代码部分

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>whiteBlock</title>
        <style type="text/css">
            .main {
                width: 400px;
                height: 400px;
                border: 1px solid #FF8C00;
                margin: auto;
                position: relative;
                overflow: hidden;
            }
            .container {
                width: 100%;
                height: 100%;
                position: relative;
                top: -100px;
            }
            .row {
                width: 100%;
                height: 100px;
            }
            .cow {
                width: 100px;
                height: 100px;
                float: left;
            }
            .black {
                background-color: black;
            }
            .score_box {
                margin: 100px 200px;
                position: absolute;
                border: 1px solid #00FFFF;
            }
            .score_box h1{
                float: left;
            }
        </style>
    </head>
    <body>
        <div class="score_box">
            <h1 id="score">0</h1><h1>:分</h1>
        </div>
        
        <div class="main" id="main">
            <div class="container" id="container"></div>
        </div>
        <script>
        var control = null;//定时器控制句柄    
        var flag = 0;//全局判断标记: 0:初始化   1:进行中   2:暂停  3:失败
        var speed = 2;//用于控制速度的快慢
            
         //游戏初始化函数
         function init(){
             for (var i=0;i<4;i++) {
                 createRow();
             }
             $('main').onclick = function(event){
                 judge(event);
             };
         }
         
         function createRow(){
             var row = createrDiv('row');
             var con = $('container');
             //随机产生0~3的数,用以确定那个盒子的颜色为黑色
             var index = Math.floor(Math.random()*4);
             for (var i=0;i<4;i++) {
                 index===i?row.appendChild(createrDiv('cow black')):row.appendChild(createrDiv('cow'));
             }
             
             con.firstChild==null?con.appendChild(row):con.insertBefore(row,con.firstChild);
         }
         //创建div元素,并添加其传过来的classname
         function createrDiv(className){
             var div = document.createElement('div');
             div.className = className;
             return div;
         }
            
         //根据类名返回元素
         function $(name){
             return document.getElementById(name);
         }
         
         //方块移动函数
         function move(){
             var con = $('container');
             var top = parseInt(window.getComputedStyle(con,null)['top']);
             top+=speed;
             if(Math.abs(top)<speed){
                 top=0;
             }
             con.style.top = top+'px';
             if(top===0){ //每新建一行的时候相应的删除最后一行
                 createRow();
                 delectRow();
                 con.style.top = '-100px';
             }
             else if(top===(-100+speed)){//
                 var rows = con.childNodes;
                 if((rows.length==5)&&rows[rows.length-1].pass!==1){
                     fail();
                 }
             }
            
         }
         //定时器启动函数
         function start(){
              control = window.setInterval('move()',30);
         }
         //删除越界的行的函数
         function delectRow(num){
                 var con = $('container');
                 if(con.children.length==6)
                    con.removeChild(con.lastChild);    
         }
         //得分函数
         function score(){
             $('score').innerHTML = parseInt($('score').innerHTML)+10;
             if(parseInt($('score').innerHTML)>0&&parseInt($('score').innerHTML)%100==0){
                 speed+=2;
             }
         }
         //判断函数
         function judge(){
             if(flag===3){
                 alert('游戏结束!');
                 return;
             }
             if(event.target.className.indexOf('black')==-1){
                 fail();
             }
             else{
                     event.target.className = 'cow';
                     event.target.parentNode.pass = 1;
                     score();
             }
         }
         //判断输赢函数
         function fail(){
             clearInterval(control);
                 flag = 3;
                 alert('游戏结束');
         }
         
         
         init();
         start();
        </script>
    </body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值