首先他的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>
.score01{
width: 480px;
margin: 0 auto;
padding-top: 40px;
font-size: 40px;
}
.score01>span{
color: red;
}
.girdPanel{
width: 480px;
height:480px;
border-radius: 10px;
background-color: #bbada0;
margin: 0 auto;
}
.cell{
width: 100px;
height: 100px;
margin-top: 16px;
margin-left: 16px;
background-color: #ccc0b3;
float: left;
font-size: 60px;
line-height: 100px;
text-align: center;
color: #fff;
}
.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}
.gameover{
display: none;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background-color: rgba(55,55,55,0.5);
}
.gameover>p{
width: 300px;
height: 200px;
background-color: #fff;
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -150px;
text-align: center;
border-radius: 10px;
border: 1px solid;
line-height: 65px;
font-size: 40px;
}
.gameover>p>a{
padding: 10px;
color: #fff;
background-color: #9f8d77;
border-radius: 6px;
text-decoration: none;
}
.btn{
width: 100px;
height: 50px;
background-color: #9f8d77;
color: white;
font-size: 15px;
border: 0px;
border-radius: 10px;
text-align: center;
position: relative;
left: 50%;
margin-left: -50px;
}
</style>
</head>
<body>
<p class="score01">SCORE:<span id="score01">0</span></p>
<div class="girdPanel">
<div class="cell" id="c00"></div>
<div class="cell" id="c01"></div>
<div class="cell" id="c02"></div>
<div class="cell" id="c03"></div>
<div class="cell" id="c10"></div>
<div class="cell" id="c11"></div>
<div class="cell" id="c12"></div>
<div class="cell" id="c13"></div>
<div class="cell" id="c20"></div>
<div class="cell" id="c21"></div>
<div class="cell" id="c22"></div>
<div class="cell" id="c23"></div>
<div class="cell" id="c30"></div>
<div class="cell" id="c31"></div>
<div class="cell" id="c32"></div>
<div class="cell" id="c33"></div>
</div>
<div class="gameover" id="gameover">
<p>
GAME OVER !<br>
SCORE:<span id="score02">0</span><br>
<a href="">TRY AGAIN</a>
</p>
</div>
<button class="btn">new Game</button>
<script src="./index.js"> </script>
</body>
</html>
引入script
var game = {
score : 0,//添加一个的得分属性
status: 1,//添加一个游戏状态
gameover:0,//添加一个游戏结束状态
gamerunning:1,//添加一个游戏运行状态
data:[],//储存游戏数据
start:function(){
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()
this.dataView()
},
randomNum:function(){
//生成随机位置
//判断位置上是否有数据
for(;;){
//设置行
var r = Math.floor(Math.random()*4);
//设置列
var c = Math.floor(Math.random()*4);
if(this.data[r][c] == 0){
var num = Math.random() >0.5 ? 2 : 4;
this.data[r][c] = num;
break;
}
}
},
//时间hi图更新 分数,样式,数据
dataView:function(){
for(var r=0;r<4;r++){
for(var c=0;c<4;c++){
var div = document.getElementById("c"+r+c);
//如果数组不是0的时候就改变
if(this.data[r][c] != 0){
div.innerHTML = this.data[r][c]
div.className = "cell n" +this.data[r][c]
}else{
div.innerHTML = "";
div.className = "cell"
}
}
}
},
//判断游戏有没有结束
isGameover:function(){
for(var r=0;r<4;r++){
for(var c=0;c<4;c++){
//如果游戏周围有相等的就没结束
if(this.data[r][c] == this.data[r+1][c]){
return false;
}
if(this.data[r][c] == this.data[r][c+1]){
return false;
}
//如果游戏有空位置就没结束
if(this.data[r][c] == 0){
return false;
}
}
}
return true;
},
moveLeft:function(){
var before = String(this.data)
for(var r = 0 ; r < 4;r++){
this.moveLeftRow(r)
}
var after = String(this.data)
if(before != after){
this.randomNum()
if(this.isGameover()){
this.status = this.gameover
}
this.dataView()
}
},
moveLeftRow:function(r){ //单独抽出一行进行写逻辑
for(var c = 0;c<3;c++){ //最左边的不需要移动,所以遍历3次
// 找出r行c列后面不为0的数据的位置保存至nextc中
var nextc = this.getNextinRow(r,c)
if(nextc != -1){
// 这一行中有需要移动的数据
if(this.data[r][c] == 0){ //一定是把所有需要移动的数据移动到位置上去
this.data[r][c] = this.data[r][nextc]
this.data[r][nextc] = 0;
c--;//c留在原地继续左移
}else if(this.data[r][c] == this.data[r][nextc]){
this.data[r][c] = this.data[r][c] * 2;
this.score += this.data[r][c];//分数
this.data[r][nextc] = 0;
}
}else{
// 这一行中meiyou需要移动的数据
break;
}
}
},
getNextinRow:function(r,c){
for(var i = c + 1;i<4;i++){
// 如果数据不为0,表示找到了,返回i,i对应的是当前数据的列下标
if(this.data[r][i] != 0){
return i;
}
}
return -1;//如果当前行找不到不为0的数据则返回-1标识
},
moveRight:function(){
var before = String(this.data)
for(var r = 0 ; r < 4;r++){
this.moveRightRow(r)
}
var after = String(this.data)
if(before != after){
this.randomNum()
if(this.isGameover()){
this.status = this.gameover
}
this.dataView()
}
},
moveRightRow:function(r){ //单独抽出一行进行写逻辑
for(var c = 3;c>=0;c--){ //最左边的不需要移动,所以遍历3次
// 找出r行c列后面不为0的数据的位置保存至nextc中
var nextc = this.getNextinRow1(r,c)
if(nextc != -1){
// 这一行中有需要移动的数据
if(this.data[r][c] == 0){ //一定是把所有需要移动的数据移动到位置上去
this.data[r][c] = this.data[r][nextc]
this.data[r][nextc] = 0;
c++;//c留在原地继续左移
}else if(this.data[r][c] == this.data[r][nextc]){
this.data[r][c] = this.data[r][c] * 2;
this.score += this.data[r][c];//分数
this.data[r][nextc] = 0;
}
}else{
// 这一行中meiyou需要移动的数据
break;
}
}
},
getNextinRow1:function(r,c){
for(var i = c - 1;i>=0;i--){
// 如果数据不为0,表示找到了,返回i,i对应的是当前数据的列下标
if(this.data[r][i] != 0){
return i;
}
}
return -1;//如果当前行找不到不为0的数据则返回-1标识
},
moveTop:function(){
var before = String(this.data)
for(var c = 0 ; c < 4;c++){
this.moveTopRow(c)
}
var after = String(this.data)
if(before != after){
this.randomNum()
if(this.isGameover()){
this.status = this.gameover
}
this.dataView()
}
},
moveTopRow:function(c){ //单独抽出一行进行写逻辑
for(var r = 0;r<3;r++){ //最左边的不需要移动,所以遍历3次
// 找出r行c列后面不为0的数据的位置保存至nextc中
var nextr = this.getNextinRow2(r,c)
if(nextr != -1){
// 这一行中有需要移动的数据
if(this.data[r][c] == 0){ //一定是把所有需要移动的数据移动到位置上去
this.data[r][c] = this.data[nextr][c]
this.data[nextr][c] = 0;
r--;//c留在原地继续左移
}else if(this.data[r][c] == this.data[nextr][c]){
this.data[r][c] = this.data[r][c] * 2;
this.score += this.data[r][c];//分数
this.data[nextr][c] = 0;
}
}else{
// 这一行中meiyou需要移动的数据
break;
}
}
},
getNextinRow2:function(r,c){
for(var i = r + 1;i<4;i++){
// 如果数据不为0,表示找到了,返回i,i对应的是当前数据的列下标
if(this.data[i][c] != 0){
return i;
}
}
return -1;//如果当前行找不到不为0的数据则返回-1标识
},
moveBot:function(){
var before = String(this.data)
for(var c = 0 ; c < 4;c++){
this.moveBotRow(c)
}
var after = String(this.data)
if(before != after){
this.randomNum()
if(this.isGameover()){
this.status = this.gameover
}
this.dataView()
}
},
moveBotRow:function(c){ //单独抽出一行进行写逻辑
for(var r = 3;r>=0;r--){ //最左边的不需要移动,所以遍历3次
// 找出r行c列后面不为0的数据的位置保存至nextc中
var nextr = this.getNextinRow3(r,c)
if(nextr != -1){
// 这一行中有需要移动的数据
if(this.data[r][c] == 0){ //一定是把所有需要移动的数据移动到位置上去
this.data[r][c] = this.data[nextr][c]
this.data[nextr][c] = 0;
r++;//c留在原地继续左移
}else if(this.data[r][c] == this.data[nextr][c]){
this.data[r][c] = this.data[r][c] * 2;
this.score += this.data[r][c];//分数
this.data[nextr][c] = 0;
}
}else{
// 这一行中meiyou需要移动的数据
break;
}
}
},
getNextinRow3:function(r,c){
for(var i = r - 1;i>=0;i--){
// 如果数据不为0,表示找到了,返回i,i对应的是当前数据的列下标
if(this.data[i][c] != 0){
return i;
}
}
return -1;//如果当前行找不到不为0的数据则返回-1标识
}
}
game.start()
document.onkeydown = function(event){
if(event.keyCode == 37){
game.moveLeft()
}
else if(event.keyCode == 38){
game.moveTop();
}
else if(event.keyCode == 39){
game.moveRight();
}
else if(event.keyCode == 40){
game.moveBot();
}
}
效果图展示