• 2010-07-20

    js版算术小游戏(兼容ie&ff&op) - [技术空间]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/lily64-logs/69832425.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>calculate game</title>
    <script>
    answer="";
    gamestart=false;
    function test()
    {
    num1=Math.floor(Math.random()*9+1);
    num2=Math.floor(Math.random()*9+1);
    operarr=new Array("+","-","*","/");
    oper=operarr[Math.round(Math.random()*(operarr.length-1))];
    formula=num1+" "+oper+" "+num2;
    if(Math.round(eval(formula))==eval(formula)&&eval(formula)>=0&&eval(formula)<=9)
    {
    answer=eval(formula);
    document.getElementById("mydiv").innerHTML="doing...&nbsp;&nbsp;"+formula+" = ?";
    }
    else
    test();
    }
    function mycheck(e)
    {
    if(!e)
    e=event;
    if(gamestart)
    {
    if(document.all)
    {
    if(e.keyCode-48==answer)
    document.getElementById("score").value++;
    }
    else
    {
    if(e.which-48==answer)
    document.getElementById("score").value++;
    }
    test();
    }
    }
    function showtime()
    {
    document.getElementById("mytime").value++;
    if(document.getElementById("mytime").value>=60)
    {
    gamestart=false;
    clearInterval(timestart);
    document.getElementById("btn").disabled=false;
    document.getElementById("mydiv").innerHTML="over!&nbsp;&nbsp;"+formula+" = ?";
    }
    }
    function mystart(obj)
    {
    gamestart=true;
    obj.disabled=true;
    document.getElementById("mytime").value=0;
    document.getElementById("score").value=0;
    timestart=setInterval('showtime()',1000);
    document.getElementById("score").focus();
    test();
    }
    document.onkeypress=mycheck;
    window.onkeypress=mycheck;
    </script>
    </head>
    <body>
    <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 >
    <span id=mydiv></span>
    </body>
    </html>

    分享到: