当前位置:首页 > VUE

vue实现打字游戏

2026-03-09 06:04:12VUE

实现思路

使用Vue实现打字游戏的核心在于动态渲染文本、监听用户输入、匹配字符并实时反馈。需要结合Vue的响应式数据绑定和键盘事件监听。

vue实现打字游戏

核心代码结构

<template>
  <div class="typing-game">
    <div class="target-text" ref="targetText">
      <span 
        v-for="(char, index) in targetChars" 
        :key="index"
        :class="getCharClass(index)"
      >{{ char }}</span>
    </div>
    <input 
      type="text" 
      v-model="userInput" 
      @input="checkInput"
      placeholder="开始输入..."
    />
    <div class="stats">
      进度: {{ currentIndex }}/{{ targetChars.length }} | 
      正确率: {{ accuracy }}%
    </div>
  </div>
</template>

数据与逻辑实现

<script>
export default {
  data() {
    return {
      targetText: '这是一个示例文本,用于打字游戏练习。',
      userInput: '',
      currentIndex: 0,
      correctCount: 0
    }
  },
  computed: {
    targetChars() {
      return this.targetText.split('')
    },
    accuracy() {
      return this.currentIndex > 0 
        ? Math.round((this.correctCount / this.currentIndex) * 100)
        : 0
    }
  },
  methods: {
    checkInput() {
      const lastChar = this.userInput.slice(-1)
      if (lastChar === this.targetChars[this.currentIndex]) {
        this.correctCount++
      }
      this.currentIndex++

      if (this.currentIndex >= this.targetChars.length) {
        alert(`游戏结束!正确率: ${this.accuracy}%`)
        this.resetGame()
      }
    },
    getCharClass(index) {
      return {
        'correct': index < this.currentIndex && 
                 this.userInput[index] === this.targetChars[index],
        'incorrect': index < this.currentIndex && 
                    this.userInput[index] !== this.targetChars[index],
        'current': index === this.currentIndex
      }
    },
    resetGame() {
      this.userInput = ''
      this.currentIndex = 0
      this.correctCount = 0
    }
  }
}
</script>

样式设计

<style scoped>
.typing-game {
  font-family: monospace;
  font-size: 24px;
  max-width: 800px;
  margin: 0 auto;
}

.target-text span {
  transition: all 0.2s;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}

.current {
  background-color: yellow;
}

input {
  width: 100%;
  padding: 10px;
  font-size: 20px;
  margin-top: 20px;
}

.stats {
  margin-top: 10px;
  font-size: 16px;
}
</style>

功能扩展建议

  1. 增加难度选择:提供不同长度的文本或专业词汇库
  2. 计时功能:计算完成时间并显示打字速度(字/分钟)
  3. 随机文本:从API获取随机文本或使用用户自定义文本
  4. 音效反馈:正确/错误输入时播放不同音效
  5. 进度保存:使用localStorage保存用户最佳成绩

性能优化点

  1. 避免在模板中使用复杂的计算,将逻辑移到computed属性
  2. 对于超长文本,考虑虚拟滚动技术
  3. 使用防抖处理高频输入事件
  4. 预加载可能用到的所有文本资源

完整实现时可根据实际需求调整样式和交互细节,核心机制是通过比较用户输入与目标文本的字符来实现打字检测。

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

相关文章

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue 实现tabs

vue 实现tabs

Vue 实现 Tabs 组件的方法 使用动态组件和 v-for 在 Vue 中实现 Tabs 可以通过动态组件和 v-for 指令结合完成。定义一个 tabs 数组,包含每个标签的标题和对应的组件。…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…