当前位置:首页 > VUE

vue实现打地鼠

2026-01-16 04:00:06VUE

Vue实现打地鼠游戏

打地鼠游戏是一种常见的互动小游戏,通过Vue的响应式特性和组件化开发可以高效实现。以下是实现步骤和代码示例:

游戏界面布局

使用Vue模板构建游戏界面,包含地鼠洞和计分板:

<template>
  <div class="game-container">
    <div class="score">得分: {{ score }}</div>
    <div class="holes">
      <div 
        v-for="(hole, index) in holes" 
        :key="index"
        class="hole"
        @click="hitMole(index)"
      >
        <div v-if="hole.active" class="mole"></div>
      </div>
    </div>
  </div>
</template>

游戏状态管理

在Vue的data中定义游戏状态变量:

vue实现打地鼠

data() {
  return {
    holes: Array(9).fill({ active: false }),
    score: 0,
    gameInterval: null,
    gameDuration: 30000,
    timeLeft: 30
  }
}

地鼠随机出现

使用定时器控制地鼠随机出现:

methods: {
  startGame() {
    this.gameInterval = setInterval(() => {
      const randomIndex = Math.floor(Math.random() * this.holes.length)
      this.$set(this.holes, randomIndex, { active: true })

      setTimeout(() => {
        this.$set(this.holes, randomIndex, { active: false })
      }, 1000)
    }, 800)

    setTimeout(() => {
      clearInterval(this.gameInterval)
    }, this.gameDuration)
  }
}

击中地鼠逻辑

实现点击地鼠得分功能:

vue实现打地鼠

methods: {
  hitMole(index) {
    if (this.holes[index].active) {
      this.score++
      this.$set(this.holes, index, { active: false })
    }
  }
}

样式设计

添加CSS样式增强游戏体验:

.game-container {
  max-width: 600px;
  margin: 0 auto;
}

.holes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

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

.mole {
  width: 80px;
  height: 80px;
  background: #000;
  border-radius: 50%;
  position: absolute;
  bottom: 0;
  left: 10px;
  transition: all 0.1s;
}

.score {
  font-size: 24px;
  margin-bottom: 20px;
}

游戏控制

添加开始按钮和倒计时功能:

<button @click="startGame">开始游戏</button>
<div>剩余时间: {{ timeLeft }}秒</div>
methods: {
  startGame() {
    this.score = 0
    this.timeLeft = 30

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

    // 原有游戏逻辑...
  }
}

完整组件代码

将上述代码整合为完整Vue组件:

<template>
  <div class="game-container">
    <div class="score">得分: {{ score }}</div>
    <div>剩余时间: {{ timeLeft }}秒</div>
    <button @click="startGame">开始游戏</button>

    <div class="holes">
      <div 
        v-for="(hole, index) in holes" 
        :key="index"
        class="hole"
        @click="hitMole(index)"
      >
        <div v-if="hole.active" class="mole"></div>
      </div>
    </div>
  </div>
</template>

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

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

      this.gameInterval = setInterval(() => {
        const randomIndex = Math.floor(Math.random() * this.holes.length)
        this.$set(this.holes, randomIndex, { active: true })

        setTimeout(() => {
          this.$set(this.holes, randomIndex, { active: false })
        }, 1000)
      }, 800)
    },
    hitMole(index) {
      if (this.holes[index].active) {
        this.score++
        this.$set(this.holes, index, { active: false })
      }
    }
  }
}
</script>

这个实现包含了游戏的核心功能,可以根据需要进一步扩展,如添加难度级别、音效、动画效果等。

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

相关文章

vue实现下拉菜单

vue实现下拉菜单

实现下拉菜单的基本思路 使用Vue实现下拉菜单可以通过动态绑定v-show或v-if控制菜单显示隐藏,结合事件监听(如@click或@mouseenter)触发状态切换。以下是两种常见实现方式:…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…

vue使用vr实现标注

vue使用vr实现标注

Vue 中使用 VR 实现标注的方法 在 Vue 项目中结合 VR 技术实现标注功能,可以通过 WebXR 和 Three.js 等库实现。以下为具体实现方法: 安装依赖 确保项目中已安装 Thr…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…