当前位置:首页 > VUE

vue实现猜拳

2026-01-12 09:06:13VUE

实现猜拳游戏的Vue组件

创建一个Vue猜拳游戏需要处理游戏逻辑、用户交互和状态管理。以下是实现方案:

vue实现猜拳

游戏组件结构

<template>
  <div class="game-container">
    <h2>猜拳游戏</h2>
    <div class="choices">
      <button @click="play('rock')">✊</button>
      <button @click="play('paper')">✋</button>
      <button @click="play('scissors')">✌️</button>
    </div>
    <div class="result" v-if="result">
      <p>你的选择: {{ userChoice }}</p>
      <p>电脑选择: {{ computerChoice }}</p>
      <p>结果: {{ result }}</p>
    </div>
  </div>
</template>

游戏逻辑实现

<script>
export default {
  data() {
    return {
      userChoice: null,
      computerChoice: null,
      result: null
    }
  },
  methods: {
    play(choice) {
      this.userChoice = choice
      this.computerChoice = this.getComputerChoice()
      this.result = this.getResult()
    },
    getComputerChoice() {
      const choices = ['rock', 'paper', 'scissors']
      const randomIndex = Math.floor(Math.random() * 3)
      return choices[randomIndex]
    },
    getResult() {
      if (this.userChoice === this.computerChoice) {
        return '平局'
      }

      const winConditions = {
        rock: 'scissors',
        paper: 'rock',
        scissors: 'paper'
      }

      return winConditions[this.userChoice] === this.computerChoice 
        ? '你赢了!' 
        : '你输了!'
    }
  }
}
</script>

样式设计

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

.choices button {
  font-size: 2rem;
  margin: 0 10px;
  padding: 10px 20px;
  cursor: pointer;
  background: none;
  border: 2px solid #42b983;
  border-radius: 5px;
  transition: all 0.3s;
}

.choices button:hover {
  background-color: #42b983;
  color: white;
}

.result {
  margin-top: 20px;
  font-size: 1.2rem;
}
</style>

功能扩展建议

添加游戏统计功能可以增强用户体验:

vue实现猜拳

data() {
  return {
    // 原有数据...
    stats: {
      wins: 0,
      losses: 0,
      ties: 0
    }
  }
},
methods: {
  play(choice) {
    this.userChoice = choice
    this.computerChoice = this.getComputerChoice()
    this.result = this.getResult()
    this.updateStats()
  },
  updateStats() {
    if (this.result === '你赢了!') this.stats.wins++
    else if (this.result === '你输了!') this.stats.losses++
    else this.stats.ties++
  }
}

在模板中添加统计显示:

<div class="stats">
  <p>胜: {{ stats.wins }} | 负: {{ stats.losses }} | 平: {{ stats.ties }}</p>
</div>

标签: 猜拳vue
分享给朋友:

相关文章

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的position…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…