使用html5+css+js制作一个猜数字密码的小游戏

这是编写完打开之后的图片

代码给你们咯(拿完代码不要忘记点赞哟))

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>猜密码游戏</title>
  <style>
    body {
      font-family: "Comic Sans MS", cursive, sans-serif;
      background-color: #fdeff9;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    .container {
      background-color: #fff;
      border-radius: 20px;
      padding: 20px;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
      width: 320px;
      text-align: center;
    }

    h1 {
      margin-top: 0;
      color: #ff0099;
      font-size: 30px;
      text-shadow: 2px 2px 2px #ff99cc;
    }

    .clues-container {
      margin-bottom: 20px;
    }

    .form-container {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .password-input {
      width: 200px;
      padding: 10px;
      margin-bottom: 20px;
      border: 2px solid #ff99cc;
      border-radius: 20px;
      font-size: 18px;
    }

    .submit-button {
      padding: 10px 30px;
      background-color: #ff0099;
      color: #fff;
      border: none;
      cursor: pointer;
      border-radius: 20px;
      font-size: 20px;
      text-shadow: 1px 1px 1px #ff99cc;
    }

    .submit-button:hover {
      background-color: #ff33cc;
    }

    .error-message {
      color: #ff0000;
      margin-top: 10px;
      font-size: 16px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>猜密码游戏</h1>
    <div id="clues" class="clues-container">
      <!-- 线索将显示在这里 -->
    </div>
    <div id="passwordForm" class="form-container">
      <label for="passwordInput">请输入4位数字密码:</label>
      <input type="text" id="passwordInput" class="password-input" pattern="[0-9]{4}" maxlength="4" autocomplete="off" required>
      <button onclick="checkPassword()" class="submit-button">提交</button>
      <div id="error" class="error-message" aria-live="assertive"></div>
    </div>
  </div>
  <script>
    const password = Math.floor(1000 + Math.random() * 9000).toString(); // 生成一个随机的4位数字密码
    const cluesDiv = document.getElementById('clues');
    const errorDiv = document.getElementById('error');

    function checkPassword() {
      const guess = document.getElementById('passwordInput').value;
      if (guess.length !== 4 || isNaN(guess)) {
        showError('请输入有效的4位数字。');
        return;
      }

      let correctDigits = 0;
      for (let i = 0; i < 4; i++) {
        if (guess[i] === password[i]) {
          correctDigits++;
        }
      }

      if (correctDigits === 4) {
        showSuccess('恭喜!您猜对了密码:' + password);
      } else {
        showClue(`您的猜测 ${guess} 有 ${correctDigits} 个数字正确。`);
      }
    }

    function showError(message) {
      errorDiv.textContent = message;
      errorDiv.style.display = 'block';
    }

    function showSuccess(message) {
      alert(message);
    }

    function showClue(clue) {
      const clueParagraph = document.createElement('p');
      clueParagraph.textContent = clue;
      cluesDiv.appendChild(clueParagraph);
    }
  </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2201_75396384

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值