亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何基于Vue制作一個(gè)猜拳小游戲

 更新時(shí)間:2023年01月05日 10:12:10   作者:清風(fēng)?與我  
Vue.js作為目前最熱門(mén)最具前景的前端框架之一,其提供了一種幫助我們快速構(gòu)建并開(kāi)發(fā)前端項(xiàng)目的新的思維模式,下面這篇文章主要給大家介紹了關(guān)于如何基于Vue制作一個(gè)猜拳小游戲的相關(guān)資料,需要的朋友可以參考下

前言:

在工作學(xué)習(xí)之余玩一會(huì)游戲既能帶來(lái)快樂(lè),還能緩解生活壓力,跟隨此文一起制作一個(gè)小游戲吧。

描述:
石頭剪子布,是一種猜拳游戲。起源于中國(guó),然后傳到日本、朝鮮等地,隨著亞歐貿(mào)易的不斷發(fā)展它傳到了歐洲,到了近現(xiàn)代逐漸風(fēng)靡世界。簡(jiǎn)單明了的規(guī)則,使得石頭剪子布沒(méi)有任何規(guī)則漏洞可鉆,單次玩法比拼運(yùn)氣,多回合玩法比拼心理博弈,使得石頭剪子布這個(gè)古老的游戲同時(shí)用于“意外”與“技術(shù)”兩種特性,深受世界人民喜愛(ài)。
游戲規(guī)則:石頭打剪刀,布包石頭,剪刀剪布。
現(xiàn)在,需要你寫(xiě)一個(gè)程序來(lái)判斷石頭剪子布游戲的結(jié)果。

項(xiàng)目效果展示:

對(duì)應(yīng)素材:

代碼實(shí)現(xiàn)思路:

準(zhǔn)備對(duì)應(yīng)的素材用于界面效果展示。

在盒子中上面設(shè)置兩個(gè) img 標(biāo)簽,src 用動(dòng)態(tài)展示,左側(cè)代表玩家,右側(cè)代表電腦。

在JS中設(shè)置定時(shí)器,每隔0.1秒切換背景圖,達(dá)到一個(gè)閃爍的效果。

給代表玩家的image動(dòng)態(tài)賦值加載中的動(dòng)畫(huà),同時(shí)在頁(yè)面下方實(shí)現(xiàn)選擇的區(qū)域,讓用戶(hù)能夠點(diǎn)擊。

實(shí)現(xiàn)圖片的點(diǎn)擊事件,當(dāng)點(diǎn)擊時(shí)修改上方代表玩家圖片的src值,同時(shí)停止右側(cè)代表電腦的圖片的閃爍,并通過(guò)下標(biāo)判斷電腦的選擇。

在給玩家圖片賦值的同時(shí),停止電腦方圖片的閃爍,獲取其src,判斷哪方獲勝并在頁(yè)面進(jìn)行顯示。

在頁(yè)面底部實(shí)現(xiàn)再來(lái)一次按鈕,點(diǎn)擊時(shí)將頁(yè)面數(shù)據(jù)進(jìn)行重置。

實(shí)現(xiàn)代碼:

<template>
  <div class="box">
    <h1>石頭剪刀布</h1>
    <div class="boxli">
      <div class="top">
        <p>
          你已獲勝了<span class="id">{{ id }}</span> 次
        </p>
        <div class="liimg">
          <img src="@/assets/logo.gif" id="img" />
          <span>{{ text }}</span>
          <img :src="list[index].image" alt="" />
        </div>
      </div>
      <div class="bottom">
        <img
          v-for="item in list"
          :key="item.id"
          :src="item.image"
          @click="add(item.id)"
        />
      </div>
      <div class="btn" @click="btn">再來(lái)一局</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {
          id: 1,
          image: require("@/assets/one.png"),
        },
        {
          id: 2,
          image: require("@/assets/two.png"),
        },
        {
          id: 3,
          image: require("@/assets/three.png"),
        },
      ],
      index: 0,
      setInter: "",
      text: "",
      id: 0,
    };
  },
  mounted() {
    this.SetInter();
  },
  methods: {
    SetInter() {
      this.setInter = setInterval(() => {
        this.index++;
        if (this.index === 3) {
          this.index = 0;
        }
      }, 100);
    },
    add(id) {
      let img = document.getElementById("img");
      if (img.src === "http://localhost:8080/img/logo.b990c710.gif") {
        img.src = this.list[id - 1].image;
        clearInterval(this.setInter);
        switch (this.index) {
          case 0:
            if (id - 1 === 0) {
              this.text = "平局了";
            } else if (id - 1 === 1) {
              this.text = "獲勝了";
            } else if (id - 1 === 2) {
              this.text = "失敗了";
            }
            break;
          case 1:
            if (id - 1 === 0) {
              this.text = "失敗了";
            } else if (id - 1 === 1) {
              this.text = "平局了";
            } else if (id - 1 === 2) {
              this.text = "獲勝了";
            }
            break;
          case 2:
            if (id - 1 === 0) {
              this.text = "獲勝了";
            } else if (id - 1 === 1) {
              this.text = "失敗了";
            } else if (id - 1 === 2) {
              this.text = "平局了";
            }
            break;
        }
        if (this.text === "獲勝了") {
          this.id++;
          console.log(this.id);
        }
      }
    },
    btn() {
      let img = document.getElementById("img");
      if (img.src !== "http://localhost:8080/img/logo.b990c710.gif") {
        img.src = require("@/assets/logo.gif");
        this.SetInter();
        this.text = "";
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  text-align: center;
  h1 {
    margin: 20px 0;
  }
  .boxli {
    width: 800px;
    height: 550px;
    border: 1px solid red;
    margin: 0 auto;
    .top {
      img {
        width: 200px;
        height: 200px;
      }
      .liimg {
        display: flex;
        justify-content: center;
        align-items: center;
      }
      span {
        display: inline-block;
        width: 100px;
        color: red;
        text-align: center;
      }
      .id {
        width: 30px;
        margin-top: 20px;
      }
    }
  }
  .btn {
    width: 200px;
    height: 50px;
    background-image: linear-gradient(to right, #ff00ad, #f09876);
    margin: 0 auto;
    line-height: 50px;
    color: #fff;
    border-radius: 10px;
  }
}
</style>

總結(jié):

到此這篇關(guān)于如何基于Vue制作一個(gè)猜拳小游戲的文章就介紹到這了,更多相關(guān)Vue猜拳小游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論