欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

重新编写标题:游戏化的2048网页版挑战:前端开发在Web环境中的威力

最编程 2023-12-31 10:40:57
...

2048游戏

最近突发奇想想写个网页版游戏,然后还没写完,调bug时就看见个比较漂亮的2048代码,忍不住想分享给大家。

在他基础上,我稍微修改了下样式和一些小操作

效果图片

在这里插入图片描述在这里插入图片描述

效果

刷新
正式玩
再玩一次
关闭

代码

index.html

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>2048小游戏</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/game.js"></script>
</head>
<body>

<div class="container">
    <div class="main">
        <div class="gameName">2048小游戏</div>
        <div class="maxScore">最高分:
            <span id="maxScore">1345612</span>
        </div>
        <div class="gameBody">
            <div class="row">
                <div class="item emptyItem x0y0" x="0" y="0"></div>
                <div class="item emptyItem x0y1" x="0" y="1"></div>
                <div class="item emptyItem x0y2" x="0" y="2"></div>
                <div class="item emptyItem x0y3" x="0" y="3"></div>
            </div>
            <div class="row">
                <div class="item emptyItem x1y0" x="1" y="0"></div>
                <div class="item emptyItem x1y1" x="1" y="1"></div>
                <div class="item emptyItem x1y2" x="1" y="2"></div>
                <div class="item emptyItem x1y3" x="1" y="3"></div>
            </div>
            <div class="row">
                <div class="item emptyItem x2y0" x="2" y="0"></div>
                <div class="item emptyItem x2y1" x="2" y="1"></div>
                <div class="item emptyItem x2y2" x="2" y="2"></div>
                <div class="item emptyItem x2y3" x="2" y="3"></div>
            </div>
            <div class="row">
                <div class="item emptyItem x3y0" x="3" y="0"></div>
                <div class="item emptyItem x3y1" x="3" y="1"></div>
                <div class="item emptyItem x3y2" x="3" y="2"></div>
                <div class="item emptyItem x3y3" x="3" y="3"></div>
            </div>
        </div>
        <div class="gameRule">请按上、下、左、右键进行操作</div>
        <div class="scoreAndRefresh">
            <div class="gameScore">得分:<span id="gameScore">0</span></div>
            <button type="button" class="btn btn-danger refreshBtn">
                <span class="glyphicon glyphicon-repeat"><img src="image/refresh.jpg" alt=""></span>
            </button>
        </div>
<!------------------------gameover----------------------------->
        <div class="modal fade" id="gameOverModal" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-tittle" id="myModalLabel">2048小游戏</h4>
                    </div>
                    <div class="modal-body">
                        Game Over!
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-info" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn  btn-danger refreshBtn">再玩一次</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
<script>
    $('.btn-info').click(function(){
        $('.modal-dialog').css({'display':'none'});
    });
    $('.refreshBtn').click(function(){
        $('.modal-dialog').css({'display':'none'});
    });
</script>
</html>

bootstrap.css


        * {
            margin: 0;
            padding: 0;
            font-family: "微软雅黑";
        }
        body {
            background: lavenderblush;
        }
        .container {
            margin-top: 30px;
        }
        .main {
            width: 1000px;
            height: 100%;
            margin: 0 auto;
            overflow: hidden;
            text-align: center;
        }
        .main .gameName {
            font-size: 35px;
            font-weight: bold;
        }
        .main .maxScore {
            font-size: 20px;
        }
        .main .maxScore span {
            color: red;
            font-weight: bold;
        }
        .main .gameBody {
            width: 400px;
            height: 400px;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding: 15px;
            background: #bbada0;
            border-radius: 8px;
        }
        .main .gameBody .row {
            display: flex;
            justify-content: space-between;
        }
        .main .gameBody .row .item {
            width: 80px;
            height: 80px;
            border-radius: 10px;
            background: #ccc0b3;
            text-align: center;
            line-height: 80px;
            font-size: 30px;
            font-weight: bold;
            color: #666;
            font-family: "microsoft yahei";
        }
        .main .gameRule {
            font-size: 20px;
            font-weight: bold;
            margin-top: 15px;
        }
        .main .gameScore {
            font-size: 20px;
            font-weight: bold;
            margin-top: 15px;
        }
        .main .gameScore span {
            color: red;
            font-size: 30px;
        }
        .main .scoreAndRefresh {
            display: flex;
            justify-content: space-around;
            width: 280px;
            margin: 0 auto;
        }
        .main .scoreAndRefresh .refreshBtn {
            height: 30px; margin-top: 22px;
        }
        .modal {
            margin-top: 7%;

        }
        .modal .modal-header h4 {
            text-align: left;
            font-weight: bold;
        }
        .modal .modal-dialog {
            width: 300px;
            height: 100px;
            margin: 0 auto;

            background:#eee;
            border-radius: 10px;
            opacity: 0.9;

            position: absolute;
            top:30%;
            left: 40%;

            display: none;

        }
        .btn-danger img{
            cursor: pointer;
            width: 25px;
            height: 25px;
            border-radius: 5px;
        }
        .modal-footer .btn{
            cursor: pointer;
            width: 70px;
            height: 30px;
            border-radius: 5px;
            text-align: center;
            color: #fff;
        }
        .modal-footer .btn-info{
            background:	#008B8B;
        }

        .modal-footer .btn-info:hover{
            font-weight: bold;
            background:	#2F4F4F;
        }

        .modal-footer  .refreshBtn{
            background:	 #8B0000;
        }
        .modal-footer .refreshBtn:hover{
            font-weight: bold;
            background:	#CD5C5C;
        }

        .modal .modal-body {
            font-size: 18px;
            font-weight: bold;
            color: red;
        }
        #resetMaxScore {
            color: #fff;
            height: 30px;

        }

game.js

$(function(){
        //是否产生新元素
        var isNewRndItme = false;
        var gameScore = 0;
        //最高分
        var maxScore = 0;

        if(localStorage.maxScore) {
            maxScore = localStorage.maxScore - 0;
        }else {
            maxScore = 0;
        }


        //游戏初始化
        gameInit();

        //上、下、左、右监听事件
        $('body').keydown(function(e) {
            switch (e.keyCode) {
                case 37:
                    // left
                    console.log('left');
                    isNewRndItme = false;
                    isGameOver();
                    break;
                case 38:
                    // up
                    console.log('up');
                    isNewRndItme = false;
                    move('up');
                    isGameOver();
                    break;
                case 39:
                    // right
                    console.log('right');
                    isNewRndItme = false;
                    move('right');
                    isGameOver();
                    break;
                case 40:
                    // down
                    console.log('down');
                    isNewRndItme = false;
                    move('down');
                    isGameOver();
                    break;
            }
        });


        function refreshGame(){
            var items = $('.gameBody .row .item');
            for(var i = 0; i < items.length; i++) {
                items.eq(i).html('').removeClass('non