js写2048小游戏

首先页面布局

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="demo1.css">
</head>
<body>
	<div class="all">
		<div class="head">score:<span id="sco">0</span></div>
		<div class="center">
			<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>
	<div class="foot" id="gameover">
		<div class="end">
	      <div class="one">GAME OVER!!!</div>
	      <div class="two">SCORE:<span id ='score'></span></div>
	      <a href="demo1.html" style="text-decoration: none;"><div class="three">try again</div><a>
	    </div>
    </div>
	 <script type="text/javascript" src="demo.js"></script>
</body>
</html>

效果:

在这里插入图片描述
接下来上js:

var game = {
data:[],//定义一个数组,用来存储所有的游戏数据
score:0,//定义一个存储分数的属性
gameover:0,//定义一个游戏结束时
gamerunning:1,//定义一个游戏运行的状态
status:1,//这是目前游戏的状态,时刻跟上面的两个状态作比较,来确定游戏游戏处于运行还是结束的状态
start:function(){//将页面初始化 游戏开始时候的方法
	this.data = [];
	this.status = this.gamerunning;
	//游戏开始的时候分数清空
	this.score = 0;
	for(var i = 0; i < 4; i++){
		this.data[i] = [];//生成4行[]
		for(var j = 0; j < 4; j++){
			this.data[i][j] = 0;//生成4列[]
		}
	}
	/*console.log(this.data);//最终结果 4行4列*/
	this.randomNum();//第一个数生成时 需要调用一次随机数
	this.randomNum();//第二个数随机生成时需要再一次调用随机数
	this.dataView();
	console.log(this.data)

},


randomNum:function(){//随机数的生成,开始的时候随即生成,移动的时候随机生成
	for(;;){//死循环
		var i = Math.floor(Math.random()*4);//随机生成一个行
		var j = Math.floor(Math.random()*4);//随机生成一个列
		
		if (this.data[i][j] == 0) {
			var num = Math.random() > 0.2 ?2:4;
			this.data[i][j] = num;
			break;
		}
	}
},


dataView:function(){//将数字加载到页面中
	//把所有的元素都遍历一遍
	for(var i = 0; i < 4; i++){
		for(var j = 0; j < 4; j++){
			var ids = document.getElementById("c"+i+j);//找到对应的div 利用字符串拼接
			/*console.log(ids);*/
			if (this.data[i][j] == 0) {//如果对应的元素为0 则不显示 为空
				ids.innerHTML= "";
				ids.className = "cell";//className不变
			}else{//数组中对应的内容放到格子里
				ids.innerHTML = this.data[i][j];
				ids.className = "cell n" + this.data[i][j]
			}
		}
	}
	//更新分数
	document.getElementById("sco").innerHTML = this.score;
	//游戏没有结束的时候,弹出层时刻是隐藏的
	if(this.status == this.gamerunning){
		document.getElementById("gameover").style.display = "none";
	}else{
		document.getElementById("gameover").style.display = "block";
		document.getElementById("score").innerHTML = this.score;
	}
},

//判断游戏是否结束
isgameover:  function(){
	for(var i = 0 ; i < 4; i++){
		for(var j = 0; j < 4;j++){
			if (this.data[i][j]== 0) {//当个字格子为0时 表示还有空位置 
				return false;//此时游戏还未结束
			}
			if(j < 3){
				if(this.data[i][j+1] == this.data[i][j]){//左边的后一列与前一列来对比
						return false;
				}
			}
		}
	}

	return true;
},
//左移动的方法
LeftMove:function(){
	var before = String(this.data);//记录此时的数据 并转换成字符串 方便后面进行对比
	console.log(before);
	for(var i = 0; i < 4; i++){
			this.LeftMoveRow(i);
	}
	var after = String(this.data);//方便与之前的进行对比
	console.log(after);
	if(before != after){
		this.randomNum();	//生成随机数
		//生成的随机数可能造成游戏结束
		if(this.isgameover()){
			//改变游戏的状态
			this.status = this.gameover;//
		}
		//更新视图
		this.dataView();
	}

},
LeftMoveRow:function(i){
	for(var j= 0; j < 3; j++){//
		var nextj = this.getNextinRow(i,j);
		if (nextj != -1) {
			if (this.data[i][j]==0) {
				//等于0时直接替换
				this.data[i][j] = this.data[i][nextj];
				this.data[i][nextj] = 0;//位置恢复为0
				j--;//要让位置恢复到原位
			}else if (this.data[i][j] == this.data[i][nextj]){
				this.data[i][j] *= 2;
				this.data[i][nextj] = 0;
				this.score += this.data[i][j];//更新位置
			}
		}else{//没有找到
			break;//直接退出循环
		}
	}
},
getNextinRow:function(i,j){
	for(var newj = j + 1; newj < 4; newj++){
		if (this.data[i][newj] != 0){
			return newj; //表示已经找到位置,并且把位置返回出来
		}
	}
	return -1; //返回一个标识符
},


//右移动
RightMove:function(){
	var before = String(this.data);//记录此时的数据 并转换成字符串 方便后面进行对比
	console.log(before);


	for(var i = 0; i < 4; i++){
			this.RightMoveRow(i);
	}

	var after = String(this.data);//方便与之前的进行对比
	console.log(after);
	if(before != after){//将之前的来进行对比
		this.randomNum();	//生成的一个随机数
		//生成的随机数可能造成游戏结束
		if(this.isgameover()){
			//改变游戏的状态
			this.status = this.gameover;//
		}
		//更新视图
		this.dataView();
	}

},
RightMoveRow:function(i){
	for(var j= 4 ; j > 0; j--){//
		var nextj = this.RihtlastRow(i,j);
		if (nextj != -1) {
			if (this.data[i][j]==0) {
				//等于0时直接替换
				this.data[i][j] = this.data[i][nextj];
				this.data[i][nextj] = 0;//位置恢复为0
				j++;//要让位置恢复到原位
			}else if (this.data[i][j] == this.data[i][nextj]){
				this.data[i][j] *= 2;
				this.data[i][nextj] = 0;
				this.score += this.data[i][j];//更新位置
			}
		}else{//没有找到
			break;//直接退出循环
		}
	}
},
RihtlastRow:function(i,j){
	for(var newj = j - 1; newj >= 0; newj--){
		if (this.data[i][newj] != 0){
			return newj; //表示已经找到位置,并且把位置返回出来
		}
	}
	return -1; //返回一个标识符
},


//上移动的方法
TopMove:function(){
	var before = String(this.data);//记录此时的数据 并转换成字符串 方便后面进行对比
	console.log(before);
	for(var j = 0; j < 4; j++){
			this.TopMoveRow(j);
	}
	var after = String(this.data);//方便与之前的进行对比
	console.log(after);
	if(before != after){
		this.randomNum();	//生成随机数
		//生成的随机数可能造成游戏结束
		if(this.isgameover()){
			//改变游戏的状态
			this.status = this.gameover;//
		}
		//更新视图
		this.dataView();
	}

},
TopMoveRow:function(j){
	for(var i= 0; i < 4; i++){//
		var nexti = this.TopNextinRow(i,j);
		if (nexti != -1) {
			if (this.data[i][j]==0) {
				//等于0时直接替换
				this.data[i][j] = this.data[nexti][j];
				this.data[nexti][j] = 0;//位置恢复为0
				i--;//要让位置恢复到原位
			}else if (this.data[i][j] == this.data[nexti][j]){
				this.data[i][j] *= 2;
				this.data[nexti][j] = 0;
				this.score += this.data[i][j];//更新位置
			}
		}else{//没有找到
			break;//直接退出循环
		}
	}
},
TopNextinRow:function(i,j){
	for(var nexti = i + 1; nexti < 4; nexti++){
		if (this.data[nexti][j] != 0){
			return nexti; //表示已经找到位置,并且把位置返回出来
		}
	}
	return -1; //返回一个标识符
},


//下移动的方法
	BottomMove:function(){
	var before = String(this.data);//记录此时的数据 并转换成字符串 方便后面进行对比
	console.log(before);
	for(var j = 0; j < 4; j++){
			this.BottomMoveRow(j);
	}
	var after = String(this.data);//方便与之前的进行对比
	console.log(after);
	if(before != after){
		this.randomNum();	//生成随机数
		//生成的随机数可能造成游戏结束
		if(this.isgameover()){
			//改变游戏的状态
			this.status = this.gameover;//
		}
		//更新视图
		this.dataView();
	}

},
BottomMoveRow:function(j){
	for(var i= 3; i > 0; i--){//
		var nexti = this.BottomNextinRow(i,j);
		if (nexti != -1) {
			if (this.data[i][j]==0) {
				//等于0时直接替换
				this.data[i][j] = this.data[nexti][j];
				this.data[nexti][j] = 0;//位置恢复为0
				i--;//要让位置恢复到原位
			}else if (this.data[i][j] == this.data[nexti][j]){
				this.data[i][j] *= 2;
				this.data[nexti][j] = 0;
				this.score += this.data[i][j];//更新位置
			}
		}else{//没有找到
			break;//直接退出循环
		}
	}
},
BottomNextinRow:function(i,j){
	for(var nexti = i -1; nexti >= 0; nexti--){
		if (this.data[nexti][j] != 0){
			return nexti; //表示已经找到位置,并且把位置返回出来
		}
	}
	return -1; //返回一个标识符
},
}
game.start();
document.onkeydown = function(event){
	var event = event || e ||arguments[0];
	if (event.keyCode == 37){
		game.LeftMove();
	}else if(event.keyCode == 38){
		game.TopMove();
	}else if(event.keyCode == 39){
		game.RightMove();
	}else if(event.keyCode == 40){
		game.BottomMove();
	}
}

var startX,startY,endX,endY;
document.addEventListener('touchstart',function(){
	console.log(event);
	startX = event.touches[0].pageX;
	startY = event.touches[0].pageY;
})
document.addEventListener('touchend',function(){
	endX = event.changedTouches[0].pageX;
	endY = event.changedTouches[0].pageY;
	var X = endX - startX;
    var Y = endY - startY
    var absX = Math.abs(X) > Math.abs(Y);
    var absY = Math.abs(Y) > Math.abs(X);
    if(X > 0 && absX){
        console.log("右滑动");
        game.RightMove();
    }else if(X < 0 && absX){
        console.log("左滑动")
        game.LeftMove();
    }if(Y > 0 && absY){
        console.log("下滑动");
        game.BottomMove();
    }if(Y < 0 && absY){
        console.log("上滑动");
        game.TopMove();
    }
})

注释写的很清楚 多余的就不说了 应该看得懂
但是!!!
有一个小小bug留给自行发现哟~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值