原生js写贪吃蛇(界面比较丑勿喷)

一直想给自己写点东西,于是用原生JS写了一个贪吃蛇,缅怀下童年。废话不多说了直接上代码。写的时候没有考虑性能等问题,多次用到’createElement removeChild等命令。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>贪吃蛇</title>
</head>
<style>
	body{
		background-color: #444;
	}
	#wrap{
		position: relative;
		height: 502px;
		width: 402px;
		margin: 50px auto;
		border: 1px solid;
	}
	#box{
		position: absolute;
		top: 0;
		left: 0;
		height: 502px;
		width: 402px;
		border: 1px solid red;
	}
	#box h1{
		margin-top: 50px;
		text-align: center;
		line-height: 50px;
		color: #000;
	}
	#box button{
		width: 100px;
		margin-left: 150px;
		margin-top: 200px;
		font-size: 30px;
	}
	#box1 button{
		display: none;
		width: 140px;
		margin-left: 130px;
		margin-top: 350px;
		font-size: 30px;
	}
	#box p{
		margin-top: 100px;
		margin-left: 300px;
		font-size: 20px;
		color: #fff;
	}

	#wrap p{
		display: none;
		width: 100%;
		margin-top: 150px;
		text-align: center;
		font-size: 20px;
		color: #000;
	}
	#box span{
		position: absolute;
		width: 20px;
		height: 20px;
		background-color: #ff3df1;
		border-radius: 50%;
	}

	#box1 .foods{
		position: absolute;
		width: 20px;
		height: 20px;
		border-radius: 50%;
	}
	#box1{
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}
	#box1 span{
		position: absolute;
		z-index: 100;
		text-indent: 10px;
		width: 100%;
		height: 30px;
		line-height: 30px;
		color: #000;
	}
</style>
<body>
	<div id="wrap">
		<div id="box">
			<!-- <h1>欢迎来到科科贪吃蛇</h1>
			<button type="button">PLAY</button>
			<p>版本号0.0.1</p> -->
		</div>
		<div id="box1">
			<span id="scoll"></span>
			<button type="button" id="box1btn">再来一次</button>
		</div>
		<p>笨蛋撞墙了</p>
	</div>
	<script>
		(function (){
			var wrap = document.getElementById("wrap"),
				p =wrap.getElementsByTagName("p")[0],
			    box = document.getElementById("box"),
			    boxspan =box.getElementsByTagName("span"),
			    btn= box.getElementsByTagName("button")[0],
			    box1=document.getElementById("box1"),
			    box1btn=document.getElementById("box1btn"),
			    asnake= document.getElementsByTagName("span"),
			    firstLeft = box.firstChild.offsetLeft,
				fristTop = box.firstChild.offsetTop,
				scoll = document.getElementById("scoll"),
			    timer,
			    j=0,
			    num,
		    	foodtop,
		    	arr=[],
			   	foodleft;
		   	//初始界面
		   	function init(){
		   		boxh1 = document.createElement("h1");
		   		boxh1.innerHTML = "欢迎来到科科贪吃蛇";
		   		box.appendChild(boxh1);
		   		boxbt = document.createElement("button");
		   		boxbt.innerHTML = "PLAY";
		   		boxbt.id="btn";
		   		box.appendChild(boxbt);
		   		boxp = document.createElement("h1");
		   		boxp.innerHTML = "版本号0.0.2";
		   		box.appendChild(boxp);	
		   	}
		   	init();
		   	btn= box.getElementsByTagName("button")[0];		   
		   	//开始游戏
		   	btn.onclick=function(){
			  	//清空游戏界面
			  	box.innerHTML="";
			  	//生成蛇
			  	snake();
			  	//生成食物
			  	food();
			  	//生成得分板
			  	scoll.innerHTML="得分:"+j+"";
		    }
		    //生成食物 位置随机
		    function food(){
		    	foods=document.createElement("div");
		    	foods.className = "foods";
		    	foodtop = Math.floor(Math.random()*25)*20;
		    	foodleft =Math.floor(Math.random()*20)*20;
		    	//食物的位置 随机的
	    		foods.style.top = foodtop + "px";
	    		foods.style.left= foodleft + "px";
				//食物的随机的颜色
			    fimg= Math.floor(Math.random()*10)
		    	foods.style.backgroundImage="url('img/"+fimg +".png')"; //图片的名称为1.png  				 	  2.png 这样的话就可以在实现随机显示图片 比较方便
		    	box1.appendChild(foods);
		    }
		    //生成蛇 以及蛇的运动   蛇的话利用的是数组的来存储的 吃一个食物数组长度+1并且增加位置属性
		    function snake(e){
		    	e = e || window.event;
		    	//生成初始化的蛇身
			    for (var i = 0 ; i < 5; i++) {
			    	arr.push(document.createElement("span"));
			     }
			    for (var i = 0; i < arr.length; i++) {
			     	box.appendChild(arr[i]);
			     	asnake[i].style.left = 100 +i*20+"px";
			     	asnake[i].style.top = 100 +"px";
			     }
			    firstLeft = box.lastChild.offsetLeft;
			    fristTop = box.lastChild.offsetTop;
			    //移动步长
			    var top = 0,
		    		left = 20;
		    	//运动
		    	timer = setInterval(function (){
			    	//蛇运动 键盘事件
 			    	document.onkeyup =function(e){
			    		e = e || window.event;
			    		if (top == 0) {
			    			if (e.keyCode==38) {
			    				top=-20;
			    				left = 0;
			    			}else if (e.keyCode==40) {
			    				top=20;
			    				left = 0;
			    			}
			    		}
			    		if (left==0) {
			    			if (e.keyCode==37) {
			    				left=-20;
			    				top=0;
			    			}else if (e.keyCode==39) {
			    				left=20;
			    				top=0;
			    			}
			    		}
			    	}
			    	arr.unshift(document.createElement("span"));
			    	box.appendChild(arr[0]);
			     	box.lastChild.style.left = firstLeft +left+"px";
			     	box.lastChild.style.top = fristTop+ top +"px";
			     	firstLeft = box.lastChild.offsetLeft;
			     	fristTop = box.lastChild.offsetTop;
			     	box.removeChild(box.firstChild);

			     	// 限定蛇的活动范围
					if (firstLeft<0 || firstLeft>390) {
						clearInterval(timer)
						box.innerHTML="";
						box1.removeChild(foods);
						arr.length="";
						p.style.display = 'block';
						box1btn.style.display = 'block';
					}
					if (fristTop<0 || fristTop >490) {
						clearInterval(timer)
						box.innerHTML="";
						box1.removeChild(foods);
						arr.length="";
						p.style.display = 'block';
						box1btn.style.display = 'block';
					}
					//蛇与食物相撞
		    		if (box.lastChild && touch(box.lastChild,foods)) {
		    			
							box1.removeChild(foods);
							arr.unshift(document.createElement("span"));
			    			box.appendChild(arr[0]);
			    			box.lastChild.style.left = firstLeft +left+"px";
			     			box.lastChild.style.top = fristTop+ top +"px";
							food();
							j++;
							scoll.innerHTML="得分:"+j+"";
		    		}
		}, 200 )
	}
	// obj1只能为蛇 obj2为食物
	function touch( obj1,obj2){ 
		var top1= obj1.offsetTop;
			left1= obj1.offsetLeft;
			bottom1= obj1.offsetTop + obj1.clientHeight;
			right1=obj1.offsetLeft + obj1.clientWidth;
			top2= obj2.offsetTop;
			left2= obj2.offsetLeft;
			bottom2= obj2.offsetTop + obj2.clientHeight;
			right2=obj2.offsetLeft + obj2.clientWidth;
		return !(top1 > top2 || left1 > left2 || bottom1 < bottom2 || right1 < right2)
    }
    //重新开始游戏
	box1btn.onclick = function(){
   		j=0;
   		init();
   		p.style.display = 'none';
		box1btn.style.display = 'none';
		box.innerHTML="";
		snake();
		food();
		scoll.innerHTML="得分:"+j+"";
   } 
}())		
	</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值