JavaScript-翻牌游戏选关模式
2016-09-04 09:24
141 查看
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> #mx{ width:600px; height:30px; background-color:red; border: solid 1px black; float: right; } #cx{ width:0px; height:30px; background-color: white; } </style> </head> <body> <div id="mx" style="display: none;"> <div id="cx"></div> </div> <br /> <center> <h1>请选择关卡</h1> <input type="text" id="count" /> <input type="button" onclick="addtable()" value="开始" /> <hr /> <hr /> <div id="div1"></div> </center> </body> <script> //显示生命条 function showmx(){ document.getElementById("mx").style.display="block"; } //创建游戏界面表格 var tableid="tabletemp"; function addtable(){ var count=window.parseInt(document.getElementById("count").value); count=2*count; var div=document.getElementById("div1") var table=document.createElement("table"); table.id=tableid; var cellid=1; for(var i=0;i<count;i++){ var row=table.insertRow(); for(var j=0;j<count;j++){ var cell=row.insertCell(); cell.style.width="43px"; cell.style.height="43px"; cell.style.backgroundColor="#e7e7e7"; cell.id="cell"+cellid; cell.onclick=cellclick; cellid++; } } div.appendChild(table); creater(); } var arrrandom=new Array(); var arrback=new Array(); //产生随机数 function creater(){ showmx(); var count=window.parseInt(document.getElementById("count").value); count=4*Math.pow(count,2); for(var i=0;i<count;i++){ var num=Math.random()*count+1; var num1=Math.floor(num); if(i==0){ arrrandom[i]=num1; }else{ var flag=true; for(var j=0;j<arrrandom.length;j++){ if(num1==arrrandom[j]){ i--; flag=false; break; } } if(flag==true){ arrrandom[i]=num1; } } } //alert(arrrandom); for(var i=1;i<=arrrandom.length;i++){ var imgindex=1; if(i<=count/2){ imgindex=i; }else{ imgindex=i-count/2; } document.getElementById("cell"+arrrandom[i-1]).style.backgroundImage="url(img/"+imgindex+".gif)"; arrback[i-1]=arrrandom[i-1]+"_"+imgindex; } setTimeout(hidetable,3000); } //将所有单元格的背景还原 function hidetable(){ var count=window.parseInt(document.getElementById("count").value); count=4*Math.pow(count,2); for(var i=1;i<=count;i++){ document.getElementById("cell"+i).style.backgroundImage=""; } lifvalue(); } var mxlen=600; var cxlen=0; //生命值改变函数 function lifvalue(){ document.getElementById("cx").style.width=cxlen+"px"; cxlen++; if(cxlen<mxlen){ live=setTimeout(lifvalue,100); }else{ alert("GAME OVER!"); } } var clickone=null; var clicktwo=null; //点击单元格事件 function cellclick(){ var target=event.srcElement; var cellid=target.id; cellid=parseInt(cellid.substring(4)); for(var i=0;i<arrback.length;i++){ var celldate=arrback[i].split("_"); if(celldate[0]==cellid){ target.style.backgroundImage="url(img/"+celldate[1]+".gif)"; } } if(clickone==null){ clickone=target; }else{ clicktwo=event.srcElement; if(clickone.style.backgroundImage==clicktwo.style.backgroundImage){ clickone=null; clicktwo=null; checkwin(); }else{ setTimeout(changeback,500); } } } //改变背景颜色 function changeback(){ clickone.style.backgroundImage=""; clicktwo.style.backgroundImage=""; clickone=null; clicktwo=null; } //判断是否赢了 function checkwin(){ var flag=true; var count=window.parseInt(document.getElementById("count").value); count=4*Math.pow(count,2); for(var i=1;i<=count;i++){ if(document.getElementById("cell"+i).style.backgroundImage==""){ flag=false; } } if(flag==true){ document.getElementById("mx").style.display="none"; cxlen=0; document.getElementById("cx").style.width=cxlen+"px"; document.getElementById("tabletemp").remove(); clearTimeout(live); arrrandom=[]; arrback=[]; var count=window.parseInt(document.getElementById("count").value); document.getElementById("count").value=count+1; //alert(window.parseInt(document.getElementById("count").value)); var result=confirm("恭喜您,您通过了本关,进行下一关"); if(result==true){ addtable(); } } } </script> </html>
相关文章推荐
- javascript开发HTML5游戏--斗地主(单机模式part3)
- javascript基础-翻牌游戏
- javascript开发HTML5游戏--斗地主(单机模式part1)
- JavaScript抽奖翻牌游戏/定义数组保存传入参数数组,避免操作传入数组/洗牌动作队列洗牌及翻牌效果
- javascript基础-翻牌游戏
- javascript开发HTML5游戏--斗地主(单机模式part2)
- 数据结构-翻牌游戏
- JavaScript(JScript)下Singleton(单态)模式的实现
- 休闲棋牌游戏大厅的基本模式及特点
- 一种JavaScript的设计模式
- 一种JavaScript的设计模式
- 为J2ME开发移动3D游戏之保留模式
- javascript打开模式窗口的用法
- 用窗口模式运行游戏
- javascript实现Observer模式来管理多个事件回调
- 用javaScript实现观察者模式
- 解密手机网络游戏收费模式 2005.11.23
- 深度分析:盛大实施游戏新收费模式为哪般
- AJAX只是用JavaScript来开发先进玩意儿的模式
- 为J2ME开发移动3D游戏之立即模式