js的打字游戏的例子

来源:互联网 发布:浙大gpa算法 编辑:程序博客网 时间:2024/05/01 01:17

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'c.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script type="text/javascript">
 var score = 0;
 //产生一个字母
  function getEnglishChar()
  {
   var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   var ch = str.charAt(Math.round(Math.random()*35));
   return ch;
  }
  //将字母写到屏幕上
  function display()
  {
   var ch = getEnglishChar();
   var lab = document.createElement("label");
   lab.innerHTML = ch;
   with(lab.style)
   {
    left = Math.round(document.body.clientWidth * Math.random());
    top = "0px";
    position = "absolute";
    fontSize = "20px";
    color = "green";
   }
   document.body.appendChild(lab);
  }
  //字母掉下来
  function  down()
  {
   var chs = document.getElementsByTagName("label");
   if(chs != null)
   {
    for(i = 0; i < chs.length; i++)
    {
     chs[i].style.top = parseInt(chs[i].style.top) + 20 +"px";
     if(chs[i].style.offsetHeight + chs[i].style.offsetTop >= document.body.clientHeight)
     {
      document.body.removeChild(chs[i]);
      score--;
     }
     alert(score);
    }
   }
  }
  
  var s1 = setInterval("display()",1000);
  var s2 = setInterval("down()",100);
  //alert("asdas");
  document.onkeyup = function(event)
  {
   var str = document.getElementsByTagName("label");
   for(i = 0; i < str.length; i++)
   {
    var ch = str[i].innerHTML;
    e = event?event:window.event;
    //alert(ch.charCodeAt(0)==e.keyCode);
    //alert();
    if(ch.charCodeAt(0) == e.keyCode)
    {
     document.body.removeChild(str[i]);
     score++;
    }
    
    
   }
  }
  
  function end()
  {
   clearInterval(s1);
   clearInterval(s2);
   //alert("你的得分是:"+ score);
   /*var chs = document.getElementsByTagName("label");
   for(i = 0; i < chs.length; i++)
   {
    document.body.removeChild(chs[i]);
   }*/
   alert(score);
   document.body.innerHTML = "";
   
   
  }
 </script>
 </head>
 <body>
 <input type="button" value="停止" onclick="end()"/>
</body>


</html>

原创粉丝点击