最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript編寫猜拳游戲

 更新時間:2021年11月01日 17:16:31   作者:帝國吾愛  
這篇文章主要為大家詳細(xì)介紹了JavaScript編寫猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaScript編寫猜拳游戲的具體代碼,供大家參考,具體內(nèi)容如下

HTML代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS</title>
 
    <script rel="script" src="js1.js"></script>
 
    <style>
        #Div {
            width: 1000px;
            height: 700px;
            position: relative;
            border-style: groove;
            border-width: 2px;
        }
        /*猜拳區(qū)*/
        #area {
            width: 300px;
            height: 200px;
            background-color: #011bfd;
            position: absolute;
            top: 20%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /*顯示區(qū)*/
        #results {
            width: 400px;
            height: 50px;
            background-color: #f7f8fd;
            text-align:center;
            font-size:30px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /*卡牌石頭*/
        #stone {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 30%;
            transform: translate(-50%, -50%);
        }
        /*卡牌剪刀*/
        #scissors {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /*卡牌布*/
        #cloth {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 70%;
            transform: translate(-50%, -50%);
        }
    </style>
 
</head>
<body>
 
<div id="Div">
    <div id="area"></div>
 
    <div id="results"></div>
 
    <div id="stone" draggable="true"></div>
    <div id="scissors" draggable="true"></div>
    <div id="cloth" draggable="true"></div>
 
</div>
 
<script rel="script">
    show();
</script>
 
</body>
</html>

JavaScript 代碼:

/***
 area 區(qū)域
 stone 石頭    石頭 = 石頭  石頭 > 剪刀  石頭 < 布
 scissors 剪刀 剪刀 < 石頭  剪刀 = 剪刀  剪刀 > 布
 cloth 布      布 > 石頭  布 < 剪刀  布 = 布
 ***/
 
/***
 查看數(shù)據(jù)類型:Object.prototype.toString.call(變量)
 刷新局部:window.location.reload('#area');
 ***/
 
 
function Init () {
    //獲取并且綁定HTML的ID,返回HTML格式(HTMLDivElement)
    const area = document.querySelector("#area");
    const results = document.querySelector("#results");
    const stone = document.querySelector("#stone");
    const scissors = document.querySelector("#scissors");
    const cloth = document.querySelector("#cloth");
 
    //定義拖拽的卡牌
    let ondragstart_ID = null
    //猜拳類型編寫成數(shù)組
    const random_Action = ['stone', 'scissors', 'cloth'];
    //隨機(jī)獲取數(shù)組中的一個數(shù)組的鍵
    const random_Digital = Math.round(Math.random() * (random_Action.length - 1) + 1);
    //獲取數(shù)組中的鍵值,如數(shù)組random_Action中的'stone'(random_Action[0])
    const random_Value = random_Action[random_Digital-1];
 
    //編寫猜拳類型的方法
    function attribute (parameter) {
        //鼠標(biāo)移入時(猜拳卡片變大)
        parameter.onmouseover = function () {
            this.style.height = '200px';
            this.style.width = '150px';
        }
        //鼠標(biāo)移出時(猜拳卡片恢復(fù)初始狀態(tài))
        parameter.onmouseleave = function () {
            this.style.height = '150px';
            this.style.width = '100px';
        }
        //元素開始拖動時(猜拳卡片變透明)
        parameter.ondragstart = function () {
            this.style.opacity = '0.3';
            ondragstart_ID = parameter.id
        }
    }
    //創(chuàng)建猜拳類型的對象,給猜拳對象的屬性賦值
    this.show_attribute = function () {
        attribute(stone)
        attribute(scissors)
        attribute(cloth)
    }
    //編寫卡牌拖動事件
    this.overout = function () {
        //當(dāng)卡牌拖拽到area(猜拳區(qū)域)之上
        area.ondragenter = function () {
            //判斷隨機(jī)數(shù)random_Digital,不能等于null
           if (random_Digital !== null) {
               //判斷拖拽的卡牌
               if (ondragstart_ID === 'stone') {
                   //判斷隨機(jī)數(shù)對等于哪一個
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'stone = stone,平局!';
                           break;
                       case scissors.id:
                           results.innerHTML = 'stone > scissors,你贏了!';
                           break;
                       case cloth.id:
                           results.innerHTML = 'stone < cloth,你輸了!';
                           break;
                       default:
                           //刷新
                           window.location.reload();
                   }
                   //元素拖動結(jié)束(猜拳卡片恢復(fù)初始狀態(tài))
                   stone.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   //延遲1秒后刷新
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
                   //判斷拖拽的卡牌
               }else if (ondragstart_ID === 'scissors') {
                   //判斷隨機(jī)數(shù)對等于哪一個
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'scissors < stone,你輸了!';
                           break;
                       case scissors.id:
                           results.innerHTML = 'scissors = scissors,平局!';
                           break;
                       case cloth.id:
                           results.innerHTML = 'scissors > cloth,你贏了!';
                           break;
                       default:
                           //刷新
                           window.location.reload();
                   }
                   //元素拖動結(jié)束(猜拳卡片恢復(fù)初始狀態(tài))
                   scissors.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   //延遲1秒后刷新
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
                   //判斷拖拽的卡牌
               }else if (ondragstart_ID === 'cloth') {
                   //判斷隨機(jī)數(shù)對等于哪一個
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'cloth > stone,你贏了!';
                           break;
                       case scissors.id:
                           results.innerHTML = 'cloth < scissors,你輸了!';
                           break;
                       case cloth.id:
                           results.innerHTML = 'cloth = cloth,平局!';
                           break;
                       default:
                           //刷新
                           window.location.reload();
                   }
                   //元素拖動結(jié)束(猜拳卡片恢復(fù)初始狀態(tài))
                   cloth.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   //延遲1秒后刷新
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
               }
           }
        }
    }
}
 
//調(diào)用函數(shù)
function show() {
    const show_html = new Init();
    show_html.show_attribute()
    show_html.overout()
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

天峻县| 嘉黎县| 福建省| 吉木萨尔县| 邻水| 祁连县| 巴彦淖尔市| 青冈县| 西充县| 金沙县| 玉门市| 会理县| 廉江市| 娄底市| 湘潭市| 武宁县| 安国市| 湘潭县| 大港区| 沙田区| 郎溪县| 大新县| 盘锦市| 阳西县| 德庆县| 呼玛县| 金沙县| 高州市| 米林县| 客服| 土默特右旗| 包头市| 水城县| 乌兰县| 高雄县| 松滋市| 辽阳市| 迁西县| 枣强县| 临西县| 饶平县|