当前位置:首页 > 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播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取v…

vue实现按卡片轮播

vue实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-for…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null }…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 TreeS…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…