基于Js的打老鼠游戏

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> 本周周末完成 </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 <script type="text/javascript">
// window.onload=loadbody()
 function loadbody(){
      var para=document.getElementById("para"); //获取表格对象
	  var table =document.getElementById("table");
	  if(table==null){
	       table=document.createElement("table");
	       table.id="table";
	  }else{
	      para.removeChild(table);
           table=document.createElement("table");
	       table.id="table";
	  }
	 
	  var xwidth=document.getElementById("xwidth").value;
	  var yheight=document.getElementById("yheight").value;
	  for(var i=0;i<xwidth;i++){ //循环创建表格
			var tr_=document.createElement("tr");
			for(var j=0;j<yheight;j++){
			    var td_=document.createElement("td");
			    var node=document.createTextNode("");
				td_.id="td"+i+j;//设置表格内部id参数
				td_.style.height="60px";
				td_.style.width="60px";
			    td_.appendChild(node);
				tr_.appendChild(td_);
			}
			table.appendChild(tr_);
	  }
	  table.border="1px";
	  para.appendChild(table);
	  //para.style.background="";
	  createmousesite();//表格生成后开始运行
   }
   var str="";
   var t=0;
   function createmousesite(){//创建位置
	  clearTimeout(t);
	  if((str!="")&&(str!=null)){
	    document.getElementById(str).innerHTML="";
		document.getElementById(str).onclick=null;
	  }
	  var xwidth=document.getElementById("xwidth").value;
	  var yheight=document.getElementById("yheight").value;
	  var x=parseInt( Math.random()*xwidth);
	  var y=parseInt(Math.random()*yheight);
      var td_=document.getElementById("td"+x+y);
	  this.str="td"+x+y;//保留当前Id
	  td_.innerHTML="<img src='images/cai.gif'/>";//将图片插入网格处
	  td_.onclick=function (){
	     heatmouse();//加载函数
	  };
	  t=setTimeout("createmousesite()",1000);//一秒钟更换一次位置
   }
  
   var count=0;
   function heatmouse(){
	  count++;
      document.getElementById("count").innerHTML=count*10+"分";
   }
   function stopmouse(){//停止运动
    clearTimeout(t);
   }
 </script>
 <style type="text/css">
   
 </style>
 </head>

 <body ><!--这里提供一个初始化的设置-->
   <div id="setvalue" style="width:600px;height:40px">
   <table border="0" >
      <tr>
	      <td>
              <input type="button" value="start" onclick="loadbody()"/>
	      </td>
		  <td>addvalue:</td>
		  <td>
		      x:<input type="text" id="xwidth" value="4"/>
		  </td>
		  <td>
		      y:<input type="text" id="yheight" value="4"/><!--默认参数-->
		  </td>
	  </tr>
	  <tr>
	      <td id="count" style="color:red;height:40px"></td>
	  </tr>
   </table>
   </div>
   <div id="para"><!--这里为主业务区域-->
  
   </div>
 </body>
</html>

转载于:https://my.oschina.net/trsli/blog/157267

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值