欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生JS實(shí)現(xiàn)點(diǎn)擊數(shù)字小游戲

 更新時(shí)間:2021年04月22日 14:55:54   作者:@Coollin  
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)點(diǎn)擊數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

原生JS實(shí)現(xiàn)點(diǎn)擊數(shù)字小游戲,供大家參考,具體內(nèi)容如下

最近公司在季度測(cè)試中出了一道很有趣的測(cè)試題,要求使用我們自己的黑科技–IVX來(lái)實(shí)現(xiàn),感興趣的朋友可以去了解哦,是真的黑科技,在這里我還是用原生JS來(lái)實(shí)現(xiàn)吧,題目是這樣的:

實(shí)現(xiàn)一個(gè)點(diǎn)擊數(shù)字的小游戲:依次點(diǎn)擊容器中隨機(jī)生成的數(shù)字元素,生成的數(shù)字元素會(huì)在5S后消失,你將憑借記憶點(diǎn)擊按照數(shù)字升序依次點(diǎn)擊生成的數(shù)字方可通過(guò)該關(guān)卡游戲。

話(huà)不多說(shuō)直接看運(yùn)行效果圖:

上代碼:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>點(diǎn)擊數(shù)字小游戲</title>
    <style>
      #cointainer {
        margin: auto;
        height: 600px;
        width: 400px;
        background-color: rgb(37, 37, 37);
        position: relative;
      }
      .header {
        width: auto;
        text-align: center;
        margin: auto;
      }
      .parm {
        height: 60px;
        width: 60px;
        border-radius: 30px;
        position: absolute;
        text-align: center;
        line-height: 60px;
      }
      .parm:hover {
        cursor: pointer;
      }
      .todo {
        text-align: center;
        margin-top: 16px;
      }
      button {
        width: 100px;
        height: 30px;
        background-color: coral;
        border: none;
        outline: none;
      }
    </style>
  </head>
  <body>
    <div class="header">
      <h1>點(diǎn)擊數(shù)字小游戲</h1>
      <p>
        5s后數(shù)字內(nèi)容會(huì)消失,憑借你的記憶按照數(shù)字升序依次點(diǎn)擊數(shù)字點(diǎn)可順利通關(guān)
      </p>
    </div>
    <div id="cointainer"></div>
    <div class="todo">
      <button onclick="restart(6)">重新開(kāi)始</button>
      <button style="margin-left: 20px" onclick="nextPass()">下一關(guān)</button>
      <button
        style="margin-left: 20px"
        onclick="window.clearInterval(timmer2);window.clearTimeout(timmer1)"
      >
        停止計(jì)時(shí)
      </button>
      <p>時(shí)間</p>
    </div>
  </body>
  <script>
    let circleList = [];
    //circle構(gòu)造器
    function getPosition() {
      let parm = { x: "", y: "" };
      parm.x = Math.round(Math.random() * 340);
      parm.y = Math.round(Math.random() * 540);
      return parm;
    }
    //創(chuàng)建不重疊circle
    function createCircle(total) {
      if (circleList.length === 0) {
        circleList.push(getPosition());
      }
      //限制創(chuàng)建次數(shù)200
      for (let i = 0; i < 200; i++) {
        if (circleList.length < total) {
          let circle = getPosition();
          let distan = [];
          for (let n = 0; n < circleList.length; n++) {
            let dis =
              Math.abs(circle.x - circleList[n].x) ** 2 +
              Math.abs(circle.y - circleList[n].y) ** 2;
            distan.push(dis);
          }
          if (Math.min(...distan) > 3600) {
            circleList.push(circle);
          }
        } else {
          break;
        }
      }
    }
    //創(chuàng)建8個(gè)circle
    createCircle(8);
    //隨機(jī)顏色選擇器
    function selectColor() {
      let r = 100 + Math.round(Math.random() * 155);
      let g = 100 + Math.round(Math.random() * 155);
      let b = 100 + Math.round(Math.random() * 155);
      return `rgb(${r},${g},$)`;
    }
    //在DOM中創(chuàng)建circle
    let containner = document.getElementById("cointainer");
    //構(gòu)造關(guān)卡
    function creatGame(num) {
      circleList = [];
      createCircle(num);
      for (let i = 0; i < circleList.length; i++) {
        let node = document.createElement("span");
        containner.appendChild(node);
        node.className = "parm";
        node.innerText = i + 1;
        node.style.left = circleList[i].x + "px";
        node.style.top = circleList[i].y + "px";
        node.style.backgroundColor = selectColor();
      }
    }
    //點(diǎn)擊答案
    let asw = [];
    //設(shè)置5s后開(kāi)始游戲
    let start = function () {
      let list = document.querySelectorAll("span");
      let right = "";
      for (let i = 0; i < list.length; i++) {
        list[i].innerText = "";
        list[i].number = i + 1;
        right = right + (i + 1);
        list[i].addEventListener(
          "click",
          function () {
            asw.push(list[i].number);
            if (asw.length === pass && asw.join("") === right) {
              window.clearInterval(timmer2);
              alert("恭喜過(guò)關(guān),你的用時(shí)為:" + time.toFixed(2) + "s");
              asw = [];
            } else if (asw.length === pass && asw.join("") !== right) {
              asw = [];
              window.clearInterval(timmer2);
              alert("抱歉沒(méi)能過(guò)關(guān)");
            }
          },
          false
        );
      }
    };
    let time = 0;
    let sumTime = function () {
      time = time + 0.01;
      document.querySelectorAll("p")[1].innerText = time.toFixed(2) + "s";
    };
    //初始關(guān)卡
    let pass = 6;
    creatGame(pass);
    let timmer1 = setTimeout(start, 5000);
    let timmer2 = setInterval(sumTime, 10);
    //重新開(kāi)始
    function restart(nowerPass) {
      while (containner.hasChildNodes()) {
        containner.removeChild(containner.firstChild);
      }
      pass = nowerPass;
      creatGame(nowerPass);
      clearTimeout(timmer1);
      clearInterval(timmer2);
      time = 0;
      timmer1 = setTimeout(start, 5000);
      timmer2 = setInterval(sumTime, 10);
    }
    //下一關(guān)
    function nextPass() {
      if (pass < 20) {
        pass++;
        restart(pass);
      }
    }
  </script>
</html>

至此一個(gè)很有趣的鍛煉大腦邏輯的小游戲分享完畢。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js 獲取radio按鈕值的實(shí)例

    js 獲取radio按鈕值的實(shí)例

    這篇文章介紹了js 獲取radio按鈕值的實(shí)例,有需要的朋友可以參考一下
    2013-08-08
  • JavaScript cookie原理及使用實(shí)例

    JavaScript cookie原理及使用實(shí)例

    這篇文章主要介紹了JavaScript cookie原理及使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • js+css實(shí)現(xiàn)扇形導(dǎo)航效果

    js+css實(shí)現(xiàn)扇形導(dǎo)航效果

    這篇文章主要為大家詳細(xì)介紹了js+css實(shí)現(xiàn)扇形導(dǎo)航效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 微信小程序簡(jiǎn)潔登錄頁(yè)面的實(shí)現(xiàn)(附源碼)

    微信小程序簡(jiǎn)潔登錄頁(yè)面的實(shí)現(xiàn)(附源碼)

    本文主要介紹了微信小程序簡(jiǎn)潔登錄頁(yè)面的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 懸浮數(shù)字的實(shí)現(xiàn)案例

    懸浮數(shù)字的實(shí)現(xiàn)案例

    本篇文章主要是對(duì)懸浮數(shù)字的實(shí)現(xiàn)案例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2014-02-02
  • 前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的詳細(xì)過(guò)程

    前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的詳細(xì)過(guò)程

    Compressor.js一個(gè)JavaScript圖像壓縮器,使用瀏覽器的原生canvas.toBlob API來(lái)執(zhí)行壓縮工作,這篇文章主要給大家介紹了關(guān)于前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的詳細(xì)過(guò)程,需要的朋友可以參考下
    2024-07-07
  • 用JS寫(xiě)的一個(gè)TableView控件代碼

    用JS寫(xiě)的一個(gè)TableView控件代碼

    JS寫(xiě)的一個(gè)TableView控件代碼,方便輸出表格。
    2010-01-01
  • js實(shí)現(xiàn)星星打分效果的方法

    js實(shí)現(xiàn)星星打分效果的方法

    這篇文章主要介紹了js實(shí)現(xiàn)星星打分效果的方法,涉及javascript操作頁(yè)面元素與樣式的技巧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-04-04
  • vue渲染大量數(shù)據(jù)時(shí)卡頓卡死解決方法

    vue渲染大量數(shù)據(jù)時(shí)卡頓卡死解決方法

    這篇文章主要介紹了vue渲染大量數(shù)據(jù)時(shí)發(fā)生卡頓卡死問(wèn)題時(shí)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • js對(duì)象繼承之原型鏈繼承實(shí)例

    js對(duì)象繼承之原型鏈繼承實(shí)例

    這篇文章主要介紹了js對(duì)象繼承之原型鏈繼承,以實(shí)例形式分析了原型鏈繼承的實(shí)現(xiàn)方法與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01

最新評(píng)論