vue写一个猜拳游戏

<script setup lang="js">
import { ref, onMounted } from 'vue'
const img1 = ref(null)
const img2 = ref(null)
const img3 = ref(null)
const tips = ref(0)
const refImgArray = [
  img1, img2, img3
]
const imgArray = [
  '/src/assets/cqImg/1.png',
  '/src/assets/cqImg/2.png',
  '/src/assets/cqImg/3.png',
]
const computerImg = ref(null)
const timer = ref(null)

const schedule = ref([])
const againFlag = ref(false)
const integral = ref(0)


const pollImg = () => {
  timer.value = setInterval(() => {
    const random = Math.floor(Math.random() * 3)
    computerImg.value = imgArray[random]
  }, 50)
}

const againClick = () => {
  againFlag.value = false
  tips.value = 0
  refImgArray.forEach(item => {
    item.value.style.opacity = '1'
  })
  pollImg()
}

const handlerClick = (e, img) => {
  refImgArray.forEach(item => {
    if (item.value != img) {
      item.value.style.opacity = '.01'
    }
  })
  clearInterval(timer.value)
  const random = Math.floor(Math.random() * 3)
  computerImg.value = imgArray[random]
  if (random == 0 && e == 0) {
    tips.value = 3
    schedule.value.push(3)
  } else if (random == 0 && e == 1) {
    tips.value = 2
    schedule.value.push(2)
    integral.value--
  } else if (random == 0 && e == 2) {
    tips.value = 1
    integral.value++
    schedule.value.push(1)
  } else if (random == 1 && e == 0) {
    tips.value = 1
    schedule.value.push(1)
    integral.value++
  } else if (random == 1 && e == 1) {
    tips.value = 3
    schedule.value.push(3)
  } else if (random == 1 && e == 2) {
    tips.value = 2
    schedule.value.push(2)
    integral.value--
  } else if (random == 2 && e == 0) {
    tips.value = 2
    schedule.value.push(2)
    integral.value--
  } else if (random == 2 && e == 1) {
    tips.value = 1
    schedule.value.push(1)
    integral.value++
  } else if (random == 2 && e == 2) {
    tips.value = 3
    schedule.value.push(3)
  }

  againFlag.value = true

}

onMounted(() => {
  pollImg()
})
</script>

<template>
  <div style="width: 700px;">
    <div class="content">
      <div class="top">
        <p>电脑</p>
        <img :src="computerImg" alt="" style="width: 200px; height: 200px" />
      </div>
      <div style="
          font-size: 30px;
          font-weight: bold;
          height: 100px;
          line-height: 100px;
          color: yellow;
        " v-show="tips == 0">
        对决中. . . . .
      </div>

      <div style="
          font-size: 30px;
          font-weight: bold;
          height: 100px;
          line-height: 100px;
          color: green;
        " v-show="tips == 1">
        你赢了
      </div>

      <div style="
          font-size: 30px;
          font-weight: bold;
          height: 100px;
          line-height: 100px;
          color: red;
        " v-show="tips == 2">
        你输了
      </div>

      <div style="
          font-size: 30px;
          font-weight: bold;
          height: 100px;
          line-height: 100px;
          color: white;
        " v-show="tips == 3">
        平局
      </div>

      <div class="bottom">
        <div class="mark" v-show="againFlag">
          <button @click="againClick">再来一次</button>
        </div>
        <div class="menu">
          <img src="../assets/cqImg/1.png" alt="" @click="handlerClick(0, img1)" ref="img1" />
          <img src="../assets/cqImg/2.png" alt="" style="margin: 0 50px" @click="handlerClick(1, img2)" ref="img2" />
          <img src="../assets/cqImg/3.png" alt="" @click="handlerClick(2, img3)" ref="img3" />
        </div>
        <p>你</p>
      </div>

      <el-card style="height: 300px;width: 90%;margin-bottom: 20px;font-weight: bold;font-size: 15px;overflow: auto;">
        <h2 style="display: flex;justify-content: space-between;">
          <span>计分板</span>
          <span>积分:{{ integral }}</span>
        </h2>
        <div v-for="(o, i) in schedule" :key="o" class="text item">
          第{{ i + 1 }}把:
          <span v-if="o == 1" style="color: green;">你赢了</span>
          <span v-if="o == 2" style="color: red;">你输了</span>
          <span v-if="o == 3">平局</span>
        </div>
      </el-card>
    </div>
  </div>
</template>

<style scoped lang="scss">
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #ccc;

  .top {
    p {
      text-align: center;
      font-size: 25px;
      font-weight: bold;
      color: red;
    }
  }

  .bottom {
    position: relative;

    .mark {
      position: absolute;
      width: 100%;
      height: 100%;
      background-color: black;
      opacity: .7;
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 10;

      button {
        width: 200px;
        height: 50px;
        background-color: #bfa;
        color: black;
        opacity: 1;
        cursor: pointer;
      }
    }

    .menu {
      z-index: -10;
    }

    p {
      text-align: center;
      font-size: 25px;
      font-weight: bold;
      color: blue;

    }

    img {
      width: 200px;
      height: 200px;
    }
  }
}
</style>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值