当前位置:首页 > 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实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…