• 2011-05-10

    js数列找规律小游戏(兼容ie&ff&op) - [技术空间]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/lily64-logs/127217903.html

    写了个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>
    <script>
    rulearr=new Array("+","-","*","/");
    rulemorearr=new Array("+","-");
    answer=null;
    gamestart=false;
    timestart=null;
    function mycheck()
    {
    current=document.getElementsByName("mytxt")[document.getElementsByName("mytxt").length-1];
    yesorno="×";
    if(current.value==answer)
    {
    yesorno="√";
    document.getElementById("score").value++;
    }
    mynode=document.createElement("span");
    mynode.innerHTML=current.value+" "+yesorno;
    current.parentNode.replaceChild(mynode,current);
    }
    function mypress(e)
    {
    if(!e)e=event;
    if(e.keyCode==13)
    {
    mycheck();
    makegame();
    }
    }
    function makegame(rule)
    {
    if(rule!="/")
    rule=rulearr[Math.floor(Math.random()*rulearr.length)];
    if(document.getElementById("mygrade").value==1)
    {
    switch(rule)
    {
    case "+":
    min=2;max=20;nmax=100;rule2="";break;
    case "-":
    min=2;max=20;nmax=100;rule2="";break;
    case "*":
    min=2;max=6;nmax=20;rule2="";break;
    case "/":
    min=2;max=6;nmax=1000;rule2="";break;
    }
    }
    else
    {
    switch(rule)
    {
    case "+":
    min=10;max=50;nmax=100;rule2="";break;
    case "-":
    min=10;max=50;nmax=100;rule2="";break;
    case "*":
    min=2;max=6;nmax=50;rule2=rulemorearr[Math.floor(Math.random()*rulemorearr.length)];break;
    case "/":
    min=2;max=6;nmax=5000;rule2=rulemorearr[Math.floor(Math.random()*rulemorearr.length)];break;
    }
    }
    num=Math.floor(Math.random()*(max-min)+min);
    if(document.getElementById("mygrade").value==3&&rule2!="")
    num2=Math.floor(Math.random()*(max-min)+min);
    else
    rule2=num2="";
    first=Math.floor(Math.random()*nmax+1);
    second=eval(first+rule+num+rule2+num2);
    if(parseInt(second)!=second){makegame("/");return;}
    third=eval(second+rule+num+rule2+num2);
    if(parseInt(third)!=third){makegame("/");return;}
    answer=eval(third+rule+num+rule2+num2);
    if(parseInt(answer)!=answer){makegame("/");return;}
    document.getElementById("mydiv").innerHTML+=(first+","+second+","+third+",<input name=mytxt onblur='mycheck();makegame();'><br>");
    document.getElementsByName("mytxt")[document.getElementsByName("mytxt").length-1].onkeypress=mypress;
    document.getElementsByName("mytxt")[document.getElementsByName("mytxt").length-1].select();
    }
    function showtime()
    {
    document.getElementById("mytime").value++;
    if(document.getElementById("mytime").value>=60)
    {
    gamestart=false;
    clearInterval(timestart);
    document.getElementById("btn").disabled=false;
    document.getElementById("mygrade").disabled=false;
    mycheck();
    document.getElementById("mydiv").innerHTML+="over!&nbsp;&nbsp;";
    }
    }
    function mystart(obj)
    {
    gamestart=true;
    obj.disabled=true;
    document.getElementById("mygrade").disabled=true;
    document.getElementById("mytime").value=0;
    document.getElementById("score").value=0;
    document.getElementById("mydiv").innerHTML="";
    timestart=setInterval("showtime()",1000);
    document.getElementById("score").focus();
    makegame();
    }
    </script>
    </head>
    <body>
    <select id=mygrade><option value=1 selected="selected">easy</option><option value=2>middle</option><option value=3>hard</option></select>
    <input type=button value=start id=btn onclick="mystart(this)" />
    second:<input id=mytime value=0 readOnly >&nbsp;&nbsp;&nbsp;&nbsp;score:<input id=score value=0 readOnly >
    <div id=mydiv></div>
    </body>
    </html>

    分享到:

    历史上的今天:

    晴日 2006-05-10