Skip to content

打字通小游戏制作教程:用HTML5和JavaScript提升打字速度

创建HTML结构

打开你的文本编辑器,创建一个新的HTML文件,并输入以下代码:

html
<!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>洛可可白打字通</title>
    <style>
        /* 在这里添加CSS样式 */
    </style>
</head>
<body>
    <div class="bigBox">
        <div class="container">你准备好了吗?</div>
        <textarea placeholder="开始输入..." style="resize: none" cols="30" rows="10"></textarea>
        <div class="operate">
            <button>开始</button>
            <div id="timer">60</div>
        </div>
    </div>
    <script>
        // 在这里添加JavaScript代码
    </script>
</body>
</html>

这是我们游戏的基本结构。<head>部分包含了页面的元数据和样式定义,<body>部分则是游戏的主要内容。

添加CSS样式

<style>标签内,我们将添加一些CSS样式来美化我们的打字通游戏。这包括游戏容器、文本区域、按钮和计时器的样式。

css
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -khtml-user-select: none;
    -o-user-select: none;
    user-select: none;
}

.bigBox {
    width: 50%;
    background-color: #ac8c3e;
    margin: 40px auto;
    box-sizing: border-box;
    padding: 20px;
    border-radius: 30px;
    box-shadow: 0px 0px 30px 9px #939393;
}

.container {
    margin: 0 auto;
    text-align: center;
    padding: 20px;
}

textarea {
    width: 100%;
    height: 200px;
    margin: 20px 0;
    font-size: 20px;
    border: none;
}

.operate {
    width: 20%;
    margin: 0 auto;
    text-align: center;
}

button {
    font-size: 24px;
    padding: 10px 20px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

#timer {
    font-size: 48px;
    margin: 20px;
}

编写JavaScript逻辑

现在,我们将在<script>标签内添加JavaScript代码,这是游戏的核心部分。我们将创建游戏文本、初始化游戏、处理用户输入、实现倒计时以及计算得分。

javascript
const text = "Believe in yourself and all that you are..."; // 游戏文本
const container = document.querySelector(".container");
const input = document.querySelector("textarea");
const button = document.querySelector("button");
const timer = document.getElementById("timer");
input.value = "";

let countdown;

function startGame() {
    // 游戏开始后,禁用按钮
    button.disabled = true;

    // 显示文本
    container.textContent = text;

    // 启动倒计时
    countdown = setInterval(() => {
        const remainingTime = parseInt(timer.textContent) - 1;
        if (remainingTime === 0) {
            // 时间用完,游戏结束
            endGame();
        }

        timer.textContent = remainingTime;
    }, 1000);
}

function endGame() {
    // 停止倒计时
    clearInterval(countdown);

    // 计算得分
    const score = calculateScore();
    const scoreMessage = `你的得分是 ${score} 分!`;
    container.textContent = scoreMessage;

    button.disabled = false;
}

function calculateScore() {
    const userText = input.value.trim();
    const correctText = text.trim();
    const userWords = userText.split(" ");
    const correctWords = correctText.split(" ");
    let score = 0;

    for (let i = 0; i < userWords.length; i++) {
        if (userWords[i] === correctWords[i]) {
            score++;
        }
    }

    return score;
}

// 添加按钮点击事件监听器
button.addEventListener("click", () => {
    // 设置倒计时时间
    timer.textContent = "60";

    // 清空输入框和输出文本区域
    input.value = "";
    container.textContent = "";

    // 启动游戏
    startGame();
});

在这个脚本中,我们首先定义了游戏的文本。然后,我们创建了开始游戏的函数startGame,它将显示游戏文本并启动倒计时。我们还定义了结束游戏的函数endGame,它将停止倒计时并计算得分。calculateScore函数用于计算用户的得分。最后,我们为开始按钮添加了一个点击事件监听器,当用户点击按钮时,游戏将开始。

测试游戏

保存你的HTML文件,并在浏览器中打开它。你应该能看到一个打字通游戏的界面。点击“开始”按钮,游戏将开始,你可以尝试在限定时间内尽可能准确地输入显示的文本。时间结束后,你的得分将被计算出来。

全部代码

html
<!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>洛可可白⚡️打字通</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        -moz-user-select: none;
        /*火狐*/
        -webkit-user-select: none;
        /*webkit浏览器*/
        -ms-user-select: none;
        /*IE10*/
        -khtml-user-select: none;
        /*早期浏览器*/
        -o-user-select: none;
        user-select: none;
      }

      .bigBox {
        width: 50%;
        background-color: #ac8c3e;
        margin: 40px auto;
        box-sizing: border-box;
        padding: 20px;
        border-radius: 30px;
        box-shadow: 0px 0px 30px 9px #939393;
      }

      .container {
        margin: 0 auto;
        text-align: center;
        padding: 20px;
      }

      textarea {
        width: 100%;
        height: 200px;
        margin: 20px 0;
        font-size: 20px;
        border: none;
      }

      .operate {
        width: 20%;
        margin: 0 auto;
        text-align: center;
      }

      button {
        font-size: 24px;
        padding: 10px 20px;
        background-color: #007bff;
        color: #fff;
        border: none;
        border-radius: 5px;
        cursor: pointer;
      }

      #timer {
        font-size: 48px;
        margin: 20px;
      }
    </style>
  </head>

  <body>
    <div class="bigBox">
      <div class="container">你准备好了吗?</div>
      <textarea
        name=""
        placeholder="开始输入..."
        id=""
        style="resize: none"
        cols="30"
        rows="10"
      ></textarea>
      <div class="operate">
        <button>开始</button>
        <div id="timer">60</div>
      </div>
    </div>

    <script>
      const text =
        "Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle. This quote by Christian D. Larson reminds us that we all have the power within us to overcome any obstacle we may face. When we have confidence in ourselves and our abilities, we can achieve great things. So, let's trust ourselves, believe in our dreams, and work hard to make them a reality.";

      const container = document.querySelector(".container");
      const input = document.querySelector("textarea");
      const button = document.querySelector("button");
      const timer = document.getElementById("timer");
      input.value = "";

      let countdown;

      function startGame() {
        // 游戏开始后,禁用按钮
        button.disabled = true;

        // 显示文本
        container.textContent = text;

        // 启动倒计时
        countdown = setInterval(() => {
          const remainingTime = parseInt(timer.textContent) - 1;
          if (remainingTime === 0) {
            // 时间用完,游戏结束
            endGame();
          }

          timer.textContent = remainingTime;
        }, 1000);
      }

      function endGame() {
        // 停止倒计时
        clearInterval(countdown);

        // 计算得分
        const score = calculateScore();
        const scoreMessage = `你的得分是 ${score} 分!`;
        container.textContent = scoreMessage;

        button.disabled = false;
      }

      function calculateScore() {
        const userText = input.value.trim();
        const correctText = text.trim();
        const userWords = userText.split(" ");
        const correctWords = correctText.split(" ");
        let score = 0;

        for (let i = 0; i < userWords.length; i++) {
          console.log(userWords[i], correctWords[i]);
          if (userWords[i] === correctWords[i]) {
            score++;
          }
        }

        return score;
      }

      // 添加按钮点击事件监听器
      button.addEventListener("click", () => {
        // 设置倒计时时间
        timer.textContent = "60";

        // 清空输入框和输出文本区域
        input.value = "";
        container.textContent = "";

        // 启动游戏
        startGame();
      });
    </script>
  </body>
</html>