当前位置:首页 > VUE

vue实现打地鼠

2026-03-08 17:23:40VUE

实现思路

使用Vue的响应式数据和事件绑定功能,构建一个简单的打地鼠游戏界面。通过随机生成地鼠出现的位置,玩家点击地鼠得分,计时结束后统计总分。

代码实现

<template>
  <div class="game-container">
    <h2>打地鼠游戏</h2>
    <div class="info">
      <span>得分: {{ score }}</span>
      <span>时间: {{ timeLeft }}秒</span>
    </div>
    <div class="game-board">
      <div 
        v-for="(hole, index) in holes" 
        :key="index" 
        class="hole"
        @click="hitMole(index)"
      >
        <div 
          v-if="hole.active" 
          class="mole"
        ></div>
      </div>
    </div>
    <button 
      v-if="!gameStarted" 
      @click="startGame"
    >
      开始游戏
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      holes: Array(9).fill().map(() => ({ active: false })),
      score: 0,
      timeLeft: 30,
      gameStarted: false,
      timer: null,
      moleInterval: null
    }
  },
  methods: {
    startGame() {
      this.gameStarted = true
      this.score = 0
      this.timeLeft = 30

      this.timer = setInterval(() => {
        this.timeLeft--
        if (this.timeLeft <= 0) {
          this.endGame()
        }
      }, 1000)

      this.moleInterval = setInterval(() => {
        this.popRandomMole()
      }, 800)
    },
    popRandomMole() {
      // 先重置所有洞
      this.holes.forEach(hole => {
        hole.active = false
      })

      // 随机选择一个洞显示地鼠
      const randomIndex = Math.floor(Math.random() * this.holes.length)
      this.holes[randomIndex].active = true

      // 1秒后地鼠消失
      setTimeout(() => {
        this.holes[randomIndex].active = false
      }, 1000)
    },
    hitMole(index) {
      if (this.holes[index].active) {
        this.score++
        this.holes[index].active = false
      }
    },
    endGame() {
      clearInterval(this.timer)
      clearInterval(this.moleInterval)
      this.gameStarted = false
      alert(`游戏结束! 你的得分是: ${this.score}`)
    }
  }
}
</script>

<style>
.game-container {
  max-width: 500px;
  margin: 0 auto;
  text-align: center;
}

.info {
  display: flex;
  justify-content: space-around;
  margin: 20px 0;
}

.game-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.hole {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: #8B4513;
  border-radius: 50%;
  cursor: pointer;
}

.mole {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 60px;
  background-color: #A0522D;
  border-radius: 50%;
}

button {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
}

button:hover {
  background-color: #45a049;
}
</style>

功能说明

  1. 游戏初始化

    • 创建9个洞的数组,每个洞初始状态为不活跃(无地鼠)
    • 设置初始得分0,剩余时间30秒
  2. 开始游戏

    • 点击开始按钮后启动游戏
    • 设置计时器每秒减少剩余时间
    • 设置地鼠随机出现间隔(800毫秒)
  3. 地鼠逻辑

    • 随机选择一个洞显示地鼠
    • 1秒后地鼠自动消失
    • 玩家点击活跃的地鼠得分
  4. 游戏结束

    vue实现打地鼠

    • 时间归零时清除所有定时器
    • 显示最终得分

优化建议

  1. 增加难度系统,随着游戏进行加快地鼠出现速度
  2. 添加音效增强游戏体验
  3. 实现地鼠不同状态(出现/被打中)的动画效果
  4. 添加最高分记录功能
  5. 增加游戏关卡设置选项

这个实现使用了Vue的核心功能,包括数据绑定、事件处理和条件渲染,适合作为Vue初学者的练习项目。可以根据需要进一步扩展功能或优化界面。

标签: 打地鼠vue
分享给朋友:

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…