關(guān)燈游戲

<p>我不喜歡下雨天,可我所在的城市卻偏偏細(xì)雨綿綿。我多希望明天可以看到晴空萬里。那樣我應(yīng)該就不會(huì)像現(xiàn)在這樣壓抑

</p>
<p>這樣的心情什么也不想做。

分析整個(gè)開發(fā)的過程

<p>當(dāng)然在開始寫代碼之前,還是要給大家看看游戲的效果圖的。

S61025-21204806.jpg width="200"

<p>看完了效果圖之后。咱們一起來分析一下,這個(gè)簡單的游戲到底需要幾步才可以完成。
<ol>
<li>首先,咱們要干的就是布局。怎么才能出現(xiàn)上面的效果的樣式。
<li>然后就是怎么關(guān)掉每一盞燈,當(dāng)然這里要用到onclick這個(gè)點(diǎn)擊事件。
<li>經(jīng)過前面兩步,燈應(yīng)該就可以關(guān)掉啦,這里就應(yīng)該處理邊界問題。(后面會(huì)細(xì)講)。
<li>接下來就是怎么判斷是否過關(guān)(后面細(xì)講);
</ol>

布局

<p>這里布局不會(huì)用html的。那樣的話代碼太多,而且全部重復(fù),所以直接用JavaScript,一個(gè)for循環(huán)就夠啦。
<p>咱們?cè)谧鲇螒蛑跋葋韨€(gè)簡單的游戲說明界面:
<ol>
<li>游戲說明的CSS文件

#mask {
    background: rgba(200, 200, 200, 0.9);
    width: 100%;
    height: auto;
    position: absolute;
    display: none;
   }
   .title {
    color: red;
    padding-left: 20px;
    text-align: left;
   }  
   ul {
    text-align: left;
    color: black;
    padding-right: 5px;
   }   
   li {
    line-height: 20px;
   }   
   .titleBox {
    width: 250px;
    font-size: 0;
    margin: 5px auto;
   }   
   .titleBox div {
    display: inline-block;
    background: black;
    height: 30px;
    width: 30px;
    border: 1px solid gray;
   }
   
   .titleBox div:nth-child(1),
   .titleBox div:nth-child(2),
   .titleBox div:nth-child(8),
   .titleBox div:nth-child(6),
   .titleBox div:nth-child(7),
   .titleBox div:nth-child(14),
   .titleBox div:nth-child(18),
   .titleBox div:nth-child(25),
   .titleBox div:nth-child(26),
   .titleBox div:nth-child(24),
   .titleBox div:nth-child(32),
   .titleBox div:nth-child(36),
   .titleBox div:nth-child(43),
   .titleBox div:nth-child(44),
   .titleBox div:nth-child(42),
   .titleBox div:nth-child(48),
   .titleBox div:nth-child(49) {
    background: yellow;
   }
   
   .czsm {
    border: 1px solid cyan;
    color: blue;
    padding: 2px;
    position: absolute;
    right: 15px;
    top: 10px;
   }
   
   .tuichu {
    border: 1px solid cyan;
    color: blue;
    font-size: 1.2em;
    padding: 3px 10px;
    position: absolute;
    right: 20px;
    top: 20px;
   }

<li>游戲說明的HTMl文件

<div id="mask">
   <h2 class="title">游戲操作說明</h2>
   <ul class="ul">
    <li>本游戲由49盞燈所組成,共有兩種狀態(tài),燈開的狀態(tài)為黃色,關(guān)的狀態(tài)為黑色
    </li>
    <li>當(dāng)按開始游戲按鈕時(shí),會(huì)模擬點(diǎn)亮一些燈</li>
    <li>當(dāng)你把所有黃色的燈點(diǎn)為黑色,就可以進(jìn)行下一關(guān)</li>
    <li>下面是游戲操作過程</li>
    <div class="titleBox">
    </div>
    <li>(如上圖所示,當(dāng)你按下最角上那個(gè)時(shí),會(huì)出現(xiàn)如上圖所示的亮燈狀態(tài),按下邊上的時(shí),看不到的地方不亮燈,其他的地方是以十字的形式完成,按燈時(shí)與他相關(guān)的都會(huì)變?yōu)榕c按之前相反的狀態(tài))</li>
   </ul>
   <div class="tuichu">
    進(jìn)入游戲
   </div>
</div>

<li>游戲界面的效果圖:


S61025-21150128.jpg

<p>游戲說明面界面的這個(gè)格子也是用for循環(huán)寫的

var titleBox = document.querySelector('.titleBox');
 for(var j = 0; j < col * row; j++) {
  var box1 = document.createElement('div');
  titleBox.appendChild(box1);
 }
console.log("\u5fc3\u60c5\u4e0d\u597d\u3002\u4e0d\u60f3\u5199\u5566");


<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>關(guān)燈游戲</title>
  <style media="screen">
   body {
    margin: 0px;
    padding: 0px;
    text-align: center;
    background-color: pink;
   }
   
   .wrap {
    background-color: pink;
    font-size: 0px;
    margin: auto;
    text-align: center;
   }
   
   .box {
    border-radius: 0px 10px 0px 10px;
    font-size: 16px;
    text-align: center;
    color: white;
    margin: 3px;
   }
   
   .start_btn {
    border: 1px solid gray;
    border-radius: 10px;
    background-color: pink;
    outline: none;
    font-size: 20px;
    height: 40px;
    width: 130px;
    margin: 10px;
   }
   
   .start_btna {
    border: 1px solid gray;
    background-color: pink;
    outline: none;
    border-radius: 10px;
    font-size: 20px;
    height: 40px;
    width: 130px;
    margin: 10px;
   }
   
   .left,
   .right {
    font-size: 20px;
    margin-top: 20px;
    height: 40px;
    position: absolute;
    color: red;
   }
   
   .left {
    width: 150px;
    float: left;
   }
   
   .right {
    width: 100px;
    right: 20px;
    float: right;
   }
   
   .top {
    font-size: 30px;
    padding: 5px;
    color: white;
    font-style: normal;
    text-shadow: 3px 3px 15px yellow;
   }   
   .mask {
    border: 2px solid red;
    position: absolute;
    padding: 5px;
    text-align: left;
   }   
   .footer {
    width: 100%;
    position: absolute;
    bottom: 10px;
    color: cyan;
    text-align: center;
   }   
   #mask {
    background: rgba(200, 200, 200, 0.9);
    width: 100%;
    height: auto;
    position: absolute;
    display: none;
   }
   .title {
    color: red;
    padding-left: 20px;
    text-align: left;
   }  
   ul {
    text-align: left;
    color: black;
    padding-right: 5px;
   }   
   li {
    line-height: 20px;
   }   
   .titleBox {
    width: 250px;
    font-size: 0;
    margin: 5px auto;
   }   
   .titleBox div {
    display: inline-block;
    background: black;
    height: 30px;
    width: 30px;
    border: 1px solid gray;
   }
   
   .titleBox div:nth-child(1),
   .titleBox div:nth-child(2),
   .titleBox div:nth-child(8),
   .titleBox div:nth-child(6),
   .titleBox div:nth-child(7),
   .titleBox div:nth-child(14),
   .titleBox div:nth-child(18),
   .titleBox div:nth-child(25),
   .titleBox div:nth-child(26),
   .titleBox div:nth-child(24),
   .titleBox div:nth-child(32),
   .titleBox div:nth-child(36),
   .titleBox div:nth-child(43),
   .titleBox div:nth-child(44),
   .titleBox div:nth-child(42),
   .titleBox div:nth-child(48),
   .titleBox div:nth-child(49) {
    background: yellow;
   }
   
   .czsm {
    border: 1px solid cyan;
    color: blue;
    padding: 2px;
    position: absolute;
    right: 15px;
    top: 10px;
   }
   
   .tuichu {
    border: 1px solid cyan;
    color: blue;
    font-size: 1.2em;
    padding: 3px 10px;
    position: absolute;
    right: 20px;
    top: 20px;
   }
  </style>

 </head>

 <body>
  <audio src="music/game.mp3" loop="loop" class="gameMusic"></audio>
  <audio src="music/dianji.wav" class="music"></audio>
  <div class="czsm">
   操作說明
  </div>
  <div id="mask">
   <h2 class="title">游戲操作說明</h2>
   <ul class="ul">
    <li>本游戲由49盞燈所組成,共有兩種狀態(tài),燈開的狀態(tài)為黃色,關(guān)的狀態(tài)為黑色
    </li>
    <li>當(dāng)按開始游戲按鈕時(shí),會(huì)模擬點(diǎn)亮一些燈</li>
    <li>當(dāng)你把所有黃色的燈點(diǎn)為黑色,就可以進(jìn)行下一關(guān)</li>
    <li>下面是游戲操作過程</li>
    <div class="titleBox">
    </div>
    <li>(如上圖所示,當(dāng)你按下最角上那個(gè)時(shí),會(huì)出現(xiàn)如上圖所示的亮燈狀態(tài),按下邊上的時(shí),看不到的地方不亮燈,其他的地方是以十字的形式完成,按燈時(shí)與他相關(guān)的都會(huì)變?yōu)榕c按之前相反的狀態(tài))</li>
   </ul>
   <div class="tuichu">
    進(jìn)游戲
   </div>
  </div>
  <footer class="footer">
   <i style="color: coral;">xxxxxxxxxxxxx</i></P>
  </footer>

 </body>
 <script type="text/javascript">
  var music = document.querySelector('.music');
  var row = 7; //行
  var col = 7; //列
  var boxWidth = 40; //每一
  var boxHeight = 40;
  var beforeBg = 'black';
  var afterBg = 'yellow';
  var beforColor = 'white';
  var afterColor = 'red';
  var score = 0; //分?jǐn)?shù)遞增
  var confirmAdd = 0; //關(guān)數(shù)遞增
  var confirm = 0; //關(guān)數(shù)遞增(產(chǎn)生關(guān)數(shù))
  var startOroff = true;
  var boxBorder = '1px solid gray';
  var boxBordera = '1px solid yellow'  
  var mask = document.getElementById('mask');
  mask.style.height = document.documentElement.clientHeight + 'px';
  var gameMusic=document.querySelector('.gameMusic');
  
  var czsm = document.querySelector('.czsm');
  var tuichu = document.querySelector('.tuichu');
  var body = document.getElementsByTagName('body'); //獲取節(jié)點(diǎn)body標(biāo)簽
  var wrap = document.createElement('div'); //創(chuàng)建一個(gè)大的DIV
  wrap.className = 'wrap'; //給大的DIV一個(gè)class名(方便布局)
  wrap.style.width = row * boxWidth + row * 2 + row * 7 + 'px'; //設(shè)置大DIV的寬度
  body[0].appendChild(wrap);
  var topDiv = document.createElement('div');
  topDiv.className = 'top';
  topDiv.innerHTML = '關(guān)燈游戲';
  wrap.appendChild(topDiv);
  var light = [];
  var titleBox = document.querySelector('.titleBox');
  for(var j = 0; j < col * row; j++) {
   var box1 = document.createElement('div');
   titleBox.appendChild(box1);
  }
  for(var j = 0; j < col * row; j++) {
   var box = document.createElement('div');
   box.className = 'box';
   box.style.height = boxHeight + 'px';
   box.style.width = boxWidth + 'px';
   box.style.border = boxBorder;
   box.style.backgroundColor = beforeBg;
   box.style.display = 'inline-block';
   box.style.lineHeight = boxHeight + 'px';
   box.index = j;
   box.addEventListener('touchend', boxclick, false);
   light.push(box);
   wrap.appendChild(box);
  }
  var but = document.createElement('button');
  but.className = 'start_btn';
  but.innerHTML = '開始游戲';
  but.addEventListener('touchend', rand, false);
  wrap.appendChild(but);
  var leftp = document.createElement('div');
  leftp.className = 'left';
  leftp.innerHTML = '還需關(guān)燈<div style="color:white; font-size:16px;display:inline-block; margin:0px 5px;border:1px solid black; padding:5px 10px;border-radius:20px">0</div>盞';
  wrap.appendChild(leftp);
  var rightp = document.createElement('div');
  rightp.className = 'right';
  rightp.innerHTML = '第<div style="color:white; font-size:16px;display:inline-block; margin:0px 5px;border:1px solid black; padding:5px 10px;border-radius:20px">1</div>關(guān)';
  wrap.appendChild(rightp);
  var butt = document.createElement('button');
  butt.className = 'start_btna';
  butt.innerHTML = '重置關(guān)卡';
  wrap.appendChild(butt);
  var vv = true;
  tuichu.addEventListener('touchend', function() {
   mask.style.display = 'none';
   wrap.style.display = 'block';
   czsm.style.display = 'block';
  }, false);
  czsm.addEventListener('touchend', function() {
   mask.style.display = 'block';
   wrap.style.display = 'none'
   czsm.style.display = 'none';

  }, false);
  
  butt.addEventListener('touchend', function() {
   if(startOroff) {
    alert('請(qǐng)先開始游戲');
    return;
   } else {
    if(vv) {
     startOroff = true;
     var boxDiv = document.querySelectorAll('.box');
     for(var i = 0; i < boxDiv.length; i++) {
      boxDiv[i].style.backgroundColor = beforeBg;
      boxDiv[i].style.border = boxBorder;
      boxDiv[i].style.color = beforColor;
      boxDiv[i].style.boxShadow = '0px 0px 0px yellow';
     }
     score = 0;
     confirm -= 1;
     rand();
     vv = false;
    } else {
     alert('每關(guān)只能重置一次')
    }
   }
  }, false);
  function clickItem(obj) {
   onOroff(obj);
   if(obj.index % col != col - 1) {
    onOroff(light[obj.index + 1]);
   }
   if(obj.index % col != 0) {
    onOroff(light[obj.index - 1]);
   }
   if(obj.index + col < light.length) {
    onOroff(light[obj.index + col]);
   }
   if(obj.index - col >= 0) {
    onOroff(light[obj.index - col]);
   }
   if(score == 0) {
    startOroff = true;
    confirmAdd++;
    alert("恭喜你過了第" + confirmAdd + '關(guān)');
    rightp.innerHTML = '第<div style="color:white; font-size:16px;display:inline-block; margin:0px 5px;border:1px solid black; padding:5px 10px;border-radius:20px">' + (confirmAdd + 1) + '</div>關(guān)';
    leftp.innerHTML = '還需關(guān)燈<div style="color:white; font-size:16px;display:inline-block; margin:0px 5px;border:1px solid black; padding:5px 10px;border-radius:20px">0</div>盞';
    but.innerHTML = '下一關(guān)';
    rand();
   }
  }
  function onOroff(obj) {
   if(obj.style.backgroundColor == beforeBg) {
    obj.style.backgroundColor = afterBg;
    obj.style.color = afterColor;
    obj.style.boxShadow = '3px 3px 15px yellow';
    obj.style.border = boxBordera;
    score++;
    leftp.innerHTML = '還需關(guān)燈<div style="color:white; font-size:16px;display:inline-block; margin:0px 5px;border:1px solid black; padding:5px 10px;border-radius:20px">' + score + '</div>盞';
   } else {
    obj.style.backgroundColor = beforeBg;
    obj.style.color = beforColor;
    obj.style.border = boxBorder;
    obj.style.boxShadow = '0px 0px 0px yellow';
    score--;
    leftp.innerHTML = '還需關(guān)燈<div style="color:white; font-size:16px;display:inline-block; margin:0px 5px;border:1px solid black; padding:5px 10px;border-radius:20px">' + score + '</div>盞';
   }
  }
  function boxclick() {
   music.play();
   if(startOroff) {
    alert('還沒開始游戲,請(qǐng)按下面開始按鈕開始游戲')
   } else {
    clickItem(this);
   }
  }
  function rand() {
   if(startOroff == true) {
    confirm = confirm + 3;
    for(var i = 0; i < confirm; i++) {
     var item = parseInt(Math.random() * (col * row - 1));
     clickItem(light[item]);
     gameMusic.play();
    }
   } else {
    alert('請(qǐng)先完成本關(guān)?。?!');
    return;
   }
   startOroff = false;
   vv = true;
  }
  
 </script>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、垂直對(duì)齊 如果你用CSS,則你會(huì)有困惑:我該怎么垂直對(duì)齊容器中的元素?現(xiàn)在,利用CSS3的Transform,...
    kiddings閱讀 3,297評(píng)論 0 11
  • 1、屬性選擇器:id選擇器 # 通過id 來選擇類名選擇器 . 通過類名來選擇屬性選擇器 ...
    Yuann閱讀 1,752評(píng)論 0 7
  • 游戲規(guī)則: 關(guān)燈游戲的原理是這樣的,如果我們有10x10共100盞燈,開始時(shí)都處于關(guān)閉狀態(tài),100盞燈全部點(diǎn)亮?xí)r,...
    檸檬草的幸運(yùn)閱讀 2,524評(píng)論 0 2
  • 有人說,生活一半是回憶,一半是現(xiàn)在。好像挺有道理,現(xiàn)在過得好,想想過去便可居安思危,現(xiàn)在過得不好,想過去只能徒增...
    何小兮閱讀 332評(píng)論 0 1
  • 提高可能性,明確關(guān)鍵性,增強(qiáng)渴望度。 弗魯姆認(rèn)為:激勵(lì)水平 = 期望值x效價(jià),也就是績效的可能性,和獎(jiǎng)勵(lì)的渴望度的...
    idyllis閱讀 820評(píng)論 0 0

友情鏈接更多精彩內(nèi)容