当前位置:首页 > VUE

vue实现打字游戏

2026-02-17 17:32:05VUE

Vue 实现打字游戏的核心思路

使用 Vue 的数据绑定和响应式特性,结合键盘事件监听,实现动态文字输入检测与评分功能。关键点包括随机生成文本、实时匹配输入、计时与得分计算。

基础代码结构

<template>
  <div class="typing-game">
    <h3>剩余时间: {{ timeLeft }}秒</h3>
    <p class="target-text">{{ targetText }}</p>
    <input 
      v-model="userInput" 
      @input="checkInput"
      placeholder="开始输入..."
    />
    <p>得分: {{ score }}</p>
  </div>
</template>

核心功能实现

<script>
export default {
  data() {
    return {
      targetText: '',
      userInput: '',
      timeLeft: 60,
      score: 0,
      wordList: ['前端开发', 'Vue框架', '响应式编程', '组合式API', '虚拟DOM']
    }
  },
  mounted() {
    this.generateText();
    this.startTimer();
  },
  methods: {
    generateText() {
      this.targetText = this.wordList[
        Math.floor(Math.random() * this.wordList.length)
      ];
    },

    checkInput() {
      if (this.userInput === this.targetText) {
        this.score += 10;
        this.userInput = '';
        this.generateText();
      }
    },

    startTimer() {
      const timer = setInterval(() => {
        this.timeLeft--;
        if (this.timeLeft <= 0) {
          clearInterval(timer);
          alert(`游戏结束! 最终得分: ${this.score}`);
        }
      }, 1000);
    }
  }
}
</script>

样式优化建议

<style scoped>
.typing-game {
  font-family: Arial;
  max-width: 600px;
  margin: 0 auto;
  text-align: center;
}

.target-text {
  font-size: 24px;
  letter-spacing: 1px;
  margin: 20px 0;
}

input {
  width: 80%;
  padding: 10px;
  font-size: 16px;
}
</style>

高级功能扩展

动态难度调整
根据得分动态增加单词长度:

vue实现打字游戏

if (this.score > 30) {
  this.wordList.push('组件化开发思想', '状态管理Vuex');
}

错误高亮显示
添加错误字符样式提示:

<p class="target-text">
  <span 
    v-for="(char, index) in targetText" 
    :class="{ 'error': userInput[index] && userInput[index] !== char }"
  >
    {{ char }}
  </span>
</p>

音效反馈
正确输入时播放提示音:

vue实现打字游戏

const audio = new Audio('correct.mp3');
audio.play();

性能优化建议

使用 Vue 的 computed 属性优化显示逻辑:

computed: {
  displayText() {
    return this.targetText.split('').map((char, i) => {
      return {
        char,
        isCorrect: this.userInput[i] === char
      };
    });
  }
}

游戏结束后禁用输入:

<input :disabled="timeLeft <= 0" />

注意事项

  1. 移动端需要额外处理虚拟键盘事件
  2. 考虑添加游戏重启按钮
  3. 对于长文本建议实现自动滚动
  4. 本地存储可用于保存最高分记录

通过以上方法可以构建一个完整的打字游戏,可根据需求进一步扩展多语言支持、用户等级系统等功能。

标签: 游戏vue
分享给朋友:

相关文章

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <te…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

自己实现vue

自己实现vue

实现简易版 Vue.js 核心功能 要实现一个简易版 Vue.js,需要理解其核心功能:数据响应式、模板编译、依赖收集和虚拟 DOM。以下分模块实现关键功能。 数据响应式(Reactivity) 通…