当前位置:首页 > VUE

vue实现打地鼠

2026-02-17 05:12:08VUE

vue实现打地鼠

vue实现打地鼠

实现思路

使用 Vue.js 实现打地鼠游戏需要结合数据驱动和事件处理。游戏核心逻辑包括随机生成地鼠、点击得分、计时器和难度控制。

基础代码结构

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

数据定义

<script>
export default {
  data() {
    return {
      holes: Array(9).fill({ active: false }),
      score: 0,
      timeLeft: 30,
      gameInterval: null,
      moleInterval: null,
      isPlaying: false
    }
  }
}
</script>

游戏逻辑

methods: {
  startGame() {
    this.score = 0;
    this.timeLeft = 30;
    this.isPlaying = true;

    this.gameInterval = setInterval(() => {
      this.timeLeft--;
      if (this.timeLeft <= 0) {
        this.endGame();
      }
    }, 1000);

    this.moleInterval = setInterval(() => {
      const randomIndex = Math.floor(Math.random() * 9);
      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 });
    }
  },

  endGame() {
    clearInterval(this.gameInterval);
    clearInterval(this.moleInterval);
    this.isPlaying = false;
    alert(`游戏结束! 最终得分: ${this.score}`);
  }
}

样式设计

<style>
.game-container {
  text-align: center;
  margin-top: 50px;
}

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

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

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

.mole:hover {
  cursor: pointer;
  transform: scale(1.1);
}

.score, .time {
  font-size: 20px;
  margin-bottom: 10px;
}

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

功能扩展

  1. 增加难度控制:可以添加速度滑块控制地鼠出现频率
  2. 添加音效:在地鼠出现和被击中时播放不同音效
  3. 添加动画效果:使用CSS动画让地鼠有弹出效果
  4. 保存最高分:使用localStorage保存玩家最高分记录
  5. 添加关卡系统:随着分数增加提高游戏难度

优化建议

  1. 使用Vue的transition组件实现更流畅的动画效果
  2. 考虑使用Vuex管理游戏状态,特别是多组件复杂交互时
  3. 添加响应式设计,确保在不同设备上都能良好显示
  4. 实现游戏暂停功能,允许玩家临时中断游戏

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

相关文章

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue 2…

vue 实现hover

vue 实现hover

Vue 实现 Hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 伪类 :hover 最简单的方式是直接使用 CSS 的 :hov…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API) UI组件…

vue实现单词拼写

vue实现单词拼写

Vue 实现单词拼写功能 在 Vue 中实现单词拼写功能,可以通过数据绑定、事件处理和动态样式来实现。以下是一个完整的实现方案。 数据准备 定义一个包含单词和提示信息的数组,用于拼写练习:…

vue实现理财超市

vue实现理财超市

Vue实现理财超市功能 理财超市通常包含产品展示、筛选、排序、详情页等功能,以下是基于Vue的实现方案: 技术栈建议 Vue 2/3 + Vue Router UI库:Element UI/Ant…