完整版的网页贪吃蛇

寒假无聊在家写的

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<script>
			//1.定义地图类
			var map;
			var food;
			var snake;
			var length;
			function Map(){
				//属性 高度 宽度 颜色 位置
				//方法 show()
				this.width=800;
				this.height=400;
				this.color='#dddddd';
				this.position='absolute';
				this._map=null;
				this.overflow="hidden";
				this.show=function(){
					this._map=document.createElement("div");
					this._map.style.width=this.width+"px";
					this._map.style.height=this.height+"px";
					this._map.style.position=this.position;
					this._map.style.overflow=this.overflow;
					this._map.style.backgroundColor=this.color;
					document.getElementsByTagName("body")[0].appendChild(this._map);
				}
			}
			//2.定义食物类
			function Food(){
				this.width=20;
				this.height=20;
				this.color='green';
				this.position='absolute';
				this.x=0;
				this.y=0;
				this._food=null;
				this.show=function(){
					if(this._food==null)
					{
						this._food=document.createElement("div");
						this._food.style.width=this.width+"px";
						this._food.style.height=this.height+"px";
						this._food.style.borderRadius="50%";
						this._food.style.position=this.position;
						this._food.style.backgroundColor=this.color;
						map._map.appendChild(this._food);
					}
					this.x=Math.floor(Math.random()*40);
					this.y=Math.floor(Math.random()*20);	
					this._food.style.left=this.x*20+"px";
					this._food.style.top=this.y*20+"px";
			}
			}
			//3.定义蛇类
			function Snake(){
				this.width=20;
				this.height=20;
				this.position='absolute';
				this.direct='right';
				this.body=[[3,2,'yellow',null],[2,2,'blue',null],[1,2,'blue',null]];
				this.show=function(){
					length=this.body.length;
					for(var i=0;i<length;i++)
					{
						if(this.body[i][3]==null)
						{
							this.body[i][3]=document.createElement("div");
							this.body[i][3].style.width=this.width+"px";
							this.body[i][3].style.height=this.height+"px";
							this.body[i][3].style.borderRadius="50%";
							this.body[i][3].style.position=this.position;
							this.body[i][3].style.backgroundColor=this.body[i][2];	
							this.body[i][3].style.left=this.body[i][0]*20+"px";
							this.body[i][3].style.top=this.body[i][1]*20+"px";
					//追加div元素到map地图
							map._map.appendChild(this.body[i][3]);
						}
						this.body[i][3].style.left=this.body[i][0]*20+"px";
						this.body[i][3].style.top=this.body[i][1]*20+"px";
					}
				}
				this.setDirect=function(code){
					switch(code)
					{
						case 37:
							this.direct="left";
							break;
						case 38:
							this.direct="top";
							break;
						case 39:
							this.direct="right";
							break;
						case 40:
							this.direct="bottom";
							break;
					}
				}
//				this.eat=function(){
//					
//				}
				this.move=function(){
					//出界
					if(this.body[0][0]>39||this.body[0][1]>19||this.body[0][0]<0||this.body[0][1]<0)
					{
						console.log(this.body[0][0]);
						
						if(window.confirm("游戏结束,是否重新开始!")===true) location.href="index.html";
						else 
						    {this.move=null;}
					}
					
					
					if(this.body[0][0]==food.x&&this.body[0][1]==food.y)
					{
						this.body.push([0,0,'blue',null]);
						food.show();
					}
					var length=this.body.length;
					for(var i=length-1;i>0;i--)
					{
						this.body[i][0]=this.body[i-1][0];
						this.body[i][1]=this.body[i-1][1];
					}
					if(this.direct=='right')
					{
						this.body[0][0]+=1;
						
					}
					if(this.direct=='left')
					{
						this.body[0][0]-=1;
						
					}
					if(this.direct=='top')
					{
						this.body[0][1]-=1;
						
					}if(this.direct=='bottom')
					{
						this.body[0][1]+=1;
						
					}
					this.show();
					//碰到自己,没写好哈!!~~11!!~!~!~!!!!!~!~!!!!~!~!
					//写好了
					for(var i=1;i<=length-1;i++)
					{
						if(this.body[0][0]==this.body[i][0]&&this.body[0][1]==this.body[i][1])
						{
							if(window.confirm("游戏结束,是否重新开始!")===true) location.href="index.html";
						else 
						    {this.move=null;}
						}
					}
				}
				
			}
				window.function(){
					 map=new Map();
					 map.show();
					 food=new Food();
					 food.show();
					 snake=new Snake();
					 snake.show();
					 setInterval("snake.move()",200);
					 document.function(e){
					 	var code;
					 	code=e.keyCode;
					 	snake.setDirect(code);
					 }
				}
			
			</script>
	</body>
</html>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值