代码实现
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//构造地图父类
//定义地图子类
var map
function Map()
{
//地图的属性
this.width=1200
this.height=600
this.margin="0 auto"
this.backgroundColor="black"
//地图的方法
//创建地图插入到body中//先存在一个空的
this._Map=null
//封装方法创建地图插到页面中
this.creatMap=function(){
if(this._Map==null)
{
this._Map=document.createElement("div")
this._Map.style.position="relative"
this._Map.style.width=this.width+"px"
this._Map.style.height=this.height+"px"
this._Map.style.margin=this.margin
this._Map.style.backgroundColor=this.backgroundColor
document.body.appendChild(this._Map)
}
}
}
//创建蛇的父类
//创建蛇的子类
var snack
function Snack()
{
//默认蛇的移动方向为右
this.direct="right"
this.width=30
this.height=30
//创建蛇出现的方法并将它加到地图里,先让它存在为空,用数组写其余样式
this.snackbody=[[3,1,"blue",null],[2,1,"white",null],[1,1,"white",null]];
this.creatsnack=function()
{
for(var i=0;i<this.snackbody.length;i++)
{
if(this.snackbody[i][3]==null)
{
this.snackbody[i][3]=document.createElement("div")
this.snackbody[i][3].style.position="absolute"
this.snackbody[i][3].style.width=this.width+"px"
this.snackbody[i][3].style.height=this.height+"px"
this.snackbody[i][3].style.backgroundColor=this.snackbody[i][2]
//将蛇加到地图里,注意创建的地图也是父类地图的属性要用.获取
map._Map.appendChild(this.snackbody[i][3])
}
//放在if外面因为如果存在则不用再创建,直接换位置就好
this.snackbody[i][3].style.left=this.snackbody[i][0]*this.width+"px";
this.snackbody[i][3].style.top=this.snackbody[i][1]*this.height+"px";
}
}
//创建蛇移动的方法,此方法中只要改变数组坐标再调用创建蛇中通过坐标改变lefttop值即可
this.snackmove=function()
{
var len=this.snackbody.length-1 //从后往前替换属性值,先获取长度
for(var i=len;i>0;i--)
{
//蛇尾移动的方法
this.snackbody[i][0]=this.snackbody[i-1][0]
this.snackbody[i][1]=this.snackbody[i-1][1]
}
//蛇头单独移动的方式 //注意要在蛇尾变了之后在改变
switch (this.direct)
{
case"right":this.snackbody[0][0]+=1;break;
case"left":this.snackbody[0][0]-=1;break;
case "up":this.snackbody[0][1]-=1; break;
case "down":this.snackbody[0][1]+=1; break;
}
//创建蛇吃到食物方法
//去判断蛇头和食物是否坐标一样
if(this.snackbody[0][0]==food.x&&this.snackbody[0][1]==food.y)
{
//开始吃
this.snackbody.push([
this.snackbody[this.snackbody.length-1][0], //插入数组尾部最后一个元素
this.snackbody[this.snackbody.length-1][1],
"white",
null
]);
food.creatfood()
}
this.creatsnack()//如果在一个父类里引用用this.在外面引用引用一定要用实例化的对象点.
this.chuqu()
this.zhuang();
}
//判断蛇是否穿墙的方法,也可以写在remove方法里,不过为了代码优化,封装成方法,直接调用即可
this.chuqu=function(){
if(this.snackbody[0][0]>=40){
this.snackbody[0][0]=0
}
if(this.snackbody[0][0]<0){
this.snackbody[0][0]=39
}
if(this.snackbody[0][1]>=20){
this.snackbody[0][1]=0
}
if(this.snackbody[0][1]<0){
this.snackbody[0][1]=19
}
}
//判断蛇是否自己撞到自己的身体
this.zhuang=function(){
for(var i=1;i<this.snackbody.length;i++)
{
if(this.snackbody[0][0]==this.snackbody[i][0]&&this.snackbody[0][1]==this.snackbody[i][1])
{
clearInterval(time);
alert("撞死啦!");
}
}
}
}
//创建食物父类
//创建食物子类
var food;
function Food() {
this.width = 30
this.height = 30
this.color = "yellow"
this.x = 0
this.y = 0
this.position = "absolute"
//创建食物的方法并添加到地图上
this._food = null
this.creatfood = function () {
this.x = Math.floor(Math.random() * 40)
this.y = Math.floor(Math.random() * 20)
if (this._food == null) {
this._food=document.createElement("div")
this._food.style.position=this.position
this._food.style.width = this.width+"px"
this._food.style.height=this.width+"px"
this._food.style.backgroundColor=this.color
map._Map.appendChild(this._food)
}
this._food.style.left=this.x*this.width+"px"
this._food.style.top=this.y*this.height+"px"
}
}
var time;
window.onload=function()
{
//实例化地图的类
map=new Map()
map.creatMap()
snack=new Snack()
snack.creatsnack()
//用键盘按键控制蛇的移动方向
document.οnkeydοwn=function(e)
{
switch (e.keyCode)
{
case 37:if(snack.direct!="right"){snack.direct="left"};break /*在外面引用引用一定要用实例化的对象点*/
case 38:if(snack.direct!="down"){snack.direct="up"};break;
case 39: if(snack.direct!="left"){snack.direct="right"};break;
case 40: if(snack.direct!="up"){snack.direct="down"};break;
}
}
food=new Food()
food.creatfood()
time=setInterval("snack.snackmove()",100)
}
</script>
</body>
</html>