欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用jquery實現(xiàn)別踩白塊小游戲的方法實例

 更新時間:2022年04月11日 16:46:17   作者:白日夢企鵝  
別踩白塊是一款簡單易上手的數(shù)字小游戲,下面這篇文章主要給大家介紹了關(guān)于使用jquery實現(xiàn)別踩白塊小游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

掘金真的是太懂了,寫游戲的活動,想不參加都難!第一個,別踩白塊!

先來看效果~

如上圖所示,jkl三個鍵對應(yīng)三列,左上是得分,得分右邊是時間(沒做倒計時。。。)敲擊對應(yīng)的按鍵來進(jìn)行游戲,如果敲錯了就會彈出得分與所用時間

接下來就開始肢解這個小游戲,來一步一步的分析究竟是怎么實現(xiàn)的,let's go~

html

<body>
    <div class="main">
        <div id="score">0</div>
        <div id="time">00:00:00</div>
    </div>
    <div class="caption">別踩白塊!</div>
    <div class="container">
        <table id="tab">
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
        <span class="font one">J</span>
        <span class="font two">K</span>
        <span class="font three">L</span>
    </div>
</body>

其他地方就是普通的div,中間的主體部分是用表格實現(xiàn)的,jkl置于表格的上方顯示

首頁字體

width: 300px;
font-size: 60px;
font-weight: bold;
text-align: center;
position: relative;
margin: 0 auto;
-webkit-text-fill-color: transparent;
/*文字的填充色*/
-webkit-text-stroke: 1.2px white;

重點(diǎn)的兩個屬性:最后兩個屬性-webkit-text-fill-color-webkit-text-stroke,第一個是文字的填充顏色,第二個是文字的邊框顏色,最終形成這種效果

中間的表格

主體用的是table做的,四行三列,每一行都有一個黑塊在隨機(jī)的一列生成

Math.ceil(Math.random() * 3) - 1

Math.random()會取0-1的隨機(jī)小數(shù),乘3是為了獲取1-3內(nèi)的隨機(jī)小數(shù),Math.ceil()向上取整,在減1,最后可以拿到三個可能的結(jié)果:0 1 2

實現(xiàn)每一行的黑塊隨機(jī)顯示

新建dom元素hang

var Game={
    ...
    hang: document.getElementsByTagName('tr');
    ...
}

為每一行的隨機(jī)列添加黑塊

//獲取列---------------------------------------------
Game.hang[0].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
Game.hang[1].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
Game.hang[2].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
Game.hang[3].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';

按鍵事件

鍵盤按下事件調(diào)用了兩個方法,一個是主方法key事件,另外一個是負(fù)責(zé)顯示jkl的color事件

key事件

key: function () {
            onkeydown = function (event) {
                if (Game.bool == true && (event.key == 'j' || event.key == 'k' || event.key == 'l')) {
                    Game.int = setInterval(Game.times, 10);
                    Game.bool = false;
                }
                switch (event.key) {
                    case 'j':
                        if (Game.hang[3].children[0].style.background == 'black') {
                            Game.speed();
                            Game.scores();
                        } else {
                            Game.over();
                            Game.begin()
                        }
                        break;
                    case 'k':
                        if (Game.hang[3].children[1].style.background == 'black') {
                            Game.speed();
                            Game.scores();
                        } else {
                            Game.over();
                            Game.begin()
                        }
                        break;
                    case 'l':
                        if (Game.hang[3].children[2].style.background == 'black') {
                            Game.speed();
                            Game.scores();
                        } else {
                            Game.over();
                            begin()
                        }
                        break;
                }
                Game.color();
            }
        }

第一塊if語句負(fù)責(zé)控制游戲的開始和結(jié)束,當(dāng)按下的按鍵是jkl的時候游戲開始,Game.times是一個計時器的方法,每一秒去執(zhí)行一下

times

times: function () {
           Game.mis += 1;
           if (Game.mis > 99) {
               Game.mis = 0;
               Game.sec += 1;
           }
           if (Game.sec > 59) {
               Game.sec = 0;
               Game.min += 1;
           }
           if (Game.min > 23) {
               Game.min = 0;
           }
           Game.begin();
       },

switch語句里為主要邏輯,每個按鍵分為成功和失敗,如果當(dāng)前按下的按鍵為黑色的話,執(zhí)行speed方法和score方法

speed方法

speed: function () {
    Game.adds();
    Game.speedup();
    // 下面有介紹
},

score方法

scores: function () {
    Game.score += 1;
    Game.sco.innerHTML = Game.score;
},

將已經(jīng)定好的score +1,返回給頁面

如果判斷按下的按鈕不是黑塊的話,執(zhí)行結(jié)束的操作,即調(diào)用over()和begin()

over方法

over: function () {
    alert('游戲結(jié)束,得分:' + Game.score + ';用時' + Game.time.innerHTML);
    clearInterval(Game.int);
    Game.mis = 0;
    Game.sec = 0;
    Game.min = 0;
    Game.score = 0;
    Game.sco.innerHTML = Game.score;
    Game.time.innerHTML = (Game.min < 10 ? "0" + Game.min : Game.min) + ":" + (Game.sec < 10 ? "0" + Game.sec : Game.sec) + ":" + (Game.mis < 10 ? "0" + Game.mis : Game.mis);
    Game.bool = true;
},

顯示游戲結(jié)束的提示,將計時器,分?jǐn)?shù),游戲狀態(tài)置為初始化,以便下次的重新開始

adds

adds: function () {
    Game.tab.insertRow(0);
    for (var i = 0; i < 3; i++) {
        Game.hang[0].insertCell();
    }
    Game.hang[0].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
},

此方法與下面的方法就是表格往下移動的主要函數(shù)

  • 在最上面新增一行tr
  • 給tr循環(huán)插入三個td
  • 給三個td的隨機(jī)一個設(shè)置為黑色

speedup

speedup: function () {
    if (Game.btn == 0 || Game.btn % 150 != 0) {
        Game.tab.style.bottom = -Game.btn - 5 + 'px';
        Game.btn += 5;
        setTimeout(Game.speedup, 1);
    } else {
        clearTimeout(Game.speedup);
        Game.btn += 5;
    }
},

speedup方法給予了向下移動時的動畫效果

源碼

直接扔進(jìn)一個html里就能玩

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>實驗</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            width: 100%;
            margin: 0 auto;
            background: linear-gradient(-45deg, purple, blue);
        }

        .container {
            margin: 0 auto;
            width: 470px;
            height: 630px;
            overflow: hidden;
            position: relative;
        }

        table {
            width: 464px;
            height: 612px;
            position: absolute;
            right: 0;
            bottom: 0;
            border-collapse: collapse;
        }

        td {
            width: 150px;
            height: 150px;
            border: 6px solid pink;
        }

        .main {
            color: white;
            text-align: center;
            /* vertical-align: middle; */
            font-size: 50px;
            position: absolute;
            top: 0;
            left: 0;
        }

        #score {
            display: inline-block;
            padding-right: 200px;
        }

        #time {
            display: inline-block;
        }

        .caption {
            width: 300px;
            font-size: 60px;
            font-weight: bold;
            text-align: center;
            position: relative;
            margin: 0 auto;
            -webkit-text-fill-color: transparent;
            /*文字的填充色*/
            -webkit-text-stroke: 1.2px white;
        }

        .font {
            color: transparent;
            display: inline-block;
            font-size: 60px;
            font-weight: bold;
            position: absolute;
        }

        .one {
            left: 14.5%;
            top: 81%;
        }

        .two {
            left: 45%;
            top: 81%;
        }

        .three {
            left: 79%;
            top: 81%;
        }
    </style>
</head>

<body>
    <div class="main">
        <div id="score">0</div>
        <div id="time">00:00:00</div>
    </div>
    <div class="caption">別踩白塊!</div>
    <div class="container">
        <table id="tab">
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
        <span class="font one">J</span>
        <span class="font two">K</span>
        <span class="font three">L</span>
    </div>
</body>
<script>
    var Game = {
        // 屬性
        int: null,
        mis: 0,
        sec: 0,
        min: 0,
        score: 0,
        bool: true,
        btn: 0,
        tab: document.getElementsByTagName('table')[0],
        font: document.getElementsByTagName('span'),
        sco: document.getElementById('score'),
        time: document.getElementById('time'),
        hang: document.getElementsByTagName('tr'),
        // 方法
        times: function () {
            Game.mis += 1;
            if (Game.mis > 99) {
                Game.mis = 0;
                Game.sec += 1;
            }
            if (Game.sec > 59) {
                Game.sec = 0;
                Game.min += 1;
            }
            if (Game.min > 23) {
                Game.min = 0;
            }
            Game.begin();
        },
        color: function () {
            if (Game.hang[3].children[0].style.background == 'black') {
                Game.font[0].style.color = 'white';
            } else {
                Game.font[0].style.color = 'transparent';
            }
            if (Game.hang[3].children[1].style.background == 'black') {
                Game.font[1].style.color = 'white';
            } else {
                Game.font[1].style.color = 'transparent';
            }
            if (Game.hang[3].children[2].style.background == 'black') {
                Game.font[2].style.color = 'white';
            } else {
                Game.font[2].style.color = 'transparent';
            }
        },
        adds: function () {
            Game.tab.insertRow(0);
            for (var i = 0; i < 3; i++) {
                Game.hang[0].insertCell();
            }
            Game.hang[0].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
        },
        speedup: function () {
            if (Game.btn == 0 || Game.btn % 150 != 0) {
                Game.tab.style.bottom = -Game.btn - 5 + 'px';
                Game.btn += 5;
                setTimeout(Game.speedup, 1);
            } else {
                clearTimeout(Game.speedup);
                Game.btn += 5;
            }
        },
        speed: function () {
            Game.adds();
            Game.speedup();
        },
        scores: function () {
            Game.score += 1;
            Game.sco.innerHTML = Game.score;
        },
        over: function () {
            alert('游戲結(jié)束,得分:' + Game.score + ';用時' + Game.time.innerHTML);
            clearInterval(Game.int);
            Game.mis = 0;
            Game.sec = 0;
            Game.min = 0;
            Game.score = 0;
            Game.sco.innerHTML = Game.score;
            Game.time.innerHTML = (Game.min < 10 ? "0" + Game.min : Game.min) + ":" + (Game.sec < 10 ? "0" + Game.sec : Game.sec) + ":" + (Game.mis < 10 ? "0" + Game.mis : Game.mis);
            Game.bool = true;
        },
        key: function () {
            onkeydown = function (event) {
                if (Game.bool == true && (event.key == 'j' || event.key == 'k' || event.key == 'l')) {
                    Game.int = setInterval(Game.times, 10);
                    Game.bool = false;
                }
                switch (event.key) {
                    case 'j':
                        if (Game.hang[3].children[0].style.background == 'black') {
                            Game.speed();
                            Game.scores();
                        } else {
                            Game.over();
                        }
                        break;
                    case 'k':
                        if (Game.hang[3].children[1].style.background == 'black') {
                            Game.speed();
                            Game.scores();
                        } else {
                            Game.over();
                        }
                        break;
                    case 'l':
                        if (Game.hang[3].children[2].style.background == 'black') {
                            Game.speed();
                            Game.scores();
                        } else {
                            Game.over();
                        }
                        break;
                }
                Game.color();
            }
        }
    }
    //獲取列---------------------------------------------
    Game.hang[0].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
    Game.hang[1].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
    Game.hang[2].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';
    Game.hang[3].children[Math.ceil(Math.random() * 3) - 1].style.background = 'black';

    //--------------------------------------------------
    //鍵盤按下事件----------------------------------------
    Game.color();
    Game.key();
    //---------------------------------------------------
</script>
</html>

至此這個小游戲也就完成了啦,希望對你有所幫助,再見~

總結(jié)

到此這篇關(guān)于使用jquery實現(xiàn)別踩白塊小游戲的文章就介紹到這了,更多相關(guān)jquery別踩白塊小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論