当前位置:首页 > 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)
    }
  }
}

模板部分使用动态样式:

<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实现倒计时功能:

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单位确保游戏在不同设备上的适应性:

vue实现击中类游戏

.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中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app"…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue日历实现

vue日历实现

实现基础日历布局 使用Vue的模板语法构建日历的基本HTML结构。一个典型的日历包含星期标题和日期格子,可以通过v-for循环生成。月份切换功能通过计算属性动态更新日期数组。 <templat…

vue实现tooltips

vue实现tooltips

Vue 实现 Tooltips 的方法 使用原生 HTML title 属性 在 Vue 模板中直接使用 HTML 原生 title 属性实现简单的提示效果。这种方式无需额外依赖,但功能较为基础,无法…