JavaScript制作顏色反轉(zhuǎn)小游戲
更新時間:2016年09月25日 16:00:01 作者:tianzitianxie
本文給大家分享的是一個JavaScript實現(xiàn)的顏色反轉(zhuǎn)的小游戲的源碼,非常的簡單好玩,有需要的小伙伴可以參考下
游戲規(guī)則:
單擊方塊,被單擊的方塊及相鄰方塊變色,當所有藍色方塊變?yōu)槌壬珪r,完成任務(wù),進入下一級。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame Remove this if you use the .htaccess --> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>顏色翻轉(zhuǎn)</title> <meta name="description" content=""> <meta name="author" content="jiamengkai"> <meta name="viewport" content="width=device-width; initial-scale=1.0"> <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references --> <link rel="shortcut icon" href="/favicon.ico"> <link rel="apple-touch-icon" href="/apple-touch-icon.png"> <style type="text/css"> * { margin: 0; padding: 0; font-family: "Microsoft yahei"; color: #000; } h1 { margin: 10px 0; font-size: 300%; font-weight: bolder; } span { display: block; text-indent: 32px; } .title { position: fixed; top: 50%; left: 50%; width: 300px; height: 600px; margin: -300px 0 0 -500px; } .main { position: fixed; top: 50%; left: 50%; width: 600px; height: 600px; margin: -300px 0 0 -150px; background: #555; border-radius: 8px; 1border: 5px solid #555; } .blue, .orange { margin: 5px; } .blue { background: #099; border-radius: 8px; float: left; } .orange { background: #D69C49; border-radius: 8px; float: left; } .button { 1display: inline-block; zoom: 1; /* zoom and *display = ie7 hack for display:inline-block */ display: block; vertical-align: baseline; margin: 8px 5px; outline: none; cursor: pointer; text-align: center; text-decoration: none; font: 14px/100% Arial, Helvetica, sans-serif; padding: .5em 2em .55em; text-shadow: 0 1px 1px rgba(0,0,0,.3); -webkit-border-radius: .5em; -moz-border-radius: .5em; border-radius: .5em; -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2); -moz-box-shadow: 0 1px 2px rgba(0,0,0,.2); box-shadow: 0 1px 2px rgba(0,0,0,.2); } .button:hover { text-decoration: none; } .button:active { position: relative; top: 1px; } .btn { color: #fef4e9; border: solid 1px #da7c0c; background: #f78d1d; background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20)); background: -moz-linear-gradient(top, #faa51a, #f47a20); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20'); } .btn:hover { background: #f47c20; background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015)); background: -moz-linear-gradient(top, #f88e11, #f06015); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f88e11', endColorstr='#f06015'); } .btn:active { color: #fcd3a5; background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a)); background: -moz-linear-gradient(top, #f47a20, #faa51a); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a'); } .operBtn { margin-top: 10px; width: 200px; text-align: center; } .tipSpan { display: inline-block; margin: 5px 2px; width: 200px; text-align: right; font-size: 18px; } .tipNum { display: inline-block; margin-right: 10px; text-align: right; width: 60px; font-size: 18px; } </style> </head> <body> <div class="title"> <h1>顏色翻轉(zhuǎn)</h1> <h2>游戲規(guī)則:</h2> <span> 單擊方塊,被單擊的方塊及相鄰方塊變色,當所有藍色方塊變?yōu)槌壬珪r,完成任務(wù),進入下一級。 </span> <div class="operBtn"> <input type="button" class="button btn" id="reset" value="重新開始"/> <input type="button" class="button btn" id="resetLevel" value="重置本級"/> </div> <div class="tipInfo"> <span class="tipSpan">級別:</span><div class="tipNum" id="level">0</div> <span class="tipSpan">本級點擊次數(shù):</span><div class="tipNum" id="clickNum">0</div> <span class="tipSpan">總點擊次數(shù):</span><div class="tipNum" id="clickSumNum">0</div> </div> </div> <div class="main" id="main"> </div> </body> <script type="text/javascript" src="common.js"></script> <script type="text/javascript"> var level = 1; var margin = 10; var clickNum = 0; var clickSumNum = 0; window.onload = function() { var mainNode = document.getElementById("main"); var mainWidth = mainNode.offsetWidth; var mainHeight = mainNode.offsetHeight; //重新開始 var reset = document.getElementById("reset"); reset.onclick = function() { level = 1; createNewDiv(); }; //重置本級 var resetLevel = document.getElementById("resetLevel"); resetLevel.onclick = function() { createNewDiv(); }; //換顏色 function changeColor(obj) { if(obj.getAttribute("class")) { if(obj.getAttribute("class") == "blue") { obj.setAttribute("class", "orange"); }else { obj.setAttribute("class", "blue"); } } } //換顏色 function nodesChangeColor(obj) { clickNum += 1; clickSumNum += 1; var clickNumNode = document.getElementById("clickNum"); clickNumNode.innerHTML = clickNum; var clickSumNumNode = document.getElementById("clickSumNum"); clickSumNumNode.innerHTML = clickSumNum; changeColor(obj); var obj_r = parseInt(obj.getAttribute("r")); var obj_c = parseInt(obj.getAttribute("c")); if(obj_r-1>0) { var topObj = document.getElementById("r"+(obj_r-1)+"_c"+obj_c); changeColor(topObj); } if(obj_c+1<=level) { var rightObj = document.getElementById("r"+obj_r+"_c"+(obj_c+1)); changeColor(rightObj); } if(obj_r+1<=level) { var bottomObj = document.getElementById("r"+(obj_r+1)+"_c"+obj_c); changeColor(bottomObj); } if(obj_c-1>0) { var leftObj = document.getElementById("r"+obj_r+"_c"+(obj_c-1)); changeColor(leftObj); } setTimeout(function() { //計算orange塊 的數(shù)量 orangeDivNum(); },500); } //計算orange塊 的數(shù)量 function orangeDivNum() { var o_nodes = document.getElementsByClassName("orange"); if(o_nodes.length == level*level) {//完成全部翻轉(zhuǎn) //進入下一級 level += 1; createNewDiv(); myAlert(); }else {} } //進入下一級 function createNewDiv() { var divWidth = mainWidth/level-margin; var divHeight = mainHeight/level-margin; mainNode.innerHTML = ""; for(var i=1;i<=level;i++) { for(var j=1;j<=level;j++) { var colorNodes = document.createElement("div"); colorNodes.style.cssText = "width: "+divWidth+"px;height: "+divHeight+"px;"; colorNodes.setAttribute("class", "blue"); colorNodes.setAttribute("id", "r"+i+"_c"+j); colorNodes.setAttribute("r", i); colorNodes.setAttribute("c", j); colorNodes.onclick = function() { nodesChangeColor(this); }; mainNode.appendChild(colorNodes); mainNode.style.cssText = "border: 5px solid #555;"; } } var levelSpan = document.getElementById("level"); levelSpan.innerHTML = level; clickNum = 0; var clickNumNode = document.getElementById("clickNum"); clickNumNode.innerHTML = clickNum; } }; </script> </html>
您可能感興趣的文章:
- 神奇!js+CSS+DIV實現(xiàn)文字顏色漸變效果
- js點擊列表文字對應(yīng)該行顯示背景顏色的實現(xiàn)代碼
- JS實現(xiàn)文字鏈接感應(yīng)鼠標淡入淡出改變顏色的方法
- JS實現(xiàn)讓訪問者自助選擇網(wǎng)頁文字顏色的方法
- javascript自動改變文字大小和顏色的效果的小例子
- JS實現(xiàn)顏色梯度與漸變效果完整實例
- jQuery與js實現(xiàn)顏色漸變的方法
- JS實現(xiàn)的RGB網(wǎng)頁顏色在線取色器完整實例
- 基于JavaScript實現(xiàn)隨機顏色輸入框
- JavaScript隨機生成顏色的方法
- js設(shè)置文字顏色的方法示例
相關(guān)文章
JavaScript中使用Spread運算符的八種方法總結(jié)
這篇文章主要給大家介紹了JavaScript中使用Spread運算符的八種方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用JavaScript具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-06-06javascript實現(xiàn)數(shù)組扁平化六種技巧總結(jié)
這篇文章主要為大家詳細介紹了六種javascript中實現(xiàn)數(shù)組扁平化的技巧,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解下2023-12-12bootstrap布局中input輸入框右側(cè)圖標點擊功能
這篇文章主要為大家詳細介紹了bootstrap布局中input輸入框右側(cè)圖標點擊功能實現(xiàn)的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-05-05js prototype 格式化數(shù)字 By shawl.qiu
js prototype 格式化數(shù)字 By shawl.qiu...2007-04-04在javascript中使用com組件的簡單實現(xiàn)方法
下面小編就為大家?guī)硪黄趈avascript中使用com組件的簡單實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦2016-08-08