当前位置:编程学习 > JS >>

JS写的一个方块移动游戏

Java代码 
<script> 
            //用于判定是否是开始初始化方块 
            var mark = false; 
            var starttime, endtime, finaltime = 0;  //记录游戏时间 
            var array = new Array();  //用一个数组装所有绿色方块对象 
            var timerarr = new Array();  //记录每个绿色方块 和 对应的计时器id 
             
            function move(e){ 
                var xy = document.getElementById("xy"); 
                var self = document.getElementById("self"); 
                x = e.clientX; 
                y = e.clientY; 
                xy.value = "当前坐标:"+x +","+y; 
                if(x>=454 && x<=530 &&y>=285 && y<=355 && mark==false){ 
                        mark = true; 
                        initothers(20); 
                } 
                target(e,x,y); 
            } 
             
            //线宽4px  格边长36px   target函数定位白色方块的位置 
            function target(e,x,y){ 
                 
                var self = document.getElementById("self"); 
                if(x>=12 && x<=855 && y>=40 &&y<=525 && mark ==true){ 
                    self.style.left = x +'px'; 
                    self.style.top =  y +'px'; 
                } 
            } 
            //初始化白方块位置 
            function init(){ 
                var today = new Date(); 
                starttime = today.getTime(); 
                document.getElementById("self").style.left = 454+'px'; 
                document.getElementById("self").style.top = 282+'px'; 
            } 
            //用一个对象存储绿色方块信息 
            function piece(){ 
                piece.prototype.id = -1;   //方块编号   从0开始 
                piece.prototype.dire = 0;  //移动方向   1上 2下 3左 4右 
                piece.prototype.top = 0; 
                piece.prototype.left = 0; 
            } 
 
            //初始化num个绿色移动方块 
            function initothers(num){ 
                for(var i=0;i<num;i++){ 
                    document.getElementById("pan").innerHTML += "<div id='"+i+"'; style='height:36px;width:36px;background-color:#00FF00;z-index:100; position:absolute;left:-50px'></div>"; 
                    //num1表示从横坐标出现还是纵坐标<1表示横 >1表示纵   num2 >1表示沿着坐标轴运动  <1表示逆着坐标走 
                    var num1 = Math.random()*2; 
                    var num2 = Math.random()*2; 
                    var top1 = 43;   //top1表示从上向下移动   
                    var top2 = 563;  //top2表示从下向上移动 
                    var left1 = 14;  //left1表示左-->右 
                    var left2 = 894;  //右-->左 
                    var p = new piece(); 
                    p.id = i; 
                     
         &

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,