javascript - 设置 cookie 来存储游戏数据

标签 javascript html css

我正在为我的同学开发一个唱首歌游戏,但我不知道如何工作/使用 cookie 来保存计算机上的数据,我希望能够通过 cookie 存储人们进度的内容/例如他们的自己的电脑。我用来帮助编写代码的程序是 glitch.com 我也想帮忙启用一个功能来显示其他人在线的进度,并帮助配置一个非作弊系统

<html>
   <head>   
      <script type = "text/javascript">
         <!--
            function WriteCookie() {
               if( document.myform.customer.value == "0" ) {
                  alert("Enter Password value!");
                  return;
               }
               cookievalue = escape(document.myform.customer.value) + ";";
               document.cookie = "name=" + cookievalue;
               document.write ("Setting Cookies : " + "name=" + cookievalue );
            }
         //-->
      </script>      
   </head>

   <body>      
      <form name = "myform" action = "">
         Enter name: <input type = "text" name = "customer"/>
         <input type = "button" value = "Set Cookie" onclick = "WriteCookie();"/>
      </form>   
   </body>
</html>
<html>
  <body>
    <script> src="Save_File.js" </script>

    <img
      width="60"
      height="50"
      src="https://media.giphy.com/media/U7qVJMN039tGU/giphy.gif"
      style="
   position: absolute;
   top: 98px;
   left: 413px;
"
    />
    <img
    width="50"
    height="50"     
         src="https://media.giphy.com/media/D8nU1wFQ62aZ2/giphy.gif"
      style="
   position: absolute;
   top: 116px;
   left: 187px;
"
    />
    <p id="cool"></p>
    <p id="press"></p>
    <p id="ss"></p>
    <button type="button" id="cool" onclick="Click()">OwO</button>
    <button
      type="button"
      id="DPS"
      onclick="DPS()"
      style="
   position: absolute;
   top: 20;
   left: 240px;
"
    >
      Buy Auto click cost:
    </button>
    <p
      id="DPSprice"
      style="
   position: absolute;
   top: 105px;
   left: 350px;
"
    ></p>
    <button
      type="button"
      id="ClickDM"
      onclick="ClickDM()"
      style="
   position: absolute;
   top: 20;
   left: 478px;
"
    >
      Buy OwO Click cost:
    </button>
    <p
      id="addCost"
      style="
   position: absolute;
   top: 105px;
   left: 620px;
"
    ></p>
    <script>
      function Click() {
        press += addPress;
        document.getElementById("cool").innerHTML = "You've OwO'd that button:";
        document.getElementById("press").innerHTML = press;
        document.getElementById("ss").innerHTML = "time(s)";
        document.getElementById("DPSprice").innerHTML = DPSprice;
        document.getElementById("addCost").innerHTML = addCost;
      }
      function DPS() {
        if (press > DPSprice) {
          press -= DPSprice;
          DPSprice *= 3;
          document.getElementById("press").innerHTML = press;
          document.getElementById("DPSprice").innerHTML = DPSprice;
          window.setInterval(function() {
            DPSadd();
            Refresh();
          }, 2000);
        }
      }
      function DPSadd() {
        press += 1;
      }
      function ClickDM() {
        if (press > addCost) {
          press -= addCost;
          addCost *= 2;
          addPress += 1;
          document.getElementById("press").innerHTML = press;
          document.getElementById("addCost").innerHTML = addCost;
        }
      }
      function Refresh() {
        document.getElementById("cool").innerHTML =
          "The bot has pressed that button:";
        document.getElementById("press").innerHTML = press;
        document.getElementById("ss").innerHTML = "time(s)";
        document.getElementById("DPSprice").innerHTML = DPSprice;
        document.getElementById("addCost").innerHTML = addCost;
      }
      var press = 0;
      var DPSprice = 3;
      var addPress = 1;
      var addCost = 50;
    </script>
    <script src="/check.js" defer></script>
  </body>
</html>```

最佳答案

您可以将其用于您的 cookie。

有关如何写入、读取、更改和删除一个或多个 cookie 的更多信息,请访问 https://www.w3schools.com/js/js_cookies.asp

function WriteCookie() {
  // the next 4 lines: why should someone enter "0" ?
  if( document.myform.customer.value == "0" ) {
    alert("Enter Password value!");
    return;
  }
  cookievalue = escape(document.myform.customer.value);
  document.cookie = "name="+cookievalue+"; expires=Fri, 25 Dec 2037 23:59:59 GMT; path=/";
  console.log("name="+cookievalue+"; expires=Fri, 25 Dec 2037 23:59:59 GMT; path=/");
}

关于javascript - 设置 cookie 来存储游戏数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59218476/

相关文章:

javascript - 如何使用 jQuery 获取表中每一列的值?

jquery - CSS 背景图片居中对齐

javascript - 调整窗口大小时重置 JQuery 函数

javascript - 使用 js 和 underscore.js 显示代码时出现问题

JavaScript 阴影与初始化

javascript - 自动将 JSON 对象中的值应用于输入标签

javascript - 我怎么能 "turn off"样式表?

javascript - 如何从 Canvas 中删除对象?

html - 偏移图像的边框

javascript - 自动填充选项标签不工作 javascript