当前位置:首页 > 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中定义游戏状态变量:

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)
  }
}

击中地鼠逻辑

实现点击地鼠得分功能:

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组件:

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 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue实现hover

vue实现hover

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

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…

vue实现多选

vue实现多选

Vue 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

vue实现xterm

vue实现xterm

在 Vue 中集成 Xterm.js Xterm.js 是一个基于 TypeScript 的前端终端组件库,可用于在浏览器中实现终端功能。以下是在 Vue 项目中集成 Xterm.js 的详细步骤。…