当前位置:首页 > VUE

vue实现打地鼠

2026-03-29 04:26:36VUE

Vue 实现打地鼠游戏

游戏逻辑设计

打地鼠游戏的核心逻辑包括随机生成地鼠、玩家点击得分、计时和计分系统。在Vue中可以通过数据驱动的方式实现这些功能。

vue实现打地鼠

创建Vue组件

新建一个Vue组件,命名为WhackAMole.vue,包含游戏区域、计分板和计时器。

vue实现打地鼠

<template>
  <div class="game-container">
    <h2>打地鼠游戏</h2>
    <div class="score-board">
      <span>得分: {{ score }}</span>
      <span>剩余时间: {{ timeLeft }}秒</span>
    </div>
    <div class="game-board">
      <div 
        v-for="(hole, index) in holes" 
        :key="index" 
        class="hole"
        @click="whack(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({ active: false }),
      score: 0,
      timeLeft: 30,
      gameStarted: false,
      gameInterval: null,
      moleInterval: null
    }
  },
  methods: {
    startGame() {
      this.gameStarted = true
      this.score = 0
      this.timeLeft = 30

      this.gameInterval = setInterval(() => {
        this.timeLeft--
        if (this.timeLeft <= 0) {
          clearInterval(this.gameInterval)
          clearInterval(this.moleInterval)
          this.gameStarted = false
        }
      }, 1000)

      this.moleInterval = setInterval(this.popUpMole, 1000)
    },

    popUpMole() {
      // 随机选择一个洞显示地鼠
      const randomIndex = Math.floor(Math.random() * this.holes.length)
      this.holes = this.holes.map((hole, index) => 
        index === randomIndex ? { active: true } : { active: false }
      )

      // 1秒后地鼠消失
      setTimeout(() => {
        this.holes = this.holes.map(hole => ({ active: false }))
      }, 1000)
    },

    whack(index) {
      if (this.holes[index].active) {
        this.score++
        this.holes = this.holes.map(hole => ({ active: false }))
      }
    }
  }
}
</script>

样式设计

添加CSS样式使游戏界面更美观:

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

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

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

.mole {
  width: 60px;
  height: 60px;
  background-color: #654321;
  border-radius: 50%;
  position: absolute;
  top: 10px;
  left: 10px;
}

.score-board {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
}

button {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
</style>

游戏优化建议

  1. 增加游戏难度随时间递增的功能,可以缩短地鼠出现的时间间隔
  2. 添加音效增强游戏体验,如击中地鼠的声音
  3. 实现不同地鼠类型,设置不同分值
  4. 添加游戏结束后的排行榜功能

完整实现要点

  • 使用Vue的响应式数据管理游戏状态
  • 通过setIntervalsetTimeout控制游戏流程
  • 利用数组方法更新地鼠位置
  • 使用网格布局创建游戏棋盘
  • 通过条件渲染显示/隐藏地鼠

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

相关文章

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现select

vue实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML select 元素 原生 HTML 的 <sel…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…