当前位置:首页 > VUE

vue实现击中类游戏

2026-02-25 06:55:34VUE

Vue 实现击中类游戏

游戏核心逻辑设计

使用Vue的响应式特性管理游戏状态,包括目标位置、得分和游戏时间。数据部分可设计为:

data() {
  return {
    score: 0,
    timeLeft: 30,
    targetPosition: { x: 0, y: 0 },
    isPlaying: false
  }
}

动态目标生成

通过随机函数生成目标位置,结合Vue的样式绑定实现动态显示:

methods: {
  moveTarget() {
    this.targetPosition = {
      x: Math.random() * (window.innerWidth - 50),
      y: Math.random() * (window.innerHeight - 50)
    }
  }
}

模板部分使用动态样式:

vue实现击中类游戏

<div 
  class="target" 
  :style="{ left: targetPosition.x + 'px', top: targetPosition.y + 'px' }"
  @click="hitTarget">
</div>

点击事件处理

实现击中逻辑和分数计算:

hitTarget() {
  if (!this.isPlaying) return
  this.score++
  this.moveTarget()
  clearTimeout(this.timer)
  this.startCountdown()
}

游戏计时系统

使用setInterval实现倒计时功能:

vue实现击中类游戏

startGame() {
  this.isPlaying = true
  this.score = 0
  this.timeLeft = 30
  this.moveTarget()
  this.startCountdown()
},
startCountdown() {
  this.timer = setInterval(() => {
    if (this.timeLeft <= 0) {
      clearInterval(this.timer)
      this.isPlaying = false
    } else {
      this.timeLeft--
    }
  }, 1000)
}

动画效果增强

通过CSS过渡效果提升交互体验:

.target {
  transition: transform 0.1s;
  transform: scale(1);
}
.target:active {
  transform: scale(0.9);
}

响应式布局适配

使用vw/vh单位确保游戏在不同设备上的适应性:

.game-area {
  width: 100vw;
  height: 100vh;
}

完整组件示例

<template>
  <div class="game-container">
    <div v-if="!isPlaying">
      <button @click="startGame">Start Game</button>
    </div>
    <div v-else>
      <div class="info">Score: {{ score }} | Time: {{ timeLeft }}</div>
      <div 
        class="target" 
        :style="targetStyle"
        @click="hitTarget">
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      score: 0,
      timeLeft: 30,
      targetPosition: { x: 0, y: 0 },
      isPlaying: false,
      timer: null
    }
  },
  computed: {
    targetStyle() {
      return {
        left: `${this.targetPosition.x}px`,
        top: `${this.targetPosition.y}px`
      }
    }
  },
  methods: {
    startGame() {
      this.isPlaying = true
      this.score = 0
      this.timeLeft = 30
      this.moveTarget()
      this.startCountdown()
    },
    moveTarget() {
      this.targetPosition = {
        x: Math.random() * (window.innerWidth - 50),
        y: Math.random() * (window.innerHeight - 50)
      }
    },
    hitTarget() {
      this.score++
      this.moveTarget()
    },
    startCountdown() {
      this.timer = setInterval(() => {
        if (this.timeLeft <= 0) {
          clearInterval(this.timer)
          this.isPlaying = false
        } else {
          this.timeLeft--
        }
      }, 1000)
    }
  },
  beforeUnmount() {
    clearInterval(this.timer)
  }
}
</script>

<style>
.target {
  width: 50px;
  height: 50px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
  cursor: pointer;
  transition: transform 0.1s;
}
.target:active {
  transform: scale(0.9);
}
.game-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}
.info {
  position: fixed;
  top: 10px;
  left: 10px;
  font-size: 20px;
}
</style>

标签: 类游戏vue
分享给朋友:

相关文章

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json)…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…