项目名称:贪吃蛇详细文档
一、首先要有背景,在这里的背景,我是在<body>里面直接写div的方式,然后在<style>里用CSS设置样式,这样写一个div,设置div的大小样式用来做贪吃蛇活动的空间,背景map就算完成了。
二、接下来就是先产生对象,这里的对象有蛇和食物,从简单的食物对象开始,先创建一个食物对象Food:
设置大小、定位、颜色等属性和show()方法,这里设置一个属性为_food属性,开始的this._food属性值为null,表示没有食物对象,接着在show()方法中:if(this._food==null){ this._food=document.createElement("div");
……
}
在第一次进来时是没有食物对象的,那么久创建一个div,然后下面开始设置它的样式。添加进map里面
接着如果下次进来时this._food已经不为空了,则只要改变它的坐标即可,this.x=Math.round(Math.random()*29);
this.y=Math.round(Math.random()*19);
this._food.style.left=this.x*this.width+"px";
this._food.style.top=this.y*this.height+"px";
因为我设置的背景是600*400的,所以设置两个属性x和y,x是产生的0-29的一个数字,y同理,因为对象的长宽都是20px,产生它们就可以实现用坐标的方式来控制对象了,刷新时
一、首先要有背景,在这里的背景,我是在<body>里面直接写div的方式,然后在<style>里用CSS设置样式,这样写一个div,设置div的大小样式用来做贪吃蛇活动的空间,背景map就算完成了。
二、接下来就是先产生对象,这里的对象有蛇和食物,从简单的食物对象开始,先创建一个食物对象Food:
设置大小、定位、颜色等属性和show()方法,这里设置一个属性为_food属性,开始的this._food属性值为null,表示没有食物对象,接着在show()方法中:if(this._food==null){ this._food=document.createElement("div");
……
}
在第一次进来时是没有食物对象的,那么久创建一个div,然后下面开始设置它的样式。添加进map里面
接着如果下次进来时this._food已经不为空了,则只要改变它的坐标即可,this.x=Math.round(Math.random()*29);
this.y=Math.round(Math.random()*19);
this._food.style.left=this.x*this.width+"px";
this._food.style.top=this.y*this.height+"px";
因为我设置的背景是600*400的,所以设置两个属性x和y,x是产生的0-29的一个数字,y同理,因为对象的长宽都是20px,产生它们就可以实现用坐标的方式来控制对象了,刷新时