實驗樓項目課學習筆記-jQuery翻轉拼圖遊戲

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

jQuery翻轉拼圖遊戲主要是通過jQuery和Bootstrap 3實現,UI主要使用了Bootstrap的Modal彈窗,對其他模組涉及較少,所以不需要特別熟悉Bootstrap,遊戲邏輯通過jQuery和JavaScript實現。

項目:

game/index.html

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>            藍色拼圖        </title>        <!-- 引入Bootstrap css -->        <link rel="stylesheet" href="bootstrap/css/bootstrap.css">        <!-- 引入自訂 css -->        <link rel="stylesheet" href="css/style.css">        <!-- 引入jQuery和Bootstrap js -->        <script src="js/jquery-1.11.1.js"></script>        <script src="bootstrap/js/bootstrap.js"></script>        <!-- 引入遊戲主程式js -->        <script src="js/game.js"></script>    </head>    <body>        <div class="container">            <div class="heading">                <h1 class="title">                    jQuery翻轉拼圖遊戲                </h1>                <div class="scoresContainer">                    <!-- 現實當前遊戲層級 -->                    <div class="currLevel">                        當前層級:<b>1</b>                    </div>                </div>            </div>            <div class="aboveGame">                <!-- 遊戲按鈕 -->                <a class="reset btn btn-primary" data-toggle="modal" data-target="#restartLevel">重設層級</a>                <a class="newgame btn btn-primary" data-toggle="modal" data-target="#newGame">重新開始</a>                <a class="instruct btn btn-primary" data-toggle="modal" data-target="#instructions">玩法說明</a>            </div>            <div class="board">                <!-- 遊戲空間 -->                <div class="gamerow">                    <div class="gamesquare coord0q0"></div>                </div>            </div>        </div>        <!-- 遊戲玩法 modal -->        <div class="modal fade" id="instructions" tabindex="-1" role="dialog" aria-hidden="true">            <div class="modal-dialog">                <div class="modal-content">                    <div class="modal-header">                        <h4 class="modal-title">遊戲玩法</h4>                    </div>                    <div class="modal-body">                        <p>如何才算贏:使拼板全部變成藍色。</p>                        <p>玩法:每個方塊一面橙色,一面藍色。點擊一個方塊,這個方塊的顏色會翻轉,並且,與它鄰接的方塊的顏色也會翻轉。</p>                    </div>                    <div class="modal-footer">                        <button type="button" class="btn btn-primary" data-dismiss="modal">開始遊戲</button>                    </div>                </div>            </div>        </div>        <!-- 新遊戲 modal -->        <div class="modal fade" id="newGame" tabindex="-1" role="dialog" aria-hidden="true">            <div class="modal-dialog">                <div class="modal-content">                    <div class="modal-header">                        <h4 class="modal-title">重新開始</h4>                    </div>                    <div class="modal-body">                        <p>你真的想重新開始嗎?</p>                    </div>                    <div class="modal-footer">                        <button type="button" class="btn btn-primary" id="newGameConfirm" data-dismiss="modal">開始遊戲</button>                    </div>                </div>            </div>        </div>        <!-- 重新開始本層級遊戲 modal -->        <div class="modal fade" id="restartLevel" tabindex="-1" role="dialog" aria-hidden="true">            <div class="modal-dialog">                <div class="modal-content">                    <div class="modal-header">                        <h4 class="modal-title">重新開始本層級遊戲</h4>                    </div>                    <div class="modal-body">                        <p>你真的想重新開始嗎?</p>                    </div>                    <div class="modal-footer">                        <button type="button" class="btn btn-primary" id="resetLevelConfirm" data-dismiss="modal">重設</button>                    </div>                </div>            </div>        </div>    </body></html>

  

game/css/style.css:

.container {    width: 600px;    margin: 0 auto;}/* 遊戲層級 */.scoresContainer {    float: right;    text-align: right;    font-size: 18px;}/* 遊戲按鈕 */.aboveGame:after {    display: block;    margin: 20px 0;    content: "";    clear: both;}/* 遊戲空間 */.board {    position: absolute;    background-color: #5f5f5f;    border-radius: 4px;}.gamesquare {    float: left;    margin-right: 15px;    border-radius: 3px;}

game/js/game.js

function StyleHelper() {    //    // 控制遊戲空間中的塊大小    //    this.setGridSize = function(level) {        var margin = this.getMargin(level);        var res = ($(‘.container‘).width() - margin * level) / (level);        // 設定塊的大小和間距        $(‘.gamesquare‘).css(‘margin-right‘, margin);        $(‘.gamesquare‘).css(‘width‘, res);        $(‘.gamesquare‘).css(‘height‘, res);        // 設定每行的高度、右邊距和下邊距        $(‘.gamerow‘).css(‘height‘, res);        $(‘.gamerow‘).css(‘margin-right‘, margin * (-1));        $(‘.gamerow‘).css(‘margin-bottom‘, margin);        // 設定遊戲空間的內邊距        $(‘.board‘).css(‘padding‘, margin);        $(‘.board‘).css(‘padding-bottom‘, 0);    };    // 擷取邊距    this.getMargin = function(level) {        if (level <= 6) return 15;        if (level > 15) return 5;        return 20 - level;    };}function Game() {    //    // 控制遊戲    //    var self = this;    // 遊戲層級    this.level = 1;    // 建立用於控制遊戲的對象    this.gb;    this.sh = new StyleHelper();    this.processClick = function(w, h) {        this.gb.processClick(w, h);        this.updateCounts();        if (this.gb.isGameWin()) {            this.gameEnd();        }    }    // 開始遊戲    this.beginGame = function() {        self.setupLevel();    }    // 遊戲結束    this.gameEnd = function() {        this.level++;        this.resetGame();    }    // 遊戲過關    this.resetGame = function() {        $(‘#levelDescriptor‘).html("進入層級 " + this.level);        setTimeout(function() {            self.setupLevel();        }, 500);    }    // 初始化遊戲層級    this.setupLevel = function() {        this.gb = new GameBoard(this.level, this.level);        $(‘.board‘).html("");            // 清空遊戲面板        this.gb.populate();              // 重設所有圖塊為橙色        self.gb.renderBoard();           // 渲染遊戲面板並建立圖塊        self.sh.setGridSize(this.level); // 控制遊戲空間中的塊大小        self.updateCounts();             // 更新顯示當前層級        self.applyBindings();            // 翻轉所點圖塊周圍圖塊的顏色    }    // 顯示當前層級    this.updateCounts = function() {        $(".currLevel").html("當前層級: <b>" + this.level + "</b>");    }    this.applyBindings = function() {        $(‘.gamesquare‘).click(function(){            // 擷取所點擊的圖塊的位置            var cname = $(this).context.className.split(" ")[1];            var coord = cname.substring(5).split("q");            var height = parseInt(coord[1]);            var width = parseInt(coord[0]);            self.processClick(width, height); // 翻轉所點圖塊周圍圖塊的顏色        });    }    // 開始新遊戲    this.onNewGameClick = function() {        this.level = 1;        this.setupLevel();    }}function GameBoard (wd, hi) {    //    // 遊戲面板    //    // 圖塊座標    this.high = hi - 1;    this.wide = wd - 1;    this.count = 0;    // 橫向座標為 wide,縱向座標為 high    //    0 | 1 | 2 | 3 | ....    //  - - - - - - - - - - - -       //  0   |   |   |   |     //  - - - - - - - - - - - -    //  1   |   |[2][1]    //  -    //  2    //  :    //  :    //    // 建立圖塊二維數組    this.board = new Array(wd);    for (var i = 0; i <= this.wide; i++) {        this.board[i] = new Array(hi);    }    // 渲染遊戲面板並建立圖塊    this.renderBoard = function() {        var s = "";        for (var j = 0; j <= this.high; j++) {            s += "<div class=‘gamerow‘>";            for (var i = 0; i <= this.wide; i++) {                s += "<div class=‘gamesquare coord" + i + "q" + j + "‘></div>";            }            s += "</div>";        }        $(‘.board‘).html(s);        for (var i = 0; i <= this.wide; i++) {            for (var j = 0; j <= this.high; j++) {                this.processCLickView(i, j);            }        }    }    this.processClick = function(w, h) {        //        // 翻轉所點圖塊周圍圖塊的顏色        //        // 找到所點圖塊周圍需要翻轉顏色的圖塊        var lowx = w - 1;        var highx = w + 1;        var lowy = h - 1;        var highy = h + 1;        // 檢查被點擊的圖塊是否是邊緣圖塊        if (w == 0) lowx = 0;        if (w == this.wide) highx = this.wide;        if (h == 0) lowy = 0;        if (h == this.high) highy = this.high;        // 翻轉所點圖塊垂直方向圖塊        for (var i = lowy; i <= highy; i++) {            if (this.board[w][i] == 0) {                this.board[w][i] = 1;                this.count++;            } else {                this.board[w][i] = 0;                this.count--;            }            this.processCLickView(w, i);        }        // 翻轉所點圖塊水平方向的圖塊        for (var i = lowx; i <= highx; i++) {            if (i == w) continue;            if (this.board[i][h] == 0) {                this.board[i][h] = 1;                this.count++;            } else {                this.board[i][h] = 0;                this.count--;            }            this.processCLickView(i, h);        }    }    // 翻轉圖塊顏色    this.processCLickView = function(w, h) {        var coord = ".coord" + w + "q" + h;        if (this.board[w][h] == 0) {            $(coord).css("background-color", "#e8BB39");        } else {            $(coord).css("background-color", "#6060e0");        }    }    // 重設所有圖塊為橙色    this.populate = function() {        for (var i = 0; i <= this.wide; i++) {            for (var j = 0; j <= this.high; j++) {                this.board[i][j] = 0;            }        }    }    // 遊戲勝利    this.isGameWin = function() {        return this.count == (this.wide + 1) * (this.high + 1);    }}// 初始化遊戲$(document).ready(function() {    // 建立遊戲    var game = new Game();    // 開始遊戲    game.beginGame();    // 重設遊戲空間圖塊    $(‘#resetLevelConfirm‘).click(function() {        game.setupLevel();    });    // 開始新遊戲    $(‘#newGameConfirm‘).click(function() {        game.onNewGameClick();    });});

詳細講解:http://www.shiyanlou.com/ 內查看jQuery翻轉拼圖遊戲

 

實驗樓項目課學習筆記-jQuery翻轉拼圖遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.