当前位置:首页 > 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 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…