JS贪吃蛇游戏个人练习之用放在这备份一下
复制代码 代码如下:
<!DOCTYPE html>
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>JS贪吃蛇练习</title>
<style type="text/css">
#pannel table {
bordercollapse: collapse;
}
#pannel table td {
border: px solid #;
width: px;
height: px;
fontsize: ;
lineheight: ;
overflow: hidden;
}
#pannel table snake {
backgroundcolor: green;
}
#pannel table food {
backgroundcolor: blue;
}
</style>
<script type="text/javascript">
var Direction = new function () {
thisUP = ;
thisRIGHT = ;
thisDOWN = ;
thisLEFT = ;
};
var Common = new function () {
thiswidth = ; /*水平方向方格数*/
thisheight = ; /*垂直方向方格数*/
thisspeed = ; /*速度 值越小越快*/
thisworkThread = null;
};
var Main = new function () {
var control = new Control();
windowonload = function () {
controlInit("pannel");
/*开始按钮*/
documentgetElementById("btnStart")onclick = function () {
controlStart();
thisdisabled = true;
documentgetElementById("selSpeed")disabled = true;
documentgetElementById("selSize")disabled = true;
};
/*调速度按钮*/
documentgetElementById("selSpeed")onchange = function () {
Commonspeed = thisvalue;
}
/*调大小按钮*/
documentgetElementById("selSize")onchange = function () {
Commonwidth = thisvalue;
Commonheight = thisvalue;
controlInit("pannel");
}
};
};
/*控制器*/
function Control() {
thissnake = new Snake();
thisfood = new Food();
/*初始化函数创建表格*/
thisInit = function (pid) {
var html = [];
htmlpush("<table>");
for (var y = ; y < Commonheight; y++) {
htmlpush("<tr>");
for (var x = ; x < Commonwidth; x++) {
htmlpush(<td id="box_ + x + "_" + y + "></td>);
}
htmlpush("</tr>");
}
htmlpush("</table>");
thispannel = documentgetElementById(pid);
thispannelinnerHTML = htmljoin("");
};
/*开始游戏 监听键盘创建食物刷新界面线程*/
thisStart = function () {
var me = this;
thisMoveSnake = function (ev) {
var evt = windowevent || ev;
mesnakeSetDir(evtkeyCode);
};
try {
documentattachEvent("onkeydown" thisMoveSnake);
} catch (e) {
documentaddEventListener("keydown" thisMoveSnake false);
}
thisfoodCreate();
CommonworkThread = setInterval(function () {
mesnakeEat(mefood); mesnakeMove();
} Commonspeed);
};
}
/*蛇*/
function Snake() {
thisisDone = false;
thisdir = DirectionRIGHT;
thispos = new Array(new Position());
/*移动 擦除尾部向前移动判断游戏结束(咬到自己或者移出边界)*/
thisMove = function () {
documentgetElementById("box_" + thispos[]X + "_" + thispos[]Y)className = "";
//所有 向前移动一步
for (var i = ; i < thisposlength ; i++) {
thispos[i]X = thispos[i + ]X;
thispos[i]Y = thispos[i + ]Y;
}
//重新设置头的位置
var head = thispos[thisposlength ];
switch (thisdir) {
case DirectionUP:
headY;
break;
case DirectionRIGHT:
headX++;
break;
case DirectionDOWN:
headY++;
break;
case DirectionLEFT:
headX;
break;
}
thispos[thisposlength ] = head;
//遍历画蛇同时判断游戏结束
for (var i = ; i < thisposlength; i++) {
var isExits = false;
for (var j = i + ; j < thisposlength; j++)
if (thispos[j]X == thispos[i]X && thispos[j]Y == thispos[i]Y) {
isExits = true;
break;
}
if (isExits) { thisOver();/*咬自己*/ break; }
var obj = documentgetElementById("box_" + thispos[i]X + "_" + thispos[i]Y);
if (obj) objclassName = "snake"; else { thisOver();/*移出边界*/ break; }
}
thisisDone = true;
};
/*游戏结束*/
thisOver = function () {
clearInterval(CommonworkThread);
alert("游戏结束!");
}
/*吃食物*/
thisEat = function (food) {
var head = thispos[thisposlength ];
var isEat = false;
switch (thisdir) {
case DirectionUP:
if (headX == foodposX && headY == foodposY + ) isEat = true;
break;
case DirectionRIGHT:
if (headY == foodposY && headX == foodposX ) isEat = true;
break;
case DirectionDOWN:
if (headX == foodposX && headY == foodposY ) isEat = true;
break;
case DirectionLEFT:
if (headY == foodposY && headX == foodposX + ) isEat = true;
break;
}
if (isEat) {
thispos[thisposlength] = new Position(foodposX foodposY);
foodCreate(thispos);
}
};
/*控制移动方向*/
thisSetDir = function (dir) {
switch (dir) {
case DirectionUP:
if (thisisDone && thisdir != DirectionDOWN) { thisdir = dir; thisisDone = false; }
break;
case DirectionRIGHT:
if (thisisDone && thisdir != DirectionLEFT) { thisdir = dir; thisisDone = false; }
break;
case DirectionDOWN:
if (thisisDone && thisdir != DirectionUP) { thisdir = dir; thisisDone = false; }
break;
case DirectionLEFT:
if (thisisDone && thisdir != DirectionRIGHT) { thisdir = dir; thisisDone = false; }
break;
}
};
}
/*食物*/
function Food() {
thispos = new Position();
/*创建食物 随机位置创建立*/
thisCreate = function (pos) {
documentgetElementById("box_" + thisposX + "_" + thisposY)className = "";
var x = y = isCover = false;
/*排除蛇的位置*/
do {
x = parseInt(Mathrandom() * (Commonwidth ));
y = parseInt(Mathrandom() * (Commonheight ));
isCover = false;
if (pos instanceof Array) {
for (var i = ; i < poslength; i++) {
if (x == pos[i]X && y == pos[i]Y) {
isCover = true;
break;
}
}
}
} while (isCover);
thispos = new Position(x y);
documentgetElementById("box_" + x + "_" + y)className = "food";
};
}
function Position(x y) {
thisX = ;
thisY = ;
if (argumentslength >= ) thisX = x;
if (argumentslength >= ) thisY = y;
}
</script>
</head>
<body>
<div id="pannel" style="marginbottom: px;"></div>
<select id="selSize">
<option value="">*</option>
<option value="">*</option>
<option value="">*</option>
</select>
<select id="selSpeed">
<option value="">速度慢</option>
<option value="" selected="selected">速度中</option>
<option value="">速度快</option>
</select>
<input type="button" id="btnStart" value="开始" />
</body>
</html>