当前位置:首页 > 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. 游戏初始化

    vue实现打地鼠

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

    vue实现打地鼠

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

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

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

优化建议

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

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

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

相关文章

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展示…

vue实现城市

vue实现城市

Vue 实现城市选择功能 使用 Element UI 的 Cascader 组件 Element UI 提供了一个 Cascader 级联选择器组件,非常适合实现城市选择功能。需要先安装 Elemen…

vue实现滚动显示

vue实现滚动显示

Vue 实现滚动显示的几种方法 在 Vue 中实现滚动显示可以通过多种方式实现,以下是几种常见的方法: 使用 CSS 动画 通过 CSS 的 animation 属性结合 @keyframes 实现…

vue实现转盘抽奖

vue实现转盘抽奖

Vue 实现转盘抽奖 基本思路 转盘抽奖的核心是通过 CSS 动画实现旋转效果,Vue 负责控制旋转逻辑和数据绑定。关键点包括转盘扇形区域的绘制、旋转动画的触发与停止、中奖结果的判定。 实现步骤 H…