js 考记忆力得小游戏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
    window.onload = function (){
         var oStar = document.getElementById("start");
         var oSpan = oStar.getElementsByTagName("span")[0];

         var oContent = document.getElementById("content");
         var oRect = document.getElementById("rect");
         var timer = null;
         var oTime = document.getElementById("time");

         var aImg = document.getElementsByTagName("img");

         var arrImg = ["http://img3.douban.com/view/photo/thumb/public/p1994200192.jpg",
                        "http://img5.douban.com/view/photo/thumb/public/p1994200528.jpg",
                        "http://img3.douban.com/view/photo/thumb/public/p1994200963.jpg",
                        "http://img3.douban.com/view/photo/thumb/public/p1994201400.jpg",
                        "http://img3.douban.com/view/photo/thumb/public/p1994201804.jpg",
                        "http://img5.douban.com/view/photo/thumb/public/p1994201968.jpg",
                        "http://img3.douban.com/view/photo/thumb/public/p1994202154.jpg",
                        "http://img5.douban.com/view/photo/thumb/public/p1994202276.jpg",
                        "http://img3.douban.com/view/photo/thumb/public/p1994202342.jpg"];

         var oScore = document.getElementById("score");
         var oFail = document.getElementById("fail");
         var oSuccess = document.getElementById("success");
         var level = [{"num":"3","timeGame":"10"},
                       {"num":"4","timeGame":"10"},
                       {"num":"6","timeGame":"15"},
                       {"num":"8","timeGame":"20"}];

         oSpan.onclick = function (){

             var rating = 0;
             starGame( level[rating].num,level[rating].timeGame,rating );

         }

         // 游戏程序 参数分别代表 块数 游戏时间 等级
         function starGame( num,timeGame,rating){ 

             oStar.style.display = "none";
             oFail.style.display = "none";
             oSuccess.style.display = "none";
             oContent.style.display = "block";
             oContent.setAttribute("off","0");

             oTime.style.width = "440px";
             oScore.innerHTML = 0;
             oRect.innerHTML = "";

             fillContent(num);
             setTimeout(function (){
                 for(var i = 0; i < aImg.length; i++){
                     aImg[i].src = "http://img3.douban.com/view/photo/thumb/public/p2191686093.jpg";
                 }
                 getTime( oTime,"width", -Math.floor( parseInt( getStyle(oTime,"width") )/timeGame ), timeGame, num*20, rating );
             },3000);
         }

         //填充块
         function fillContent(size){
             var arr = Mix(size,1,2);
             //console.log(arr);

             for(var i = 0; i < arr.length; i++){
                 oRect.innerHTML += "<img style = ‘top:"+ 110*Math.floor(i/4) +"px; left:"+ 110*Math.floor(i%4) +"px‘ select = ‘N‘ index = " + i + " address = " + arrImg[arr[i]] +" src = "+ arrImg[arr[i]] +" num = "+ arr[i] + ">";
             }
         }

         function Mix( max,min,fre){
             var arr = [];
             for(var i = min; i <= max; i++ ){
                 arr.push(i);
             }
             var newarr = [getRandom(arr.length,1)];
             for(var i = 0; newarr.length < arr.length*fre; i++ ){

                 var num = getRandom(arr.length,1);

                 if( toCon(num,fre-1 ) ){
                     newarr.push(num);
                 }
             }

             function toCon( num,fre ){
                 var iNow = 0;
                 for(var i = 0; i < newarr.length; i++ ){
                     if( num == newarr[i] && iNow != fre ){
                         ++iNow;
                     }else if( num == newarr[i] && iNow == fre ){
                         return false;
                     }
                 }
                 return true;
             }

             return newarr;
         }

         function getTime( obj,attr,speed,time,totle,rating ){
             clearInterval(obj.timer);

             selectRect(totle,rating);
             obj.timer = setInterval( function (){

                 time--;
                 if( time != 0 ){
                     obj.style[attr] = parseInt( getStyle(obj,attr) ) + speed + "px";
                     if( parseInt(oContent.getAttribute("off")) ==  1  ){
                         clearInterval(obj.timer);
                     }
                 }

                 if( time == 0  ){
                     obj.style[attr] = "0px";
                     clearInterval(obj.timer);
                     for(var i = 0; i < aImg.length; i++ ){
                         aImg[i].src = aImg[i].getAttribute("address");
                     }

                     //console.log( oContent.getAttribute("off") );
                     if( parseInt(oContent.getAttribute("off"))!=  1 ){

                         oContent.style.display = "none";
                         oFail.style.display = "block";

                         var oP = oFail.getElementsByTagName("p")[0];

                         oP.onclick = function (){

                             oFail.style.display = "none";
                             oStar.style.display = "block";

                             oSpan.onclick = function (){
                                 starGame(level[0].num,level[0].timeGame,0);
                             }
                         }
                     }

                 }
             },1000)
         }

         function selectRect(totle,rating){
             var selectNum = 0;
             var selectVal = [];
             var selectIndex = [];
             for(var i = 0; i < aImg.length; i++ ){
                 aImg[i].style.cursor = "pointer";
                 aImg[i].onclick = function(){
                     selectNum++;
                     this.style.border = "1px red solid";
                     this.src = this.getAttribute("address");
                     this.setAttribute("select","Y");
                     selectVal.push( parseInt( this.getAttribute("num")) );
                     selectIndex.push( parseInt( this.getAttribute("index")) );

                     //console.log(selectVal );
                     if( selectNum == 2 ){
                         selectNum = 0;

                          if( selectVal.length == 2 ){
                              if( selectVal[0] == selectVal[1]  && selectIndex[0]!= selectIndex[1] ){

                                  toSelectSuccess(totle,rating);
                                  oScore.innerHTML = parseInt( oScore.innerHTML ) + 20;
                              } else {
                                  toSelectError();
                              }
                          }
                         selectVal = [];
                         selectIndex = [];
                     }
                 }
             }

             function toSelectError(){
                 setTimeout (function(){
                     var selArrE = [];

                     for(var i = 0; i < aImg.length; i++ ){
                         if( aImg[i].getAttribute("select") == "Y"){
                             selArrE.push( parseInt(aImg[i].getAttribute("index")) );
                         }
                     }

                     for(var i = 0; i < aImg.length; i++ ){
                         for(var j = 0; j < selArrE.length; j++ ){
                             if( aImg[i].getAttribute("index") == selArrE[j] ){
                                 aImg[i].src = aImg[i].getAttribute("address");
                                 aImg[i].src = "http://img3.douban.com/view/photo/thumb/public/p2191686093.jpg";
                                 aImg[i].style.border = "1px white solid";
                                 aImg[i].setAttribute("select","N");
                             }
                         }
                     }
                     selArrE.length = 0;
                 },300);
            }

            function toSelectSuccess( totle,rating){
                 setTimeout (function(){
                     var selArrS = [];
                     for(var i = 0; i < aImg.length; i++ ){
                         if( aImg[i].getAttribute("select") == "Y"){
                             selArrS.push( aImg[i].getAttribute("index") );
                         }
                     }

                     //console.log(totle);

                     for(var i = 0; i < aImg.length; i++ ){
                         for(var j = 0; j < selArrS.length; j++ ){
                             if( aImg[i].getAttribute("index") == selArrS[j] ){
                                 aImg[i].src = aImg[i].getAttribute("address");
                                 aImg[i].style.display = "none";
                                 aImg[i].setAttribute("select","N");
                             }
                         }
                     }
                     selArrS.length = 0;

                     if( parseInt( oScore.innerHTML ) == parseInt( totle ) ){

                         oContent.setAttribute("off","1");
                         oContent.style.display = "none";
                         oSuccess.style.display = "block";

                         var oP = oSuccess.getElementsByTagName("p")[0];
                         oP.onclick = function(){
                             rating++;

                             if( rating == 4 ){
                                 oStar.style.display = "block";
                                 oFail.style.display = "none";
                                 oSuccess.style.display = "none";
                                 oContent.style.display = "none";
                                 rating = 0;

                                 oSpan.onclick = function (){

                                     starGame( level[0].num,level[0].timeGame,0 );

                                 }

                             }

                             starGame( level[rating].num,level[rating].timeGame,rating);

                         }

                     }

                     //console.log( selArr[0],selArr[1] );
                     var selArr = [];
                     //console.log( selArr[0],selArr[1] );
                     //var selArr = [];
                 },200);
        }

         }

         function getStyle(obj,attr){
             if(obj.currentStyle){
                 return obj.currentStyle[attr];
             } else{
                 return getComputedStyle(obj)[attr];
             }
         }

         function getRandom( max, min){
             return Math.floor( Math.random()*( max - min + 1) + min );
         }
    }
    </script>
    <style>
        *{
            padding: 0px;
            margin: 0px;
        }
        #start{
            background: url(images/background.jpg) no-repeat;
            margin: 50px auto;
            position: relative;
            width: 800px;
            height: 500px;
            overflow: hidden;
            display: block;
        }

        #start span{
            background: pink;
            color: white;
            position: absolute;
            top: 400px;
            left: 700px;
            cursor: pointer;
            padding: 15px;
        }

        #content{
            background: #cccccc;
            margin: 50px auto;
            position: relative;
            width: 440px;
            height: 440px;
            display: none;
        }
        #content p{
            position: absolute;
            right: 5px;
            top: -20px;
        }

        #content #rect{
            width: 440px;
            height: 440px;
            position: relative;;
        }

        #content img{
            border: 1px white solid;
            width: 98px;
            height: 98px;
            margin: 5px;
            position: absolute;
        }

        #time{
            width: 440px;
            background: red;
            height: 30px;
            position: absolute;
            bottom: -30px;
        }

        #fail{
            background: black;
            margin: 50px auto;
            width: 440px;
            height: 440px;
            display: none;
        }

        #fail p {
            margin: 50px auto;
            font-size: 120px;
            color:white;
            cursor: pointer;
        }

        #success{
            background: red;
            margin: 50px auto;
            width: 440px;
            height: 440px;
            display: none;
        }

        #success p {
            margin: 50px auto;
            font-size: 120px;
            color:white;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id = "start">
        <span>start</span>
    </div>

    <div id = "content"  off = "0">
        <p>Score: <span id = "score">0</span>point</p>
        <div id = "time"></div>
        <div id="rect"></div>

    </div>

    <div id = "fail">
        <p>Failure!</p>
    </div>

    <div id = "success">
        <p>Success</p>
    </div>
</body>
</html>
时间: 2024-10-19 15:44:51

js 考记忆力得小游戏的相关文章

JS打造的点灯小游戏

类似于模方游戏的网页小游戏代码,游戏玩法:在方格上点击,你可以将下面的方格全部填成蓝色吗?快开动你智慧脑筋,看看有什么技巧与规律. <HTML> <HEAD> <TITLE>接触角测定仪</TITLE> <META http-equiv=Content-Type content="text/html; charset=gb2312"> <style type="text/css"> <!-

原生JS实现的h5小游戏-植物大战僵尸

代码地址如下:http://www.demodashi.com/demo/12755.html 项目介绍 本项目是利用原生js实现的h5小游戏-植物大战僵尸,主要结合了一下自己对于h5小游戏的理解,结合面向对象的编程思想进行开发,在实现时使用了部分es6语法,对于es6语法不太熟悉的小伙伴可以先查阅相关资料了解一下. 如有需要,可根据自己的需求修改源码样式.源码配置属性代码,实现个性化定制. 以下为文件目录结构示意图,核心代码在js文件夹下的四个common.js.main.js.game.js

css3+jquery+js做的翻翻乐小游戏

主要是为了练习一下css3的3D翻转功能,就做了这么个小游戏,做的比较粗糙,但是效果看的见. 主要用到的css3代码如下: html结构: 1 <div class="container"> 2 <div class="side"> 3 4 <div class="front"> 5 <!-- 正面 --> 6 </div> 7 8 <div class="back&qu

JS实现找色块小游戏

之前用WPF的方式做了个简单的小游戏,找不同色块,WPF可以实现,JS更加Easy,于是又用JS做了一个,逻辑差不多,只是代码的实现方式有所不同 先上效果 关键代码如下 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; chars

使用前端原生 js,贪吃蛇小游戏

好久好久,真的是好久好久没来写过了,因为最近有点小忙.不过即使是忙,也也还是写了两个小游戏,其中一个就是这个,贪吃蛇啦. 算是一个小练手了,这个是在有点太简单了,只是第一次写这种小游戏,还是零零星星花了三五天时间,下面就是这个小游戏的gif小动画,比较简单,对比过网上其他用来写出来练手的贪吃蛇作品,这个在颜值还是功能上,都还是不错的,霍霍. 这里讲解一下功能: 空格控制开始和暂停. 方向键控制移动方向. Q 键加速,再按一次恢复常规速度(在加速状态时,按下或者方向键,或者吃到了白色小食物,速度自

js制作贪吃蛇小游戏

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #map{ width: 800px; height: 800px; background-color: black; position: relative; } </style> <

js实现点气球小游戏

二话不说直接贴代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>点气球</title> </head> <body> <!-- 实现功能: js生成div 在页面上向上移动 已不同的速度 点击气球 气球爆炸消失 1.js生成div标签 并初始化 1 一次生成10个 2

原生js实现一个连连看小游戏(二)-----------生成随机不重复数字

直接贴代码: <!DOCTYPE html> <html> <head> <title>生成随机不重复数</title> </head> <body> <script type="text/javascript"> var arr=new Array(); for(var i=0;i<10;i++){ arr.push(i); } // console.log(arr) var get

原生js实现一个连连看小游戏(三)-----------点击列表获取索引

需求:当点击一个列表时,我们要知道它在列表的第几项,即索引,代码实现如下: <!DOCTYPE html> <html> <head> <title></title> </head> <body> <ul id="myUl"> <li>第1个li</li> <li>第1个li</li> <li>第1个li</li> &l