实战项目-2048小游戏

在这里插入图片描述

html+css代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:  0 auto;
            padding: 0;
        }
        :root,body{
            width: 100%;
            height: 100%;
        }
        .content{
            /* width: 800px; */
            width: 100%;
            height: 100%;
            text-align: center;
            background-color: #FAF8EF;
            font-family: Arial;
        }
        .top{
            width: 440px;
            padding: 20px;
        }
        .mid{
            width: 440px;
            background-color: #bbada0;
            text-align: center;
            padding: 5px;
            border-radius: 1.5%;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: #ccc0b3;
            float: left;
            margin: 5px;
        }
        .tofs{
            font-size: 70px;
            text-align: left;
            color: #776E65;
        }
        .top span{
            display: inline-block;
            font-size: 18px;
            /* float: right; */
            width: 70px;
            height: 40px;
            background-color: aliceblue;
            text-align: center;
            padding: 10px;
            background-color: #BBADA0;
            /* margin-top: -10px; */
            color: aliceblue;
            border-radius: 3px;
        }
        .span1{
            margin-left: 90px;
            margin-right: 10px;
        }
        .btn1{
            width: 120px;
            height: 35px;
            background-color: #8F7A66;
            border-radius: 3px;
            border: 0;
            color: aliceblue;
            margin-top: 20px;
            margin-left: 230px;
            /* text-align: right; */
        }
        .clear{
            clear: both;
        }
        .son{
            font-size: 60px;
            text-align: center;
            line-height: 100px;
        }
        .n2{background-color:#eee3da;color:#776e65}
        .n4{background-color:#ede0c8;color:#776e65}
        .n8{background-color:#f2b179}
        .n16{background-color:#f59563}
        .n32{background-color:#f67c5f}
        .n64{background-color:#f65e3b}
        .n128{background-color:#edcf72}
        .n256{background-color:#edcc61}
        .n512{background-color:#9c0}
        .n1024{background-color:#33b5e5;font-size:40px}
        .n2048{background-color:#09c;font-size:40px}
        .n4096{background-color:#a6c;font-size:40px}
        .n8192{background-color:#93c;font-size:40px}
        .over{
            font-size: 50px;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            display: none;
            background-color: rgba(33, 55, 56, 0.3);
            
        }
        .pp {
            width: 300px;
            height: 200px;
            top: 50%;
            left: 50%;
            position: absolute;
            margin-top: -100px;
            margin-left: -150px;
            background-color: rgb(123, 132, 156);
            padding: 20px;
        }
        .btn2{
            width: 120px;
            height: 55px;
            background-color: #ad937b;
            border-radius: 3px;
            border: 0;
            color: aliceblue;
        }
        .score01 {
            color: red;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="top">
            <p class="tofs">2048<span class="span1">SCORE <br><b class="score01">0</b></span><span>BEST  <br>0</span> </p>
            
            <p><button class="btn1">New Game</button></p>
        </div>
        <div class="mid">
            <div class="son c00" id="c00"></div>
            <div class="son c01" id="c01"></div>
            <div class="son c02" id="c02"></div>
            <div class="son c03" id="c03"></div>

            <div class="son c10" id="c10"></div>
            <div class="son c11" id="c11"></div>
            <div class="son c12" id="c12"></div>
            <div class="son c13" id="c13"></div>

            <div class="son c20" id="c20"></div>
            <div class="son c21" id="c21"></div>
            <div class="son c22" id="c22"></div>
            <div class="son c23" id="c23"></div>

            <div class="son c30" id="c30"></div>
            <div class="son c31" id="c31"></div>
            <div class="son c32" id="c32"></div>
            <div class="son c33" id="c33"></div>
            <div class="clear"></div1>
        </div>
        <div class="over">
            <div class="pp">
                <p>Game Over!</p>
                <p>SCORE:<span class="score02">0</span></p>
                <p><button class="btn2">Try again</button></p>
            </div>
        </div>
    </div>
</body>
<script src="js.js"></script>
</html>

js功能代码

var game = {
    score : 0,  //游戏得分
    status : 1, // 游戏状态
    gameover : 0,// 游戏结束状态
    gamerunning : 1, // 游戏开始状态
    data : [],
    start : function(){
        this.status = this.gamerunning
        this.data = []
        this.score = 0
        // 生成二维数组,放置保存16个0
        for(var r = 0;r < 4;r++){
            this.data[r] = []
            for(var c = 0;c < 4;c++){
                this.data[r][c] = 0
            }
        }
        this.randomNum()
        this.randomNum()
        console.log(this.data);
        this.dataView()
    },
    randomNum : function(){
        while(true){
            var x = null,y = null,z = null;
            x = this.getRandom(0,3);
            y = this.getRandom(0,3);
            z = Math.random() < 0.8 ? 2 : 4
            
            if(this.data[x][y] == 0){
                this.data[x][y] = z
                break;
            }

        }
    },
    // 获取随机数
    getRandom : function (min,max){
        return Math.floor(Math.random() * (max - min + 1)) + min
    },
    // 试图渲染
    dataView : function(){
        for(var x = 0;x < 4;x++){
            for(var y = 0;y < 4;y++){
                var div = document.getElementById("c" +x+y)
                if(this.data[x][y] !=0){
                    div.innerHTML = this.data[x][y]
                    div.className = "son n"+this.data[x][y]
                }else{
                    div.innerHTML = "";
                    div.className = "son";
                }
            }
        }
        // 找到得分id,设置分数
        document.getElementsByClassName("score01")[0].innerHTML = this.score
        // 判断状态
        if(this.status == this.gameover){
            document.getElementsByClassName("score02")[0].innerHTML = this.score;
            document.getElementsByClassName("over")[0].style.display = "block"
        }else{
            document.getElementsByClassName("over")[0].style.display = "none"
        }
    },
    isGameover : function(){
        for(var x = 0;x < 4;x++){
            for(var y = 0;y < 4;y++){
                if(this.data[x][y] == 0){
                    return false
                }
                if(x < 3){
                    if(this.data[x][y] == this.data[x+1][y]){
                        return false
                    }
                }
                if(y < 3){
                    if(this.data[x][y] == this.data[x][y+1]){
                        return false
                    }
                }
            }
        }
        return true;
    },
    // 往左移动
    moveLeft : function(){
        var before = String(this.data)
        for(var x = 0;x < 4;x++){
            this.moveLeftRow(x)
        }
        var after = String(this.data)
        if(before != after){
            // 点击后随机生成一个新的数
            this.randomNum()
            if(this.isGameover()){
                this.status = this.gameover
            }
            this.dataView()
        }
    },
    moveLeftRow : function(x){
        for(var y = 0; y < 3;y++){
            var nexty = this.getNextinRow(x,y)
            if(nexty != -1){
                if(this.data[x][y] == 0){
                    this.data[x][y] = this.data[x][nexty]
                    this.data[x][nexty] = 0
                    y--;
                }else if(this.data[x][y] == this.data[x][nexty]){
                    this.data[x][y] = this.data[x][y]*2
                    this.score += this.data[x][y]
                    this.data[x][nexty] = 0;
                }
            }else{
                break;
            }
        }
    },
    getNextinRow : function(x,y){
        for(var i = y + 1;i < 4;i++){
            // 如果数据不为0,表示找到了,返回i,i对应的是当前数据的列下标
            if(this.data[x][i] != 0){
                return i;
            }
        }
        return -1;//如果当前行找不到不为0的数据则返回-1标识
    },
    // 往右移动
    moveRight : function(){
        var before = String(this.data)
        for(var x = 0;x < 4;x++){
            this.moveRightRow(x)
        }
        var after = String(this.data)
        if(before != after){
            this.randomNum()
            if(this.isGameover()){
                this.status = this.gameover
            }
            this.dataView()
        }
    },
    moveRightRow : function(x){
        for(var y = 3;y > 0;y--){
            var nexty = this.getNextinRowRight(x,y)
            if(nexty != -1){
                if(this.data[x][y] == 0){
                    this.data[x][y] = this.data[x][nexty]
                    this.data[x][nexty] = 0
                    y++;
                }else if(this.data[x][y] == this.data[x][nexty]){
                    this.data[x][y] = this.data[x][y]*2
                    this.score += this.data[x][y]
                    this.data[x][nexty] = 0
                }
            }else{
                break;
            }
        }
    },
    getNextinRowRight : function(x,y){
        for(var i = y-1;i > -1;i--){
            if(this.data[x][i] != 0){
                return i;
            }
        }
        return -1;
    },
    // 向下移动
    moveBottom : function(){
        var before = String(this.data)
        for(var y = 0;y < 4;y++){
            this.moveBottomRow(y)
        }
        var after = String(this.data)
        if(before != after){
            this.randomNum()
            if(this.isGameover()){
                this.status = this.gameover
            }
            this.dataView()
        }
    },
    moveBottomRow : function(y){
        for(var x = 3;x > 0;x--){
            var nextx = this.getNextinRowBottom(x,y)
            
            if(nextx != -22){
                if(this.data[x][y] == 0){
                    this.data[x][y] = this.data[nextx][y]
                    this.data[nextx][y] = 0
                    x++;
                }else if(this.data[x][y] == this.data[nextx][y]){
                    this.data[x][y] = this.data[x][y]*2
                    this.score += this.data[x][y]
                    this.data[nextx][y] = 0
                }
            }else{
                break;
            }
        }
    },
    getNextinRowBottom : function(x,y){
        for(var i = x-1;i > -1;i--){
            if(this.data[i][y] != 0){
                console.log("a");
                return i;
            }
        }
        return -22;
    },
    // 向上移动
    moveTop : function(){
        var before = String(this.data)
        for(var y = 0;y < 4;y++){
            this.moveTopRow(y);
        }
        var after = String(this.data)
        if(before != after){
            this.randomNum()
            if(this.isGameover()){
                this.status = this.gameover
            }
            this.dataView()
        }
    },
    moveTopRow : function(y){
        for(var x = 0;x<3;x++){
            var nextx = this.getNextinRowTop(x,y)
            if(nextx != -1){
                if(this.data[x][y] ==0){
                    this.data[x][y] = this.data[nextx][y]
                    this.data[nextx][y] = 0
                    x--;
                }else if(this.data[x][y] == this.data[nextx][y]){
                    this.data[x][y] = this.data[x][y]*2
                    this.score += this.data[x][y]
                    this.data[nextx][y] = 0;
                }
            }else{
                break;
            }
        }
    },
    getNextinRowTop : function(x,y){
        for(var i = x+1;i < 4;i++){
            if(this.data[i][y] != 0){
                return i
            }
        }
        return -1
    }
}
game.start()
document.onkeydown = function(event){
    if(event.keyCode == 37){
        game.moveLeft()
    }
    if(event.keyCode == 39){
        game.moveRight()
    }
    if(event.keyCode == 38){
        game.moveTop()
    }
    if(event.keyCode == 40){
        console.log(123123);
        game.moveBottom()
    }
}
var over = document.getElementsByClassName("over")[0]
var again = document.getElementsByClassName("btn2")[0]
again.onclick = function(){
    game.start()
    console.log(over);
    game.status = game.gamerunning
    document.getElementsByClassName("over")[0].style.display = "none"
}

完成展示效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值