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

功能扩展建议

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

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实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容:…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…